1. What Is a SQL Server Execution Plan?
When SQL Server receives a query, it does not simply begin reading tables in the order they appear in the SQL statement.
The SQL Server Query Optimizer evaluates different strategies for executing the query and selects a physical execution plan based on its cost model.
For example:
SELECT
Id,
CustomerId,
OrderDate,
TotalAmount
FROM Orders
WHERE CustomerId = 125;
The optimizer may consider:
- Scanning the table.
- Scanning an index.
- Seeking into a nonclustered index.
- Using a seek followed by lookups.
- Executing parts of the query in parallel.
In simple terms:
An Execution Plan describes the physical operations SQL Server chooses to retrieve, join, filter, sort and return the requested data.
2. Why Execution Plans Matter
Knowing that a query takes twelve seconds is easy. Understanding why it takes twelve seconds is the difficult part.
A slow query may be caused by:
- A missing index.
- An inefficient existing index.
- Too many rows being read.
- Repeated Key Lookups.
- Bad cardinality estimates.
- Expensive sorts.
- Hash or Sort spills to tempdb.
- Implicit data-type conversions.
- Parameter sensitivity.
- An inappropriate join strategy.
The execution plan gives us evidence about which of these scenarios is actually occurring.
3. Estimated vs Actual Execution Plan
Estimated Execution Plan
An Estimated Execution Plan is produced without executing the query. It represents what the optimizer expects to do.
Because the query has not run, the plan does not contain actual runtime row counts, runtime resource usage or runtime warnings.
Actual Execution Plan
An Actual Execution Plan is generated after the query executes. It contains the compiled plan together with runtime information.
This lets us compare estimates with reality:
Estimated Rows : 100
Actual Rows : 254,000
Large differences between estimated and actual row counts can significantly influence join selection, memory grants and index access strategies.
4. How to Read a Graphical Plan
In the graphical plan, data-flow arrows generally point from right to left.
Leaf operators on the right often read rows from tables or indexes, and the data then moves through joins, filters, sorts and other operators toward the result.
Index Seek
↓
Nested Loops
↓
Key Lookup
↓
SELECT
Large plans should be treated as data-flow trees rather than simply lists of icons.
5. Operator Cost Percentages
SQL Server may display relative operator costs such as:
Index Scan 12%
Hash Match 45%
Sort 38%
SELECT 5%
These percentages are not direct measurements of elapsed runtime. They are estimates produced by SQL Server's optimizer cost model.
A 60% operator is not automatically the real bottleneck, and an operator with a small estimated percentage may still be expensive if it executes hundreds of thousands of times.
Treat cost percentages as clues, not verdicts.
6. Index Seek
An Index Seek means SQL Server can navigate an index to locate a row or range of rows.
CREATE INDEX IX_Orders_CustomerId
ON Orders(CustomerId);
For:
SELECT *
FROM Orders
WHERE CustomerId = 125;
SQL Server may be able to perform an Index Seek.
Seek is often efficient for selective queries, but a Seek is not automatically good. A Seek returning hundreds of thousands of rows can still be expensive.
7. Index Scan and Table Scan
A Scan does not automatically indicate a bad plan.
If a query needs most rows:
SELECT *
FROM Orders;
scanning the table may be the most efficient strategy.
A Scan becomes suspicious when the engine reads millions of rows to return only a few.
The useful question is therefore not simply: “Is there a Scan?”
The better question is: “How much data is SQL Server reading compared with how much it returns?”
8. Key Lookup
A nonclustered index may locate rows efficiently but not contain all the columns required by the query. SQL Server may then access the underlying clustered index once for each qualifying row.
This appears as a Key Lookup.
A few lookups are usually harmless. Hundreds of thousands can become very expensive.
A covering index may help:
CREATE INDEX IX_Orders_CustomerId
ON Orders(CustomerId)
INCLUDE
(
OrderDate,
TotalAmount,
Status
);
However, adding every SELECT column to every index increases storage and modification costs.
9. Nested Loops
Nested Loops is often efficient when the outer input contains relatively few rows and the inner input can be searched efficiently using an index.
Outer Row 1 → Search inner index
Outer Row 2 → Search inner index
Outer Row 3 → Search inner index
Problems arise when the optimizer expects a small outer input but receives hundreds of thousands of rows.
Always compare Estimated Rows and Actual Rows when a large Nested Loops operation appears in a slow plan.
10. Hash Match
Hash joins are commonly useful for joining larger unsorted inputs.
SQL Server builds a hash structure from one input and probes it using rows from the other input.
Hash Match itself is not a bad operator.
The problem may occur when the hash structure does not fit inside the granted memory and SQL Server has to use tempdb.
11. Merge Join
Merge Join can be highly efficient when both inputs are already ordered by the join keys.
If the data is not ordered, SQL Server may introduce Sort operators before the Merge Join.
In that situation the cost of sorting can make another join algorithm preferable.
12. Sort Operators
ORDER BY, GROUP BY, window functions and some join strategies may require sorting.
Sorting large rowsets consumes CPU and memory:
SELECT
CustomerId,
OrderDate,
TotalAmount
FROM Orders
WHERE OrderDate >= '2026-01-01'
ORDER BY CustomerId, OrderDate;
If the sort does not fit into its memory grant, SQL Server may spill data into tempdb.
13. What Is a Spill?
Operators such as Sort and Hash Match may request working memory before execution.
If the memory grant is insufficient, intermediate data is written to tempdb.
The Actual Execution Plan may display warnings related to:
- Sort spills
- Hash spills
- Insufficient memory grants
A spill may indicate inaccurate cardinality estimation, outdated statistics, very large rowsets or an inefficient query/index design.
14. Estimated Rows vs Actual Rows
This is one of the most valuable comparisons in execution-plan analysis.
Estimated Rows : 25
Actual Rows : 985,324
If SQL Server expected only 25 rows, it may have chosen a plan optimized for a tiny result set.
When almost a million rows actually arrive, the selected join algorithm, memory grant and access strategy may become inefficient.
Potential causes include:
- Outdated statistics.
- Parameter sensitivity.
- Correlated predicates.
- Complex expressions.
- Functions around filtered columns.
15. Number of Executions
Do not look only at rows per execution.
Actual Rows : 1
Number of Executions : 250,000
An operator returning one row can still be expensive if SQL Server calls it 250,000 times.
This is especially important for Key Lookups beneath Nested Loops joins.
16. Seek Predicate vs Predicate
Seeing Index Seek does not necessarily mean every filter was applied through the index navigation.
Operator properties may contain both:
- Seek Predicates
- Predicates
If the Seek identifies a large number of rows and a residual Predicate discards most of them afterward, SQL Server may still read much more data than expected.
17. SARGable Predicates
Consider:
SELECT *
FROM Orders
WHERE YEAR(OrderDate) = 2026;
Applying a function to the indexed column can make efficient index access more difficult.
A range is usually more index friendly:
SELECT *
FROM Orders
WHERE OrderDate >= '2026-01-01'
AND OrderDate < '2027-01-01';
18. Implicit Conversion
Different data types in predicates or join conditions can cause automatic conversions.
For example, comparing a VARCHAR database column to an NVARCHAR parameter may produce
CONVERT_IMPLICIT in a plan.
Implicit conversions can affect both index usage and cardinality estimates, so they should be investigated when they appear on critical predicates.
19. Missing Index Suggestions
Execution plans may display Missing Index recommendations. These can be useful, but they are not commands that should automatically be executed.
Before creating the suggested index, check:
- Whether a similar index already exists.
- Whether an existing index could be modified.
- How many INCLUDE columns are proposed.
- The write workload of the table.
- How frequently the query runs.
Indexes improve reads but have costs for storage, INSERT, UPDATE and DELETE operations.
20. SET STATISTICS IO and TIME
Execution plans become far more useful when combined with I/O and timing statistics.
SET STATISTICS IO ON;
SET STATISTICS TIME ON;
SELECT
CustomerId,
OrderDate,
TotalAmount
FROM Orders
WHERE CustomerId = 125;
A result may show:
Table 'Orders'.
Scan count 1,
logical reads 18452,
physical reads 0.
Logical reads are especially useful when comparing different versions of the same query.
21. Parallelism
SQL Server may execute expensive operations using multiple workers.
Execution plans may contain operators such as:
- Distribute Streams
- Repartition Streams
- Gather Streams
Parallelism is not inherently bad. Large analytical workloads can benefit enormously from multiple CPUs.
The concern is whether many small, frequently executed queries unnecessarily consume parallel worker resources.
22. Memory Grants
Sort and Hash operations may request memory before execution.
An underestimated grant may produce tempdb spills. An excessively large grant may reserve memory that other concurrent queries need.
Performance tuning therefore has to consider system-wide concurrency, not only how quickly one isolated query runs.
23. Plan Regression
The same query can use different execution plans over time because of:
- Statistics changes.
- Data growth.
- New or removed indexes.
- Compatibility-level changes.
- Parameter sensitivity.
- Plan-cache changes.
Yesterday:
Query → Plan A → 80 ms
Today:
Query → Plan B → 8 seconds
This may represent a plan-choice regression.
24. Query Store
Query Store keeps query plans and runtime statistics over time.
It can help answer:
- Which plans did this query use previously?
- When did performance become worse?
- Which plan used more CPU?
- Which plan produced more logical reads?
- Did the query recently switch plans?
The Regressed Queries view in SQL Server Management Studio is particularly useful for finding queries that recently became slower.
25. Forcing a Plan with Query Store
If Query Store contains a previously stable plan, it may be forced:
EXEC sys.sp_query_store_force_plan
@query_id = 48,
@plan_id = 49;
Plan forcing can quickly stabilize a regression, but it should not automatically replace root-cause analysis.
26. A Practical Reading Order
- Capture the Actual Execution Plan.
- Measure elapsed time and logical reads.
- Compare estimated and actual row counts.
- Find operators processing the most data.
- Determine whether scans are justified.
- Check repeated Key Lookups.
- Inspect Nested Loops execution counts.
- Look for Sort or Hash spills.
- Check implicit conversions.
- Compare Missing Index suggestions with existing indexes.
- Use Query Store to compare historical plans.
27. Practical Example
Consider:
SELECT
o.OrderDate,
o.TotalAmount,
o.Status
FROM Orders o
WHERE o.CustomerId = 125
AND o.OrderDate >= '2026-01-01';
The plan contains:
Index Seek
Estimated Rows : 20
Actual Rows : 145,000
↓
Key Lookup
Number of Executions : 145,000
The presence of an Index Seek may initially make the plan look good. The real problem is the repeated lookup.
An index such as:
CREATE INDEX IX_Orders_Customer_Date
ON Orders(CustomerId, OrderDate)
INCLUDE(TotalAmount, Status);
may allow SQL Server to satisfy the query entirely from the nonclustered index.
The index should still be evaluated against existing indexes and write costs before deployment.
28. Do Not Optimize Icons
The goal of execution-plan tuning is not to eliminate every Scan, Hash Match, Sort or parallel operator.
The goal is to reduce:
- Unnecessary reads.
- CPU consumption.
- Excessive memory use.
- tempdb pressure.
- Elapsed execution time.
- Performance instability under concurrency.
29. Execution Plan Checklist
Slow Query
↓
Actual Plan
↓
Estimated vs Actual Rows
↓
Large unnecessary scans?
↓
Repeated Key Lookups?
↓
High execution counts?
↓
Sort / Hash spills?
↓
Implicit conversions?
↓
Statistics accurate?
↓
Indexes appropriate?
↓
Logical reads?
↓
Historical plan regression?
30. Conclusion
Reading SQL Server Execution Plans is one of the most valuable skills in database performance tuning.
The skill is not simply recognizing Index Seek and Index Scan icons. Effective plan analysis requires understanding:
- What SQL Server estimated.
- What actually happened.
- How many rows moved through each operator.
- How often each operator executed.
- How much data was read.
- Whether memory and tempdb were involved.
- Whether the plan changed over time.
The first question should not be:
“Which operator has the highest cost percentage?”
The better question is:
“How much data did SQL Server actually process, and how accurately did the optimizer predict that workload?”
Once this mindset becomes familiar, an Execution Plan stops looking like a confusing diagram and becomes one of the most powerful diagnostic tools available in SQL Server.
0 Comments