Top-N join: hash join vs index-ordered NL join

A screen showing the 20 latest orders with customer names. A hash join must join and sort everything before the first row comes out. An NL join reading in index order stops at 20 rows.

SQL

Situation

The admin home screen shows "the 20 latest paid orders" with customer name and grade. During batch tuning someone turned off NL join in this session, saying "NL join is slow" (enable_nestloop = off), and the home screen has been slow since.

Tables

TableColumnsDescription
ordersstatus, ord_dtabout 140,000 paid (PAID) orders
customerscust_nm, gradeCustomer name, grade
IndexColumns
orders_x2status, ord_dt, ord_id
customers_pkcust_id

Query

BEFORE · Hash join then sort

SET LOCAL enable_nestloop = off;
SELECT o.ord_id, o.ord_dt, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.status = 'PAID'
ORDER BY o.ord_dt DESC
LIMIT 20

Run it and check how far the join node's A-Rows exceeds 20.

Approach

1. Compare the rows the screen needs with the rows actually processed

The result is 20 rows; check whether the join and sort happened for all 140,000 paid orders. A hash join starts probing only after Build finishes, and a sort must receive the whole result.

2. Check whether the index already holds the sort order

WHERE status = 'PAID' ORDER BY ord_dt DESC matches the order of reading orders_x2 backward (scenario 2).

3. Check whether the join preserves the order

Joining orders that come out in index order to customers one at a time keeps the order intact, so it can stop at 20 rows. That is how an NL join works.

Explanation

Turn NL join back on (default settings)

AFTER · Reverse index order + NL join

SELECT o.ord_id, o.ord_dt, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.status = 'PAID'
ORDER BY o.ord_dt DESC
LIMIT 20

Why it got faster

  • Before reads 140,577 paid orders (Bitmap Heap Scan), hash-joins them with customers, then sorts with Sort to keep 20. Buffers about 20,300.
  • In After, Index Scan Backward reads the 20 latest orders from orders_x2, and Nested Loop finds each customer through customers_pk one at a time (Starts 20). Limit stops at 20 rows. Buffers 66. There is no Sort.
  • A Memoize is attached to the inner side, but the 20 rows all have different customers, so there are no cache hits. The planner attaches this node to NL join inner sides by habit.

Why this is optimal

  • 20 orders read, 20 customer lookups. The work equals the number of rows on screen.

Key points

SituationBetter joinReason
Aggregation or batch building the whole resultHash joinReads each side once with no repeated lookups
Screen showing only the first N rowsNL join + sorted indexKeeps the order and stops at N rows
  • Even for a join of the same two tables, the best join method changes with how much is fetched.
  • Setting enable_* for the whole session degrades other queries the setting does not suit.
  • DB difference: Oracle changes just one query with the USE_HASH/USE_NL hints (Oracle scenario 13); PostgreSQL changes it with planner settings. With defaults, both planners pick the NL join on their own.