---
title: "Key Lookups and the Tipping Point"
description: "A nonclustered index that helps at a hundred rows can hurt at a hundred thousand. The reason is what happens after the index finds each row."
url: https://sqlpress.com/blog/sql-server-key-lookups/
category: SQL Server
tags: ["indexing", "query-optimization", "execution-plans"]
author: "Elias Rowe"
published: 2026-11-29T00:00:00.000Z
measured: false
---

# Key Lookups and the Tipping Point

## Key takeaways

- A nonclustered index that does not contain every column the query needs must fetch the rest from the table, once per matching row.
- Those lookups are random reads, so their total cost grows linearly with the row count while a scan's cost stays flat.
- Past a threshold the optimizer abandons the index and scans, which is correct — the fix is to remove the lookup, not to force the index.

## Test environment

- System: SQL Server 2022
- System: MySQL 8.4
- System: PostgreSQL 18
- Workload: Illustrative (examples verified, nothing timed)
- Notes: SQL Server is the subject, and the plan fragments show the Key Lookup and Nested Loops operators as it names them. InnoDB and PostgreSQL appear only as comparisons.

A nonclustered index stores its key columns and a pointer to the rest of the row.
When a query needs columns the index does not hold, the engine follows that pointer
and reads the row from the clustered index. That operation appears in the plan as a
Key Lookup, and it runs once per matching row.

## Why does a Key Lookup get expensive as rows grow?

An index seek that matches ten rows performs ten lookups. Each is a small B-tree
descent into the clustered index, landing on an arbitrary page. Ten random page
accesses is nothing.

The same seek matching two hundred thousand rows performs two hundred thousand
descents. Now compare that against the alternative: scanning the clustered index
reads every page once, in physical order, and the operating system and storage layer
both handle that well.

There is a crossover. Below it the index wins by a large margin; above it the scan
wins by a large margin. The optimizer estimates the row count and picks a side, and
when the estimate is right it picks correctly.

That is the important framing. A plan that abandons your index is usually not the
optimizer being wrong. It is the optimizer telling you the index does not cover the
query.

## Reading it in the plan

```text {2-3}
|--Nested Loops (Inner Join)
     |--Index Seek (NonClustered) [orders].[IX_orders_status]
     |--Key Lookup (Clustered) [orders].[PK_orders]
```

The Key Lookup under a Nested Loops join is the shape. The number of executions on
the lookup operator is the number of rows the seek produced — that is the count
that matters, not the number of rows returned to the client, which may be far
smaller if a filter is applied above.

The other detail worth reading is the output list on the Key Lookup. It names the
columns the index was missing, which is precisely the list of candidates for
`INCLUDE`.

## How is a Key Lookup removed?

Adding those columns as
[included columns rather than key columns](/blog/covering-indexes-include-versus-key/)
removes the lookup: the index now answers the query on its own, and the plan becomes
a single seek.

```sql title="cover-it.sql"
CREATE NONCLUSTERED INDEX IX_orders_status
  ON dbo.orders (status)
  INCLUDE (customer_id, total_amount, created_at)
  WITH (DROP_EXISTING = ON);
```

The cost is real: the index is wider, so it holds fewer rows per page, occupies more
memory, and every update to any included column now writes to this index as well.
Covering a query returning three columns is usually a good trade. Covering one that
returns fifteen is building a second copy of the table.

When the list is long, the better question is whether the query needs all of them.
An ORM selecting every column by default is the most common reason a Key Lookup
cannot be eliminated cheaply.

## What not to do

Forcing the index with a hint when the optimizer chose a scan takes the plan that
was correctly rejected. If the estimate is right, the hint makes the query slower
by design.

If the estimate is wrong — the seek expected two hundred rows and produced two
hundred thousand — that is a statistics problem, and the hint hides it rather than
fixing it. Estimated and Actual Number of Rows on the operator are what to check
first, before either the index or the hint; PostgreSQL reports
[the same comparison in `EXPLAIN ANALYZE`](/blog/estimated-versus-actual-rows/).

## Elsewhere

The same mechanism appears under different names. On a heap table SQL Server calls
it a RID Lookup. InnoDB performs
[a second traversal through the clustered index](/blog/innodb-clustered-index/) for
the same reason, without naming the operator. PostgreSQL's equivalent is the heap
fetch in an index scan, and its bitmap heap scan exists specifically to convert many
random fetches into one ordered pass — a middle option the other two lack.

## FAQ

### What is a Key Lookup in a SQL Server execution plan?

A nonclustered index stores its key columns and a pointer to the rest of the row. When a query needs columns the index does not hold, the engine follows that pointer and reads the row from the clustered index. That operation appears in the plan as a Key Lookup, under a Nested Loops join, and it runs once per matching row. On a heap table the same operation is called a RID Lookup.

### Why did SQL Server stop using my nonclustered index?

Each Key Lookup is a small descent into the clustered index landing on an arbitrary page, so the total cost grows with the number of matching rows, while scanning the clustered index reads every page once in physical order. There is a crossover point, and past it the scan wins by a large margin. A plan that abandons the index is usually the optimizer reporting that the index does not cover the query.

### How do I get rid of a Key Lookup in SQL Server?

Read the output list on the Key Lookup operator: it names the columns the index was missing, which is exactly the list of candidates for INCLUDE. Adding them as included columns lets the index answer the query alone and the plan becomes a single seek. The index gets wider in exchange, so it holds fewer rows per page and every update to an included column now writes to it as well.

### Should I use an index hint when SQL Server chooses a scan?

Forcing the index takes the plan the optimizer correctly rejected, so if the row estimate was right the hint makes the query slower by design. If the estimate was wrong — the seek expected two hundred rows and produced two hundred thousand — that is a statistics problem, and the hint hides it rather than fixing it. Check the estimate before touching either the index or the hint.

## Sources

- [SQL Server documentation — Clustered and nonclustered indexes described](https://learn.microsoft.com/en-us/sql/relational-databases/indexes/clustered-and-nonclustered-indexes-described) (docs)
- [SQL Server documentation — Create indexes with included columns](https://learn.microsoft.com/en-us/sql/relational-databases/indexes/create-indexes-with-included-columns) (docs)
- [SQL Server documentation — Showplan logical and physical operators reference](https://learn.microsoft.com/en-us/sql/relational-databases/showplan-logical-and-physical-operators-reference) (docs) — Defines the Key Lookup and RID Lookup operators discussed here.
