Blog
AdvancedReferenceSQL Server Engineering

Reading an execution plan

How to read an actual execution plan top-down - estimated versus actual rows, the operator that owns the cost, key lookups, spills, and the five mismatches that tell you the estimate, not the engine, is at fault.

Applies to
SQL Server2016-2025 (13.x-17.x)
matureUpdated Reviewed
advancedAssumes production experience with the basics.

What you will be able to do

  • Read an actual plan right-to-left and identify which operator owns the cost.
  • Use estimated-versus-actual row counts to split a bad plan into an estimate problem or a design problem.
  • Recognise the operators that signal trouble: key lookups, sorts and hash spills to tempdb, implicit conversions, scans on big tables.
  • Decide whether a plan fix belongs in the query, the index, the statistics, or a hint.

The execution plan is the only ground truth about what the engine actually did. Reading one well is not memorising the icons - it is knowing which three numbers to check first, and which operator shapes mean “the estimate lied” versus “the index is missing.”

The analogy is a mechanic’s job sheet versus the invoice. The job sheet is the estimated plan - what the garage expected the repair to take, part by part; the invoice is the actual plan, with the hours each step really consumed. Reading the invoice line by line, the expensive entry is obvious, and any line where the quoted hours and the real hours diverge wildly names where the original assessment was wrong. A spill warning is the note saying the garage had to send out for a part it thought was on the shelf.

The three numbers first

Get an actual plan (estimated plans omit runtime truth) and read right to left, data flowing toward the final result. Before the operator shapes, check:

  1. Estimated rows vs actual rows on the thick edges. A big divergence upstream that survives to the bottom is an estimate problem - the rest of the plan is a faithful execution of a wrong assumption.
  2. Percentage cost on the heaviest operator. The single most expensive operator is where tuning pays; the plan tells you which one it is.
  3. Row counts on each edge. Where the row count collapses is where the predicate filtered; where it stays fat into a join or a sort is where the work is.

Triage a plan in four checks

  1. Est vs actual rows

    A divergence or a match

    Divergence upstream is an estimate problem - go to statistics, CE, or parameter sensitivity

  2. Heaviest operator

    The operator owning the heaviest cost share

    Tune here first - the rest of the plan is secondary

  3. Lookup / spill / conversion

    A warning shape, if present

    Key lookup = non-covering index; spill = memory grant too small; implicit conversion = type mismatch killing a seek

  4. Decision

    Index, query, statistics, or hint

    Fix the cause the plan exposed - not the most convenient knob

Look for the divergence between what the optimiser expected and what happened - the gap is the diagnosis.

The operator shapes that mean something

  • Key Lookup / RID Lookup - the index found the row but not the columns; every row makes a second trip. Fix by covering (add to key or INCLUDE) or by accepting the lookup count is small.
  • Sort or Hash Match spilling to tempdb - a memory grant too small for the actual rows. The spill warning in the plan names it; the fix is a better estimate (so the grant is sized right), memory grant feedback on 2022+, or reducing rows before the operator.
  • Implicit conversion warning - a column compared to a mismatched type; the conversion wraps the column and kills the seek, turning it into a scan of the whole index. Match the parameter or column type.
  • A scan where you expected a seek - either no usable index (the predicate is not leftmost in the key, or a function wraps the column) or the optimiser rightly chose the scan because the predicate is not selective. The actual row count tells you which.
  • Nested loops on a huge outer input - fine when the inner side is a seek and the outer count is small; catastrophic when the outer estimate was low and millions of inner seeks fire. Usually an estimate problem, not a join problem.

Estimated versus actual - the fork in the road

Divergence Where you see it The fix lives in
Estimate far below actual, then a lookup/sort explodes Bad statistics, parameter sniffing, a function or variable the CE cannot model Statistics update, query rewrite, OPTIMIZE/RECOMPILE, PSP on 2022
Estimate right, cost still enormous The work is genuinely that big - wrong index shape, missing covering index, unindexable query shape Index design, query rewrite, or accept and measure the real cost

That table is the whole discipline of this lesson: if the estimate is wrong, fix the estimate; if the estimate is right, fix the design. Treating a statistics problem with a new index, or an index problem with a hint, is tuning the wrong layer - and it is why so much “tuning” makes things worse.

When to hint

Hints - FORCESEEK, join hints, OPTIMIZE, Query Store hints - are for the last case in the table: the estimate is right, the design is right, and the optimiser’s cost model still picks a plan measurably worse than one you can name. A hint is a promise you make to every future version of the engine, so it is the last tool, not the first - and on 2022+ it can often be enforced through Query Store without a code change.

Guided practice

A beginner exercise, worked on a scratch database. The scenario: manufacture the plan shapes the lesson names, one at a time, so you recognise them later on a plan that matters. You need an actual execution plan - “Include Actual Execution Plan” in the tool, or SET STATISTICS XML - because an estimated plan omits the runtime truth.

  1. Build a table with a deliberately non-covering index. Create a scratch table (for example orders with an index on customer_id alone) and load a few thousand rows. Check: the index exists and covers one column only. If you indexed every column the query wants you will never see a lookup - the exercise depends on the index not carrying everything.
  2. Produce a key lookup. Run a SELECT with a selective predicate on the indexed column that also returns columns the index does not carry, and read the actual plan. Check: a seek feeds a Key Lookup or RID Lookup - the index found the rows but not the columns, so each row made a second trip. If you got a scan instead, revisit “a scan where you expected a seek”: the predicate was not selective enough, so tighten it.
  3. Compare estimated and actual rows. Hover the thick edges and read estimated versus actual row counts through the plan. Check: on a freshly loaded table they should roughly match - a large divergence upstream that survives to the bottom would mean the plan faithfully executed a wrong assumption. If you only looked at operator percentages, go back to “the three numbers first”.
  4. Kill the seek with a conversion. Re-run the same query comparing the column to a mismatched type (for example an NVARCHAR parameter against a VARCHAR column) and capture the plan again. Check: an implicit conversion warning appears and the seek collapses into a scan - the conversion wraps the column, so the index can no longer be searched. Match the types and confirm the seek returns; if the warning persists, revisit the implicit-conversion bullet.
  5. File each shape under its fix. For every shape you produced - lookup, divergence, conversion - say in one sentence whether the fix lives in the index, the query, the statistics, or a hint. Check: the lookup belongs to index design (cover it, or accept the small count), the conversion to the query (type matching), a divergence to statistics or the CE - and a hint fits none of these. If you reached for a hint anywhere, revisit “when to hint” - it is the last tool, for when estimate and design are both already right.

Check your understanding

Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.

  1. Why does the lesson insist on an actual execution plan rather than an estimated one? Estimated plans omit the runtime truth - the actual row counts and warnings that carry the diagnosis. If you thought the estimated plan was enough, revisit “the three numbers first”: every check there needs the actual numbers.
  2. Which single comparison splits a bad plan into an estimate problem and a design problem? Estimated rows versus actual rows on the edges. A divergence is an estimate problem - fix statistics, the CE, or parameter sensitivity; a match with enormous cost means the work really is that big, so fix the index shape or the query. If you answered “look at the costliest operator first”, that is the second check, not the fork - revisit the fork table.
  3. What does a Key Lookup operator tell you, and what are the two honest responses? The index found the row but not the columns, so every row pays a second trip. Cover the query - add the columns to the key or INCLUDE - or accept the lookup because the count is small. If you said “remove the lookup”, revisit the operator list - a lookup on a small row count is not automatically a problem.
  4. A sort spills to tempdb. What was sized wrong, and what are the routes out? The memory grant - it was sized for the estimated rows and the actual rows did not fit. Fix the estimate so the grant is sized right, use memory grant feedback on 2022+, or reduce the rows before the operator. If you blamed tempdb, revisit the spill bullet - tempdb is where the symptom lands, not the cause.
  5. When is a hint the correct fix? Only in the last cell of the fork: the estimate is right, the design is right, and the cost model still picks a plan measurably worse than one you can name - and on 2022+ it can often be enforced through Query Store without a code change. A hint is a promise to every future version of the engine. If you hinted a plan whose estimates were wrong, revisit “when to hint” - you fixed the wrong layer.

Section close - the stage project and the debrief

Project: diagnose a slow scratch workload end to end. In a scratch database, build a small workload that is slow on purpose: a table queried through a non-covering index, a second query with a deliberate type mismatch, and a writer loop committing row by row. Capture the actual plan and the wait types for each, then write one page naming, for every query, whether the fault is the estimate, the design or the storage path - and what you would change. Walkthrough: run each query cold and warm first so you know which system you measured; read each plan right to left for the three numbers; match every wait to its owner - PAGEIOLATCH to storage, WRITELOG to the log, blocking to a lock. Where people get stuck: they fix the wrong layer - indexing a statistics problem or hinting a missing index - and they benchmark on a warm cache only, so the physical reads never show. Re-read the fork table whenever the fix feels too easy.

Debrief. Having finished this section you can walk a statement from text to a cached plan, name which component owns a wait type, choose an isolation level deliberately and say what RCSI charges, and read an actual plan for the estimate-versus-design fork. The gaps that usually remain even when it feels understood: you can name WRITELOG but not yet say whether yours is too high, and you can read one plan but not yet judge a workload’s latency shape - both need measurement before they mean anything. Next session: the SQL Server Performance Internals section, starting with “Latency, throughput, and why the average lies” - the lesson that turns these symptoms into numbers.

Where SQL Server sits

How this component relates to the rest of the stack. Items without a link are not covered here yet.

Deployed with

Commonly run alongside

  • Windows Failover Clustering

Alternatives

Solves a similar problem differently