Overview

A SQL Server execution plan is the operator tree the engine runs. You can request the estimated plan (no execution) or the actual plan (with real row counts, memory grants and warnings). General concepts are in query-planning.

Core concepts

How to get plans:

NeedHow
Estimated, textSET SHOWPLAN_TEXT ON (the statement is not run)
Estimated, XML / graphicalSET SHOWPLAN_XML ON, or Display Estimated Execution Plan in SSMS/Azure Data Studio
Actual planSET STATISTICS XML ON, or Include Actual Execution Plan
I/O and timeSET STATISTICS IO, TIME ON
Plan of a cached querysys.dm_exec_query_plan(plan_handle); history in sqlserver-query-store

Reading rules:

  • Plans are drawn right to left; data flows from the right (leaf operators) to the left. In text plans, the deepest indentation runs first.
  • Compare Estimated and Actual number of rows on each operator; large gaps usually mean bad statistics or a non-sargable predicate (sqlserver-statistics-cardinality).
  • Operators: Seek (uses an index to navigate) vs Scan (reads all, or a large range); Key/RID Lookup; joins Nested Loops, Hash Match, Merge Join; Sort; Stream/Hash Aggregate; Parallelism (Gather/Repartition/Distribute Streams).
  • Warnings to act on: implicit conversion, spill to tempdb in Sort or Hash (memory grant too small), missing statistics, no join predicate.
  • Parallel plans show Scan count = DOP + 1 in STATISTICS IO (here 17 with MAXDOP 16).

Worked example

Two plans from the test table, as SHOWPLAN_TEXT:

-- WHERE CustomerId = 7 (about 49 rows)
Nested Loops(Inner Join, ... WITH UNORDERED PREFETCH)
  |-- Index Seek(IX_Orders_Cust, SEEK: CustomerId=(7))
  |-- Clustered Index Seek(PK_Orders ... LOOKUP)
 
-- one month of data, GROUP BY Status
Stream Aggregate(GROUP BY Status)
  |-- Sort(ORDER BY Status)
     |-- Parallelism(Gather Streams)
        |-- Hash Match(Partial Aggregate, HASH: Status)
           |-- Clustered Index Scan(WHERE: CreatedAt >= ... AND CreatedAt < ...)

The second plan scans the whole clustered index because there is no index on CreatedAt; the predicate is applied during the scan (shown as WHERE: inside the scan, not SEEK:). An index on (CreatedAt) INCLUDE (Status, Amount) would turn it into a range seek.

Pitfalls and production notes

  • Estimated cost percentages in a plan are estimates even in the actual plan; judge by actual rows, reads and time.
  • A plan that is fast in SSMS can be slow from the application because of different SET options or sniffed parameters (sqlserver-parameter-sniffing).
  • Do not force plans or add hints before understanding why the optimizer chose its plan; Query Store plan forcing is the safer tool.
  • Adaptive joins, memory-grant feedback and interleaved execution (intelligent query processing) change plan behaviour at compatibility level 140 and above (sqlserver-upgrades-compat).

Practice questions

  1. An Index Seek reads 10 million rows. How is that possible?
  2. What does a Sort operator with a warning tell you, and what are two fixes?
  3. Why does STATISTICS IO report scan count 17 on a 16-CPU server?

References

  • Microsoft Learn, Execution plans and Showplan logical and physical operators reference

query-planning · sqlserver-indexes · sqlserver-statistics-cardinality · sqlserver-query-store · pg-explain-analyze