The ORDER BY Index Trap: Reading the Whole Table to Avoid a Sort

To pick the 20 canceled orders from January 2023 with the largest order IDs, the optimizer reads the clustered index backward to avoid a sort and passes through almost the whole table (12,797 pages). It does not know that the matching rows are concentrated at the opposite end of the ord_id order. Picking 20 ord_id values first from the condition index and looking up only those 20 rows cuts Buffers to 70.

SQL

Situation

On the cancellation analysis screen, choosing a period shows 20 canceled orders from that period in descending order ID order. Recent periods appear instantly, but an old period such as January 2023 is slow.

Tables

TableColumnDescription
ordersord_idOrder ID (clustered key). Same as insertion order, so also the same as ord_dt order
ordersstatus, ord_dt1,830 canceled orders in January 2023
IndexColumns
orders_pk (clustered)ord_id
orders_x2status, ord_dt, ord_id

Query

BEFORE · 20 canceled orders from January 2023

SELECT TOP (20) ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
  AND ord_dt < '2023-02-01'
ORDER BY ord_id DESC

Look at the estimated rows, actual rows, and logical reads of Clustered Index Scan (orders_pk).

Approach

1. See what the optimizer tried to save

With TOP (20), the optimizer sets a Row Goal and looks for a plan that returns the first 20 rows quickly. Reading the clustered index backward yields descending ord_id order without a sort, so it can stop once it meets 20 matching rows.

2. See what that estimate assumes

The optimizer assumes matching rows are spread evenly over the ord_id order. In the actual data, ord_id and ord_dt share the same order, and the condition is the oldest dates. Consider where matching rows first appear when reading backward.

3. Find a path that reads little even with a sort

See how many rows match in total and whether an index finds them directly. If that index contains ord_id, also work out whether the 20 rows can be picked before fetching the remaining columns.

Explanation

Pick 20 ord_id values first from the condition index and look up only those 20 rows

AFTER · Index hint + Key Lookup for only 20 rows

SELECT o.ord_id, o.ord_dt, o.cust_id, o.amt
FROM orders o
WHERE o.ord_id IN (SELECT TOP (20) ord_id
                   FROM orders WITH (INDEX(orders_x2))
                   WHERE status = 'CANCELED'
                     AND ord_dt < '2023-02-01'
                   ORDER BY ord_id DESC)
ORDER BY o.ord_id DESC

Why it got faster

  • Before reads orders_pk backward, checking status and date for each row. The estimated rows are 20, but the January 2023 orders sit at the far end of ord_id (the smallest side), so finding 20 rows passes through almost the whole table. Buffers are 12,797 (the whole table is 13,201 pages).
  • In After, the subquery reads the 1,830 rows of the status = 'CANCELED' AND ord_dt < 2023-02-01 range from orders_x2 (10 pages) and picks 20 with a TopN Sort on the ord_id in the index. The outer query looks up only those 20 rows in the clustered index (60 pages). Buffers are 70.

Why this is optimal

  • Picking 20 of the 1,830 matching rows requires seeing all of them in any read order, but they fit in 10 leaf pages of orders_x2. ord_id is in there too, so even the sort finishes within the index.
  • cust_id and amt are needed only for the 20 result rows. Adding just the hint to a single statement Key Lookups all 1,830 rows, for 5,254 Buffers (variation below). When the sort key is in the index, the pick first, look up after shape is cheaper.

Variation

TRY · Index hint only (1,830 Key Lookups)

SELECT TOP (20) ord_id, ord_dt, cust_id, amt
FROM orders WITH (INDEX(orders_x2))
WHERE status = 'CANCELED'
  AND ord_dt < '2023-02-01'
ORDER BY ord_id DESC
  • Buffers are 5,254. It Key Lookups all 1,830 rows, then sorts.

TRY · ORDER BY ord_id + 0

SELECT TOP (20) ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
  AND ord_dt < '2023-02-01'
ORDER BY ord_id + 0 DESC
  • In Oracle and PostgreSQL, turning the ORDER BY key into an expression removes the sort-avoiding plan. SQL Server recognizes that ord_id + 0 has the same order as ord_id and picks the same backward scan. Buffers are 12,797, the same as Before. In SQL Server, use an index hint.

TRY · Since September 2025 (no hint)

SELECT TOP (20) ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
  AND ord_dt >= '2025-09-01'
ORDER BY ord_id DESC
  • When the matching rows are at the large end of ord_id, the same backward scan finds 20 rows within 12 pages. The same plan is very fast or very slow depending on the period.

Key points

  • With TOP, the optimizer prefers an index that avoids the sort. The cost of that plan depends on where in the index order the matching rows are, and the optimizer assumes they are spread evenly.
  • When the sort key and the condition column are correlated (here ord_id and ord_dt), this assumption misses badly. Compare the estimated 20 rows with the pages actually passed.
  • If a nonclustered index contains the sort key, pick the Top-N from the keys alone and then fetch the remaining columns. The number of Key Lookups drops to the number of result rows.