Back to blog

// article

SQL Server execution plan: a map to a slow query

An execution plan shows how SQL Server actually runs a query. We will learn how to read the basic operators, spot expensive parts, and verify whether indexes are really helping.

12 min read#SQLServer#ExecutionPlan#QueryTuning#Debuggingupdated
Article contents17

A SQL query is like a route request.

You tell SQL Server: “Bring me this customer’s orders from last year, sort them by date, and include the customer name.”

Sounds simple. But SQL Server usually has several ways to get the result. It can start with orders. It can start with customers. It can use an index. It can scan a whole table. It can sort data. It can join tables in several different ways.

An execution plan is the map showing which route SQL Server chose.

Without an execution plan, tuning performance is a bit like standing in the middle of a forest, looking at a compass, and saying: “I feel like the database is somewhere in that direction.”

With an execution plan, you still have to think. But at least you can see the tracks.

Why should you care about execution plans?

#

In the previous articles, we talked about classic indexes and columnstore indexes.

But creating an index is one thing. Verifying that SQL Server actually uses it is another.

You can have a perfect index that the query ignores.

You can have a query that looks innocent but performs a million lookups inside.

You can have a table with more indexes than reasonable life choices, and the slowest report may still run through a scan.

An execution plan helps answer questions like:

  • Which tables does SQL Server read?
  • Is it using an index seek or a scan?
  • How many rows did it expect, and how many did it actually get?
  • Where is data sorted?
  • Where do lookups happen?
  • Which part of the plan looks suspicious?

It does not automatically tell you what to do. But it tells you where to start.

Estimated vs. actual execution plan

#

In SQL Server, you will most often work with two kinds of plans.

An estimated execution plan is generated without running the query. It is an estimate. SQL Server looks at metadata, statistics, indexes, and says: “If I ran this query, I would probably do it this way.”

An actual execution plan is available after the query runs. It contains the same basic structure, but adds runtime information: how many rows operators actually processed, warnings, and other details from the real execution.

In practice:

sql
-- Estimated plan in SSMS:
-- Ctrl + L

-- Actual plan in SSMS:
-- Ctrl + M, then run the query

The estimated plan is useful when you do not want to run the query. For example, because it would take too long or modify data.

The actual plan is more useful for performance tuning because it shows what really happened.

That is the difference between “the map says this route takes ten minutes” and “I went that way and ended up waist-deep in mud”.

Example problem

#

Let’s use a simple query:

sql
SELECT
    o.Id,
    o.OrderDate,
    o.TotalPrice,
    c.Name AS CustomerName
FROM dbo.Orders AS o
JOIN dbo.Customers AS c
    ON c.Id = o.CustomerId
WHERE o.CustomerId = 42
  AND o.OrderDate >= '2024-01-01'
ORDER BY o.OrderDate DESC;

At first glance, nothing dramatic.

We want one customer’s orders for a period of time and the customer name.

But if the Orders table has tens of millions of rows and no suitable index, SQL Server can end up doing very expensive reads.

The execution plan shows whether it took the right path or grabbed a shovel and started moving the whole warehouse.

Read the plan from right to left

#

A graphical execution plan in SSMS is typically read from right to left.

On the right, you find operators that retrieve data. As you move left, the plan gets closer to the final result returned by the query.

Simplified:

text
Index Seek / Scan -> Join -> Sort -> Select

You do not need to understand everything immediately. At the beginning, ask:

  • Where is data read from?
  • How many rows flow between operators?
  • Where are tables joined?
  • Where is data sorted?
  • Where is the difference between estimate and reality large?

An execution plan is not decoration. It is a detective map.

You are not looking for the prettiest icon. You are looking for the suspicious place.

Index Seek

#

Index Seek is often a good sign.

It means SQL Server was able to use an index to find a specific value or range of values.

For example, this index:

sql
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate);

can help this query:

sql
SELECT
    Id,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE CustomerId = 42
  AND OrderDate >= '2024-01-01';

SQL Server does not have to read the whole table. It can find customer 42 and then the date range starting in 2024.

That is like walking into a warehouse and knowing: hall B, shelf 17, row 3.

But be careful: Index Seek does not automatically mean a fast query.

A seek can find a million rows. A seek can be followed by an expensive lookup. A seek can be only the beginning of a larger problem.

It is a good sign, not an automatic victory.

Index Scan and Table Scan

#

Index Scan means SQL Server reads a larger part of an index, often the whole index.

Table Scan means it reads the table.

That sounds bad. And sometimes it is.

But a scan is not always a mistake.

If a query returns most of a table, a scan can be completely reasonable. Why jump through an index and perform thousands of random reads when you need almost everything anyway?

The problem is a scan where you expected only a small number of rows.

For example:

sql
SELECT
    Id,
    CustomerId,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE CustomerId = 42;

If customer 42 has a few hundred orders, but SQL Server scans a table with millions of rows, something is off.

Possible causes:

  • missing index,
  • wrong column order in an index,
  • function applied to a column,
  • outdated statistics,
  • query returns too many columns.

A scan is not a verdict. It is a clue.

Key Lookup

#

Key Lookup is one of the most common operators you will see when tuning indexes.

It usually appears when SQL Server uses a nonclustered index but still needs columns that are not included in that index.

Example:

sql
CREATE INDEX IX_Orders_CustomerId
ON dbo.Orders (CustomerId);

Query:

sql
SELECT
    OrderDate,
    TotalPrice,
    Status
FROM dbo.Orders
WHERE CustomerId = 42;

The index contains CustomerId, but the query returns OrderDate, TotalPrice, and Status.

SQL Server can find rows through the index and then go back to the table for each matching row to retrieve missing columns.

That is a lookup.

If only a few rows are found, this is fine.

If many rows are found, lookup can become an expensive round trip.

One possible solution is a covering index:

sql
CREATE INDEX IX_Orders_CustomerId
ON dbo.Orders (CustomerId)
INCLUDE (OrderDate, TotalPrice, Status);

Now SQL Server has everything the query needs directly in the index.

But be careful. INCLUDE is not a bottomless magic bag. Every added column makes the index larger.

Sort

#

Sort means SQL Server had to sort data.

That can be cheap. But for a large amount of data, it can be expensive.

Usually because of:

sql
ORDER BY o.OrderDate DESC;

If an index already provides data in the right order, SQL Server may avoid the sort.

For example:

sql
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate);

This index can help this query:

sql
SELECT
    Id,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE CustomerId = 42
ORDER BY OrderDate DESC;

SQL Server can find the customer’s orders and already read them in date order. The descending sort is not a problem: SQL Server can read the index backwards. DESC in an index definition only matters when a query sorts by several columns in different directions, for example ORDER BY CustomerId, OrderDate DESC.

A sort in the plan is not automatically a problem. But if it sorts millions of rows, it is worth asking whether an index or query change could help.

Nested Loops

#

Nested Loops is a way to join tables.

Think of it as: for every row from the left side, find matching rows on the right side.

This can be very fast if the left side is small and the right side has a good index.

For example:

text
Find 10 orders -> for each one quickly find the customer by primary key

That is fine.

But if the left side contains a million rows and every row triggers another lookup, nested loops can turn into a small performance fire.

Again: the operator itself is not good or bad. It depends on row counts and available indexes.

Hash Match

#

Hash Match is another way to join or aggregate data.

SQL Server builds a hash structure so it can efficiently join larger data sets or perform aggregations.

You will often see it in larger joins or GROUP BY queries.

sql
SELECT
    CustomerId,
    SUM(TotalPrice) AS Revenue
FROM dbo.Orders
GROUP BY CustomerId;

Hash Match is not automatically bad.

For large data, it can be the right choice.

It becomes suspicious when it processes far more data than expected or when the plan shows memory-related warnings.

Estimated rows vs. actual rows

#

This is one of the most important parts of the plan.

During compilation, SQL Server estimates how many rows will flow through each operator.

Then the query runs and the actual plan shows how many rows really flowed there.

If SQL Server expects 10 rows and gets 12, the world is fine.

If it expects 10 rows and gets 2,000,000, the plan may be completely wrong.

Example problem:

text
Estimated rows: 1
Actual rows:    850000

That is like ordering a taxi for one person and then an entire football stadium shows up in front of the hotel.

Bad estimates can lead to the wrong join type, too small a memory grant, poor operation order, or an unsuitable index choice.

Common causes:

  • outdated statistics,
  • uneven data distribution,
  • parameter sniffing,
  • complex predicates,
  • functions applied to columns.

This leads naturally to the next big topic: statistics.

Operator cost is not everything

#

In a graphical plan, you will see cost percentages.

For example:

text
Sort 68%
Index Seek 12%
Key Lookup 20%

It is tempting to say: “Sort has 68%, let’s fix the sort.”

Sometimes that is correct.

But execution plan cost is an optimizer estimate, not an absolute truth carved into stone.

Sometimes an operator is expensive because too much data flowed into it from an earlier part of the plan.

In other words: Sort can be the victim, not the culprit.

So do not ask only:

Which operator has the highest percentage?

Ask:

Why is so much data flowing into that operator?

That is the difference between applying a bandage and fixing the problem.

Missing index recommendation

#

SQL Server sometimes shows a missing index recommendation in the plan.

That is useful.

But it is not an order.

The optimizer looks at one query. It does not understand the full application context, write frequency, similar existing indexes, or long-term maintenance.

A recommendation may look like this:

sql
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate)
INCLUDE (TotalPrice);

Before creating it, ask:

  • Do we already have a similar index?
  • Will this help multiple queries or only one?
  • Will it slow down critical writes?
  • Can we adjust an existing index instead of creating another one?
  • How much space will the index use?

A missing index recommendation is a good start of a conversation. Not a finished solution.

Practical checklist

#

When you open an actual execution plan, go through it like this:

Question What to look for
Is SQL Server reading too much data? Scan on a large table, high Number of Rows Read
Is it using the right index? Index Seek vs. Index Scan, index name in the operator
Are estimates realistic? Large difference between estimated and actual rows
Does the plan do many lookups? Key Lookup with high execution count
Is a large amount of data sorted? Sort over many rows
Is a useful index missing? Missing index recommendation, scan with filter
Is the query itself the problem? Functions on columns, SELECT *, non-sargable predicates

You do not need to understand every detail immediately.

Just learn to recognize signals that say: “SQL Server worked harder than it needed to.”

Small tuning example

#

We have this query:

sql
SELECT
    Id,
    CustomerId,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE YEAR(OrderDate) = 2024
  AND CustomerId = 42
ORDER BY OrderDate DESC;

The plan shows:

  • Clustered Index Scan,
  • many rows read (Number of Rows Read),
  • Sort,
  • no useful seek.

The main problem is the missing index. The table has no index that starts with CustomerId, so SQL Server has no way to find the orders of customer 42 other than reading the whole table.

The second problem is the function applied to OrderDate. It would hurt even with an index: SQL Server could not seek a date range, it could only check the year on every order of the customer.

Rewrite the query:

sql
SELECT
    Id,
    CustomerId,
    OrderDate,
    TotalPrice
FROM dbo.Orders
WHERE OrderDate >= '2024-01-01'
  AND OrderDate < '2025-01-01'
  AND CustomerId = 42
ORDER BY OrderDate DESC;

Then add an index:

sql
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate)
INCLUDE (TotalPrice);

The new plan may show:

  • Index Seek,
  • fewer rows read,
  • no separate Sort,
  • no expensive lookup.

That is the moment when an execution plan stops being a colorful picture and becomes a working tool.

What to remember

#

An execution plan is not the answer to everything.

It is a map.

And a good map does not tell you why you chose the wrong direction. It shows you where you went.

When tuning a slow query, start with the actual execution plan and look for:

  • large scans,
  • expensive lookups,
  • unnecessary sorts,
  • bad row estimates,
  • suspicious joins,
  • warnings,
  • the difference between what you expected and what SQL Server actually did.

And most importantly: do not fix the plan based on one icon.

Ask why that icon is there.

Conclusion

#

Indexes are a map for finding data.

An execution plan is a map of how SQL Server actually used that map.

When a query drags, the execution plan often shows whether SQL Server is using the right shelf or walking through the whole warehouse with a flashlight again.

In the next article, I would go one level deeper: statistics.

Because the optimizer does not invent plans randomly. It builds them from estimates.

And when the estimates are wrong, even a smart SQL Server can choose a path through the swamp.