The ORDER BY index trap: reading 2 million rows to skip a sort
To pick the 20 canceled orders from January 2023 with the largest order IDs, the optimizer reads the PK backward to skip the sort and passes 1.94 million rows before the first matching row appears. It does not know that the matching rows are packed at the opposite end of the PK order. Reading 1,879 rows through the condition index and sorting them drops Buffers from 17,708 to 433.
SQL
Situation
On the cancellation analysis screen, choosing a period shows 20 canceled orders of that period in descending order ID order. Recent periods appear instantly, but an old period such as January 2023 takes several seconds.
Tables
| Table | Columns | Description |
|---|---|---|
ORDERS | ORD_ID | Order ID. Same as insertion order, so also the same as ORD_DT order |
ORDERS | STATUS, ORD_DT | 1,879 canceled orders in January 2023 |
| Index | Columns |
|---|---|
ORDERS_PK | ORD_ID |
ORDERS_X2 | STATUS, ORD_DT, ORD_ID |
Query
BEFORE · 20 canceled orders from January 2023
SELECT * FROM (
SELECT ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
AND ord_dt < DATE '2023-02-01'
ORDER BY ord_id DESC
) WHERE ROWNUM <= 20
Compare the E-Rows and A-Rows of INDEX FULL SCAN DESCENDING ORDERS_PK.
Approach
1. See what the optimizer tried to save
With ROWNUM <= 20, the optimizer looks for a plan that returns the first 20 rows quickly. Reading ORDERS_PK backward yields descending ORD_ID order without a sort, so it can stop after meeting 20 matching rows. The cost of this plan depends on "how many rows must be passed before meeting 20".
2. See what that estimate assumes
The optimizer assumes matching rows are spread evenly over the ORD_ID order. If 0.1% of all rows match, it calculates that 20 rows are met within about 2,000 rows. In the actual data, ORD_ID and ORD_DT share the same order, and the condition is the oldest dates. Consider where the first matching row appears when reading backward.
3. Find a path that reads less even with a sort
See how many rows match the condition in total, and whether an index finds those rows directly. Compare whether reading and sorting all of them is cheaper than the PK backward scan. Then find a way to stop the optimizer from choosing the plan that skips the sort with the PK index.
Explanation
Read through the condition index and sort
AFTER · Condition index hint
SELECT * FROM (
SELECT /*+ INDEX(orders orders_x2) */ ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
AND ord_dt < DATE '2023-02-01'
ORDER BY ord_id DESC
) WHERE ROWNUM <= 20
AFTER · Expression on the ORDER BY column
SELECT * FROM (
SELECT ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
AND ord_dt < DATE '2023-02-01'
ORDER BY ord_id + 0 DESC
) WHERE ROWNUM <= 20
Why it got faster
- Before's
INDEX FULL SCAN DESCENDING ORDERS_PKhas E-Rows 1,936 and A-Rows 1,938,506. The optimizer expected 20 rows after passing 1,936 PK entries, but January 2023 orders are at the far end of the PK (the smallest side), so the first row appears only after passing nearly 2 million rows. Each passed entry reads the table to check status and date, so Buffers are 17,708. - Both Afters have the same plan. They read the 1,879 rows of the
STATUS = 'CANCELED' AND ORD_DT < 2023-02-01range inORDERS_X2(11 blocks), fetch the columns from the table (422 blocks), and pick the top 20 withSORT ORDER BY STOPKEY. Buffers 433. - The hint specifies the access index.
ORD_ID + 0changes the sort key from an index column to an expression, which removes the option of skipping the sort with the PK altogether. The result order is the same.
Why this is optimal
- Whatever order they are read in, all 1,879 matching rows must be seen to pick 20.
ORDERS_X2is ordered byORD_DT, ORD_IDand theORDER BYisORD_IDalone, so 20 rows cannot be picked directly in index order. - The sort target is 1,879 rows, so it finishes in memory. If the period grows and hundreds of thousands of rows match, the PK backward scan can then be better. The variation below compares a recent period.
Variation: a recent period
TRY · Since September 2025 (no hint)
SELECT * FROM (
SELECT ord_id, ord_dt, cust_id, amt
FROM orders
WHERE status = 'CANCELED'
AND ord_dt >= DATE '2025-09-01'
ORDER BY ord_id DESC
) WHERE ROWNUM <= 20
- When the condition is at the large end of the PK, the optimizer picks the same PK backward scan and finds 20 rows after passing 482. Buffers 8. The same plan is very fast or very slow depending on the period. If the screen accepts any period, run both cases before settling the plan.
Key points
- With
ROWNUMorFETCH FIRST, the optimizer prefers an index that skips the sort. The cost of that plan depends on where the matching rows sit in the index order, and the optimizer assumes they are spread evenly. - When the sort key and the condition column are correlated (here
ORD_IDandORD_DT), this assumption misses badly. Check the E-Rows and A-Rows of the sort-skipping index in the execution plan. - The fixes are an access index hint, or turning the
ORDER BYkey into an expression to block the sort skip. This is the counterexample to Scenario 2: skipping the sort is not always a gain.