INDEX vs full read: Key Lookup sets the break-even point

The rule "use the index below some percent" needs particular care in SQL Server. A Key Lookup descends the B-tree again for every row found through a nonclustered index, so even 3% costs more than 10 times a full read.

SQL

Situation

A query shows the count and amount of this month's canceled orders. It is called thousands of times a day and reads the whole table every time. Conversely, someone attached an index hint to another query that looks at "all canceled orders".

Tables

TableColumnDescription
ordersstatusDONE 90%, PAID 7%, CANCELED 3% (mixed regardless of date)
ordersord_dtOrder date and time. The table is stored in date order (ord_id clustered)
ordersamtOrder amount (not in orders_x2)
IndexColumns
orders_pk (clustered)ord_id
orders_x2status, ord_dt, ord_id

The table (clustered index) is about 13,200 pages.

Queries

BEFORE · Default plan (this month's cancellations)

SELECT COUNT(*) AS cnt, SUM(CAST(o.amt AS bigint)) AS amt
FROM orders o
WHERE o.status = 'CANCELED'
  AND o.ord_dt >= CAST('2025-09-01' AS datetime2(0))

BEFORE · Index hint (all cancellations)

SELECT COUNT(*) AS cnt, SUM(CAST(o.amt AS bigint)) AS amt
FROM orders o WITH (INDEX(orders_x2))
WHERE o.status = 'CANCELED'

Check the operators and Buffers of the two queries, and the Starts of the Key Lookup (Clustered Index Seek).

Approach

1. Count the cost of index access in Key Lookups

orders_x2 has no amt, so the clustered index is searched again for every row found through the index (Key Lookup). Each one descends the B-tree from the root, so it is about 3 pages per row. This count does not drop even if the rows are clustered together in the table.

2. Compute the selectivity of the whole condition

This month's canceled orders are about 1,600 rows (0.08%), and all canceled orders are about 60,000 rows (3%). The Lookup costs are about 4,600 and about 170,000 pages respectively. The whole table is 13,200 pages.

3. Re-measure the optimizer's choice and the hint

The optimizer chooses by estimate, and a hint is the judgment made when it was added. Check both with actual Buffers.

Explanation

① Full read → index (this month's cancellations)

AFTER · Index hint

SELECT COUNT(*) AS cnt, SUM(CAST(o.amt AS bigint)) AS amt
FROM orders o WITH (INDEX(orders_x2))
WHERE o.status = 'CANCELED'
  AND o.ord_dt >= CAST('2025-09-01' AS datetime2(0))

Why it got faster

  • In Before #1 the optimizer picks a Clustered Index Scan and reads the whole table (about 13,200) for 1,591 rows.
  • After #1 seeks orders_x2 (10 pages) and fetches amt with 1,591 Key Lookups. Buffers about 4,600. In this case the optimizer's cost estimate rated the index side as more expensive than it really is.

② Index → full read (all cancellations)

AFTER · Hint removed (full read)

SELECT COUNT(*) AS cnt, SUM(CAST(o.amt AS bigint)) AS amt
FROM orders o
WHERE o.status = 'CANCELED'

Why a full read here

  • Before #2 performs 59,934 Key Lookups, for Buffers of about 172,500.
  • After #2 reads the table once, for about 13,200. Even selecting only 3%, the Lookups cost 13 times a full read.

Why this is optimal

  • In ①, the Lookup count (1,591 × about 3) is smaller than the table size, so the index is right. Reducing it further requires adding amt to the index (INCLUDE) to remove the Lookups, which is a structural change.
  • In ②, the Lookup count is several times the table size, so a full read that reads every page once in order is the minimum.

Variation: at 24%

TRY · Index: 2025 completed orders (24%)

SELECT COUNT(*) AS cnt, SUM(CAST(o.amt AS bigint)) AS amt
FROM orders o WITH (INDEX(orders_x2))
WHERE o.status = 'DONE'
  AND o.ord_dt >= CAST('2025-01-01' AS datetime2(0))

TRY · Full read: 2025 completed orders (24%)

SELECT COUNT(*) AS cnt, SUM(CAST(o.amt AS bigint)) AS amt
FROM orders o WITH (INDEX(0))
WHERE o.status = 'DONE'
  AND o.ord_dt >= CAST('2025-01-01' AS datetime2(0))
  • Reading 484,655 rows through the index pushes Buffers to about 1,395,000 from the Key Lookups, more than 100 times a full read (about 13,200). WITH (INDEX(0)) is a hint meaning "scan the clustered index (the table)".

Key points

  • In SQL Server the cost of nonclustered index access is Key Lookup count × B-tree depth. Logical reads do not drop even if the rows are clustered together in the table.
  • So the ratio at which the index wins (tipping point) is very low: usually up to a row count of a fraction of the table's page count.
  • Both the optimizer's choice and a hint can be wrong. Measure with actual Buffers.
  • DB differences: in Oracle, TABLE ACCESS BY INDEX ROWID BATCHED reads rows in the same block consecutively, so when rows are clustered together (a good clustering factor) the index wins even at 24% (Oracle scenario 14). In SQL Server the same query gives the opposite result.