SQL Server execution plan analysis
A SQL Server execution plan is a tree of operators, and internally it is XML (Showplan XML). The estimated execution plan shows the plan the optimizer chose, with estimated row counts and costs, without running the statement. The actual execution plan runs the statement and then attaches the actual row count, number of executions, logical reads, and elapsed time to each operator. Tuning means finding, in the actual execution plan, the operators that did a lot of work and the operators whose estimates were far off.
Summary
| Item | Description |
|---|---|
| Estimated plan | SET SHOWPLAN_XML ON (does not run the statement). Ctrl+L in SSMS |
| Actual plan | Run after SET STATISTICS XML ON. Ctrl+M in SSMS |
| During execution | Live Query Statistics (SSMS), sys.dm_exec_query_profiles |
| Plan cache | sys.dm_exec_query_stats + sys.dm_exec_query_plan(plan_handle). Execution statistics are cumulative, and the plan has no actual values |
| History | Query Store (sys.query_store_*). From 2022, on by default for new databases. Plan forcing (sp_query_store_force_plan) |
| Text statistics | SET STATISTICS IO ON (logical, physical, and read-ahead reads per table), SET STATISTICS TIME ON (compile and execution CPU and elapsed time) |
| Sandbox | Shows the actual plan from SET STATISTICS XML ON as a table. Buffers = the operator's ActualLogicalReads + the sum of its children |
Reading direction
This is the actual plan from the sandbox for a query of the last 7 days of orders for VIP customers (the table in the sandbox Plan tab is indented from top to bottom, like Oracle).
| Operator | Executions | Estimated rows (per execution) | Actual rows (total) | Buffers |
|---|---|---|---|---|
| Nested Loops (Inner Join) | 1 | 140 | 144 | 3,605 |
├ Clustered Index Scan customers_pk | 1 | 1,000 | 1,000 | 601 |
| └ Nested Loops (Inner Join) | 1,000 | 1 | 144 | 3,004 |
| ├ Compute Scalar ← Constant Scan | 1,000 | 1 | 1,000 | 0 |
└ Index Seek orders_x1 | 1,000 | 1 | 144 | 3,004 |
- In the SSMS diagram, data flows from right to left. The leftmost node (
SELECT) is the result, and the operator at the far right produces rows first. - The upper input of a join operator is the outer input and the lower input is the inner input. The inner side of an NL join runs once per outer row, so its number of executions equals the number of outer rows.
Constant Scan+Compute Scalaris the part that produces the range values for the Seek. It has no cost.- In the plan above,
orders_x1(cust_id, ord_dt)also containsord_id(the clustered key), so there is noKey Lookup.
Operator properties
When you click an operator in SSMS, the properties window shows the values below. The sandbox shows the same values as table columns.
| Property | Meaning | What to look for |
|---|---|---|
| Number of Executions | Number of executions (Oracle's Starts) | Large on the NL inner side and in Key Lookup |
| Estimated Number of Rows | Estimated rows per single execution (shown as "Estimated Number of Rows Per Execution" from SSMS 18) | When comparing, multiply by the number of executions |
| Actual Number of Rows | Sum over all executions | Compare with estimated × number of executions |
| Estimated / Actual Rows to be Read | Number of rows read (2016 SP1+) | If much larger than the actual rows, the Predicate discards a lot |
| Actual Logical Reads | Pages the operator read | The operator's own share of the sandbox Buffers |
| Seek Predicates | Conditions that set the index seek range (Oracle's access) | |
| Predicate | Conditions filtered after reading (Oracle's filter) | The gap between Rows to be Read and Actual Rows is the amount discarded |
| Probe Residual / Hash Keys | The hash join's keys and the remaining non-key conditions | |
| Ordered / Scan Direction | Whether the index was read in order / FORWARD or BACKWARD | Skipping the sort for Top-N |
Statement-level properties and warnings
The SELECT operator at the top of the plan (QueryPlan and StmtSimple in Showplan XML) holds information for the whole statement.
| Property | Meaning |
|---|---|
CardinalityEstimationModelVersion | Cardinality estimator version. 70 means the old estimator, 120 or above means the new estimator (Statistics) |
StatementOptmLevel / StatementOptmEarlyAbortReason | TRIVIAL means the plan was chosen without cost comparison. GoodEnoughPlanFound or TimeOut means the search stopped partway |
MemoryGrantInfo (GrantedMemory, MaxUsedMemory) | Memory granted for sorts and hashes, and the memory actually used. A large gap indicates an estimation error |
QueryTimeStats (CpuTime, ElapsedTime) | The statement's CPU and elapsed time (ms) |
OptimizerStatsUsage (2017+) | The statistics objects used to build this plan and their update times |
ParameterList | The value sniffed at compile time (ParameterCompiledValue) and the value at execution time (ParameterRuntimeValue). If they differ, suspect parameter sniffing |
Warnings appear as the yellow triangle on an operator icon or as Warnings elements. The sandbox shows them at the top of the Raw tab.
| Warning | Meaning |
|---|---|
PlanAffectingConvert | An implicit conversion blocked a Seek or blurred the estimate (Scenario 21) |
SpillToTempDb (Sort, Hash) | Exceeded the granted memory and spilled to tempdb |
MemoryGrantWarning | The granted memory is excessively large or small |
NoJoinPredicate | A join with no join condition (Cartesian product) |
ColumnsWithNoStatistics | The estimate was made on a column that has no statistics |
SET STATISTICS IO, TIME
SET STATISTICS IO, TIME ON;
SELECT …;
-- Table 'orders'. Scan count 1000, logical reads 3004, physical reads 0, read-ahead reads 0, …
-- Table 'customers'. Scan count 1, logical reads 601, …
-- SQL Server Execution Times: CPU time = 5 ms, elapsed time = 5 ms.
| Item | Meaning |
|---|---|
| Scan count | Number of times that table (index) was seeked or scanned. On the NL inner side, the number of outer rows |
| logical reads | Pages read from the buffer pool |
| physical reads / read-ahead reads | Pages read from disk / pages read ahead |
Worktable | Reads of the work table that a spool, sort, or hash creates in tempdb |
The order to check in practice
- Find the operator with the most logical reads. Even within the same table, look at places like
Key Lookupwhere the cost grows to number of executions × 3 pages. - Find the rightmost operator (the one that runs first) where estimated rows × executions and actual rows diverge by 10 times or more. Look at the statistics it used (
OptimizerStatsUsage). - Use the gap between Rows to be Read and Actual Rows to see how much the Predicate discarded. Look for a way to move it into the Seek Predicate (index column order, shape of the condition).
- Check the memory grant,
SpillToTempDb, and conversion warnings. - If the statement has parameters, compare the compile value with the execution value. Run it again with several representative values.
Comparison with other DBMSs
| Item | SQL Server | Oracle | PostgreSQL |
|---|---|---|---|
| Actual plan | SET STATISTICS XML ON | DISPLAY_CURSOR … ALLSTATS LAST | EXPLAIN (ANALYZE, BUFFERS) |
| Number of executions | Number of Executions | Starts | loops |
| Estimated rows | Per execution | Per execution | Per execution |
| Actual rows | Total | Total | Average per execution |
| Conditions | Seek Predicates / Predicate | access / filter | Index Cond / Filter |
| Plan history and pinning | Query Store | AWR, SQL Plan Baseline | None (pg_stat_statements has no plans) |
See also: Oracle execution plan analysis, PostgreSQL execution plan analysis, Join methods.
NOTE The SSMS display and the Showplan XML properties grow with each version. The plans in this document are from SQL Server 2022 (compatibility level 160) in the sandbox.