skip to content

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.

index =
SELECT id, customer_id, ordered_at FROM orders ORDER BY ordered_at DESC LIMIT 10
pages read by the whole query (Buffers: shared hit + read)
86 pages
3 pages

no index on ordered_at: read all 10,000 orders and keep the newest 10 in a top-N heapsort.

real output, postgres 18.6 (PGlite 0.5.12) · order-by
the 10 newest orders. without an index, postgres reads all 83 pages of the table, sorts as it goes (a top-N heapsort that only keeps the best 10), and returns 10. with an index on ordered_at it reads the index from its newest end, one leaf, one heap page, and stops: 3 pages.

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.

sqldb=shop
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;
output, from postgres 18.6
#1 · EXPLAIN
Limit (actual rows=10.00 loops=1) Buffers: shared hit=3 read=83 -> Sort (actual rows=10.00 loops=1) Sort Key: ordered_at DESC Sort Method: top-N heapsort Memory: 17kB Buffers: shared hit=3 read=83 -> Seq Scan on orders (actual rows=10000.00 loops=1) Buffers: shared read=83 Planning: Buffers: shared hit=41 read=7 dirtied=1
#2 · CREATE INDEX
#3 · EXPLAIN
Limit (actual rows=10.00 loops=1) Buffers: shared hit=1 read=2 -> Index Scan Backward using orders_ordered_at on orders (actual rows=10.00 loops=1) Index Searches: 1 Buffers: shared hit=1 read=2 Planning: Buffers: shared hit=15 read=1

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:

ORDER BY shipped_at =
SELECT id, shipped_at FROM orders ORDER BY shipped_at LIMIT 5
pages read by the whole query (Buffers: shared hit + read)
3 pages
3 pages
83 pages
83 pages

ASC NULLS LAST: the index's own order. read it forwards.

real output, postgres 18.6 (PGlite 0.5.12) · order-by
an index on shipped_at (ASC NULLS LAST, the default) and four ways to order by it. forwards and backwards, no Sort. the two orders that put NULLs at the other end need a Sort over the whole table: 83 pages instead of 3.
predict the output
ORDER BY shipped_at DESC LIMIT 5: the five most recently shipped orders?
sql
SELECT id, shipped_at FROM orders ORDER BY shipped_at DESC LIMIT 5;
sqldb=shop
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;
output, from postgres 18.6
#1 · SELECT 1
unshippedint8
430
(1 row)
#2 · SELECT 5
idint4shipped_attimestamptz
1null
6null
10null
30null
56null
(5 rows)
#3 · SELECT 5
idint4shipped_attimestamptz
99592026-06-27 09:12:00+00
99152026-06-26 23:00:00+00
99082026-06-26 08:16:00+00
98982026-06-25 17:59:00+00
98772026-06-25 05:09:00+00
(5 rows)

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.

sqldb=shop
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;
output, from postgres 18.6
#1 · CREATE INDEX
#2 · EXPLAIN
Limit (actual rows=3.00 loops=1) Buffers: shared hit=3 read=2 -> Index Scan Backward using orders_customer_at on orders (actual rows=3.00 loops=1) Index Cond: (customer_id = 42) Index Searches: 1 Buffers: shared hit=3 read=2 Planning: Buffers: shared hit=58 read=8 dirtied=1

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.

show =
actual rowsbuffer hitsbuffer reads
real output, postgres 18.6 (PGlite 0.5.12) · order-by
an index on customer_id alone, and 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.