|
| 1 | +# Extract Execution Plans from Query Store |
| 2 | + |
| 3 | +Pull execution plans from SQL Server Query Store for performance analysis. |
| 4 | + |
| 5 | +## Purpose |
| 6 | + |
| 7 | +Extracts actual execution plans (as XML) from Query Store and saves them as .sqlplan files. |
| 8 | +The SQL Analyzer can then analyze these plans for: |
| 9 | +- Expensive sort operations |
| 10 | +- Table/index scans |
| 11 | +- Missing indexes |
| 12 | +- High-cost operators |
| 13 | +- Implicit conversions |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- SQL Server with Query Store enabled |
| 18 | +- Connection details in .env file or passed as parameters |
| 19 | +- Query Store must have captured queries |
| 20 | + |
| 21 | +## Workflow |
| 22 | + |
| 23 | +### Extract Execution Plans |
| 24 | + |
| 25 | +**Default (top 50 plans by duration):** |
| 26 | +```powershell |
| 27 | +.\scripts\export-queries.ps1 -ExportPlans |
| 28 | +``` |
| 29 | + |
| 30 | +**By specific metric:** |
| 31 | +```powershell |
| 32 | +# Top plans by CPU |
| 33 | +.\scripts\export-queries.ps1 -ExportPlans -Top 20 -Metric cpu |
| 34 | +
|
| 35 | +# Top plans by logical reads |
| 36 | +.\scripts\export-queries.ps1 -ExportPlans -Top 30 -Metric reads |
| 37 | +
|
| 38 | +# Top plans by execution count |
| 39 | +.\scripts\export-queries.ps1 -ExportPlans -Top 25 -Metric executions |
| 40 | +``` |
| 41 | + |
| 42 | +**With custom lookback window:** |
| 43 | +```powershell |
| 44 | +# Last 4 hours |
| 45 | +.\scripts\export-queries.ps1 -ExportPlans -LookbackMinutes 240 |
| 46 | +
|
| 47 | +# Last 7 days |
| 48 | +.\scripts\export-queries.ps1 -ExportPlans -LookbackMinutes 10080 |
| 49 | +``` |
| 50 | + |
| 51 | +### Analyze Execution Plans |
| 52 | + |
| 53 | +After extraction, analyze the .sqlplan files: |
| 54 | + |
| 55 | +**Single plan:** |
| 56 | +```powershell |
| 57 | +.\SqlAnalyzer.exe --file 'Analysis\input\QueryStoreTopPlans_qid14_pid1_.sqlplan' |
| 58 | +``` |
| 59 | + |
| 60 | +**All plans:** |
| 61 | +```powershell |
| 62 | +.\scripts\run-analyzer.ps1 |
| 63 | +``` |
| 64 | + |
| 65 | +## Output Format |
| 66 | + |
| 67 | +Execution plans are saved as: |
| 68 | +``` |
| 69 | +QueryStoreTopPlans_qid<query_id>_pid<plan_id>_<row>.sqlplan |
| 70 | +``` |
| 71 | + |
| 72 | +Example: |
| 73 | +``` |
| 74 | +QueryStoreTopPlans_qid14_pid1_001.sqlplan |
| 75 | +QueryStoreTopPlans_qid28_pid7_004.sqlplan |
| 76 | +``` |
| 77 | + |
| 78 | +## What Gets Analyzed |
| 79 | + |
| 80 | +The analyzer detects: |
| 81 | +- **Expensive Sorts**: Operations without supporting indexes |
| 82 | +- **Table Scans**: Full table scans on large tables |
| 83 | +- **Index Scans**: Non-selective index usage |
| 84 | +- **Missing Indexes**: Optimizer suggestions |
| 85 | +- **Implicit Conversions**: Type mismatches preventing index usage |
| 86 | +- **High Cost Operators**: Nested loops, hash joins on large sets |
| 87 | +- **Key Lookups**: RID/Key lookups indicating missing covering indexes |
| 88 | + |
| 89 | +## Common Use Cases |
| 90 | + |
| 91 | +**Find slow queries:** |
| 92 | +```powershell |
| 93 | +# Top 10 slowest by duration |
| 94 | +.\scripts\export-queries.ps1 -ExportPlans -Top 10 -Metric duration |
| 95 | +``` |
| 96 | + |
| 97 | +**Find CPU-intensive queries:** |
| 98 | +```powershell |
| 99 | +# Top 20 by CPU time |
| 100 | +.\scripts\export-queries.ps1 -ExportPlans -Top 20 -Metric cpu |
| 101 | +``` |
| 102 | + |
| 103 | +**Recent performance issues:** |
| 104 | +```powershell |
| 105 | +# Last hour only |
| 106 | +.\scripts\export-queries.ps1 -ExportPlans -LookbackMinutes 60 -Top 10 |
| 107 | +``` |
| 108 | + |
| 109 | +## Next Steps |
| 110 | + |
| 111 | +After analyzing plans: |
| 112 | +1. Review JSON reports in Analysis/output or reports/ folder |
| 113 | +2. Focus on plans with high-cost operations (>50% cost) |
| 114 | +3. Implement missing indexes suggested by optimizer |
| 115 | +4. Consider query rewrites for expensive operations |
| 116 | +5. Use LLM fixes for eligible violations in the SQL text |
| 117 | + |
| 118 | +## Extract SQL Text Instead |
| 119 | + |
| 120 | +To extract SQL text (not plans): |
| 121 | +```powershell |
| 122 | +# This is the default - omit -ExportPlans flag |
| 123 | +.\scripts\export-queries.ps1 -Top 50 -Metric duration |
| 124 | +``` |
| 125 | + |
| 126 | +## Reference |
| 127 | + |
| 128 | +- Script: [scripts/export-queries.ps1](file:///c%3A/Projects/Analyzer/scripts/export-queries.ps1) |
| 129 | +- Query template: [Queries/QueryStoreTop.sql](file:///c%3A/Projects/Analyzer/Queries/QueryStoreTop.sql) |
| 130 | +- Output location: [Analysis/input](file:///c%3A/Projects/Analyzer/Analysis/input) |
| 131 | + |
0 commit comments