Top-N partial range processing (sort elimination)
A screen showing the 10 latest orders. Instead of sorting the whole result for the app to use the front, LIMIT and index order stop at 10 rows.
SQL
Situation
The admin home screen shows the 10 latest paid orders. The query fetches all paid orders newest first, and the application puts only the first 10 on screen.
Tables
| Table | Column | Type | Description |
|---|---|---|---|
orders | status | varchar | about 140,000 rows are PAID |
orders | ord_dt | timestamp | Order date and time |
| Index | Columns |
|---|---|
orders_x2 | status, ord_dt, ord_id |
Latest paid orders
BEFORE · Full sort
-- Sort and send the whole result; the app uses only the first 10 rows
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC
Run it and check how many rows the Sort node sorted.
Approach
1. Check whether the DB was told how many rows are needed
Even if the app uses only 10 rows, the DB builds the whole result when the SQL does not say so. See whether the number of rows the screen needs can move into the SQL.
2. Check whether the index already holds the sort order
WHERE status = 'PAID' ORDER BY ord_dt DESC equals reading the order of orders_x2(status, ord_dt, …) backward. Reading the index from the end yields newest first without a sort.
3. Both must be present to stop
With only a row limit but a required sort, everything must be read; with only index order and no row limit, it reads to the end.
Explanation
Full sort → LIMIT + reverse index order
AFTER · LIMIT
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC
LIMIT 10
Why it got faster
- Before reads all 140,577 paid orders (
Bitmap Heap Scan, Buffers about 19,400) and sorts them withSort. - In After,
Index Scan BackwardunderLimitreads only 10 rows from the end of thePAIDsection oforders_x2and stops. Buffers 5. There is noSortnode.
Why this is optimal
- It reads only the blocks from the index root down to the leaf and the table blocks holding the 10 rows. The work equals the number of rows on screen.
Variation: when the sort direction differs from the index
TRY · NULLS LAST
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC NULLS LAST
LIMIT 10
- A PostgreSQL B-tree index returns NULLs first when read
DESC(DESC NULLS FIRST). AddingNULLS LASTdeparts from the index order, so all 140,577 rows are sorted (Buffers about 19,400). Even iford_dthas no NULLs, the planner does not know it without a NOT NULL constraint.
TRY · Top 10 by amount
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY amt DESC
LIMIT 10
- Without an index in
amtorder, everything is read even withLIMIT. However,Sortswitches to a Top-N sort (keeping only 10 rows), so it uses little memory.
Key points
- Top-N stops at the first N rows only with both a row limit (
LIMIT) and a sorted index. - The sort disappears only when the
ORDER BYdirection and evenNULLS FIRST/LASTmatch the index order. - DB difference: Oracle uses
ROWNUM(inline view) orFETCH FIRST; PostgreSQL usesLIMITorFETCH FIRST.