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

TableColumnTypeDescription
ordersstatusvarcharabout 140,000 rows are PAID
ordersord_dttimestampOrder date and time
IndexColumns
orders_x2status, 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 with Sort.
  • In After, Index Scan Backward under Limit reads only 10 rows from the end of the PAID section of orders_x2 and stops. Buffers 5. There is no Sort node.

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). Adding NULLS LAST departs from the index order, so all 140,577 rows are sorted (Buffers about 19,400). Even if ord_dt has 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 amt order, everything is read even with LIMIT. However, Sort switches 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 BY direction and even NULLS FIRST/LAST match the index order.
  • DB difference: Oracle uses ROWNUM (inline view) or FETCH FIRST; PostgreSQL uses LIMIT or FETCH FIRST.