indexes for ORDER BY and LIMIT
a sorted index means no sort node. the top 10 rows in three page reads.
- address
- everything.indexes.indexes_and_order_by
- part
- part 8, indexes, from the outside
- chapter
- chapter 7 of 12
- reading time
- 7 min
a b-tree index is already sorted. if a query's ORDER BY matches the index's order, postgres reads the index in that order and the Sort node disappears. with a LIMIT on top, it stops after the first few entries: the newest 10 orders in 3 page reads instead of reading and sorting all 10,000.
it works backwards too, but only for the exact reverse, NULLs included.
the picture
you know how a leaderboard shows the top 10 without anyone sorting a million scores every time you look? it's kept in order. reading the first ten lines of something already sorted is the cheapest query there is.
no index on ordered_at: read all 10,000 orders and keep the newest 10 in a top-N heapsort.
the Limit node asks the scan for one row at a time, and stops asking after 10. a Sort can't work that way: it has to see every row before it can hand out the first (the iterator model). an index scan in the right order can, so LIMIT turns into "read 10 entries". that's the whole trick, and it's also why top-N per group and keyset pagination are fast with the right index.
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT id, customer_id, ordered_at FROM orders ORDER BY ordered_at DESC LIMIT 10;
CREATE INDEX orders_ordered_at ON orders (ordered_at);
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT id, customer_id, ordered_at FROM orders ORDER BY ordered_at DESC LIMIT 10;Index Scan Backward: the leaf pages are linked both ways, so the same index serves ASC (forwards) and DESC (backwards). you don't need a second index for the other direction.
directions and NULLs
a b-tree index has one order: by default ASC NULLS LAST. reading it backwards gives exactly the reverse, DESC NULLS FIRST, and those are the two orders it can produce without sorting. shipped_at is NULL for orders that haven't shipped, so it's a good column to watch:
ASC NULLS LAST: the index's own order. read it forwards.
SELECT id, shipped_at FROM orders ORDER BY shipped_at DESC LIMIT 5;SELECT count(*) FILTER (WHERE shipped_at IS NULL) AS unshipped FROM orders;
SELECT id, shipped_at FROM orders ORDER BY shipped_at DESC, id LIMIT 5;
SELECT id, shipped_at FROM orders ORDER BY shipped_at DESC NULLS LAST, id LIMIT 5;| unshippedint8 |
|---|
| 430 |
| idint4 | shipped_attimestamptz |
|---|---|
| 1 | null |
| 6 | null |
| 10 | null |
| 30 | null |
| 56 | null |
| idint4 | shipped_attimestamptz |
|---|---|
| 9959 | 2026-06-27 09:12:00+00 |
| 9915 | 2026-06-26 23:00:00+00 |
| 9908 | 2026-06-26 08:16:00+00 |
| 9898 | 2026-06-25 17:59:00+00 |
| 9877 | 2026-06-25 05:09:00+00 |
the first five are all NULLs, which is rarely what "latest shipped" meant. (NULLs in ORDER BY has the default rules.)
several columns
for an index on (a, b) the rule is the same, per column: the index can produce a ASC, b ASC and its exact reverse a DESC, b DESC. a mixed order, a ASC, b DESC, needs an index declared that way: (a, b DESC). and an ORDER BY can use a leading prefix of the index (ORDER BY a), or skip columns that the WHERE fixes with equality: with WHERE customer_id = 42, an index on (customer_id, ordered_at) serves ORDER BY ordered_at DESC directly.
CREATE INDEX orders_customer_at ON orders (customer_id, ordered_at);
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT id, ordered_at FROM orders WHERE customer_id = 42 ORDER BY ordered_at DESC LIMIT 3;Ada's last three orders: no Sort, a backward scan inside her slice of the index.
half a match: incremental sort
if the index matches only the first column of the ORDER BY, postgres doesn't throw it away. it reads rows in index order, which are already grouped by the first column, and sorts each group by the rest as it goes: an Incremental Sort. with a LIMIT, it stops as soon as enough groups are done.
ORDER BY customer_id, ordered_at DESC LIMIT 10. the index supplies customer_id order (Presorted Key), and only each customer's handful of orders gets sorted. the scan stopped after 58 rows.a full sort of 10,000 rows became a few tiny sorts of one customer's orders each. (sorting and work_mem has the sort methods, including the top-N heapsort above.)
keylines
- an index in ORDER BY order removes the Sort, and LIMIT then stops after a few entries.
- the same index serves the exact reverse: backward scan, NULLS flipped too.
- DESC means NULLS FIRST by default. match the index to the NULLS you want.
- mixed directions need an index declared that way, like (a, b DESC).
- a partial match still helps: Incremental Sort sorts within each group.