Top-N join: hash join vs index-ordered NL join
A screen showing the 20 most recent 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 most recent paid orders" with customer name and grade. A developer who learned "large joins use hash joins" while tuning batch queries added OPTION (HASH JOIN) to this query too.
Tables
| Table | Column | Description |
|---|---|---|
orders | status, ord_dt, amt | About 140,000 paid (PAID) orders |
customers | cust_nm, grade | Customer name, grade |
| Index | Columns |
|---|---|
orders_x2 | status, ord_dt, ord_id |
customers_pk (clustered) | cust_id |
Query
BEFORE · Hash join then sort
SELECT TOP (20) 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
OPTION (HASH JOIN)
Run it and check how much larger than 20 the A-Rows of the join operator is.
Approach
1. Compare the row count the screen needs with the row count actually processed
The result is 20 rows; check whether the join and sort happened for all 140,000 paid orders. A hash join must receive its whole input before it can pass rows on to the sort.
2. Check whether the index already has the sort order
WHERE status = 'PAID' ORDER BY ord_dt DESC matches the order of reading orders_x2 backward.
3. Check whether the join preserves order
An NL join emits results in the order of the outer rows, so the order produced by the index is kept after the join and it can stop at 20 rows.
Explanation
Hash join → index-ordered NL join
AFTER · Reverse index order + NL join
SELECT TOP (20) 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
OPTION (LOOP JOIN)
Why it got faster
- Before reads all customers and all paid orders, hash joins them (
Hash MatchA-Rows 139,682), and keeps only 20 rows withSort (TopN Sort). Buffers about 13,800. - After reads only the 20 newest rows of
orders_x2with a backwardIndex Seekand attaches the remaining values per order with a Key Lookup and acustomers_pkSeek. Once 20 rows are filled,Topstops. Buffers about 79. There is no sort operator.
Why this is optimal
- 20 orders read, 20 order Key Lookups, 20 customer searches. The work matches the number of rows shown on screen.
Variation: removing all hints
TRY · No hints
SELECT TOP (20) 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
- Seeing
TOP (20), the optimizer picks a plan tuned for the first 20 rows on its own (the same NL join as After, Buffers about 79). The problem in Before was an unnecessary hint.
Key points
| Situation | Better join | Reason |
|---|---|---|
| Aggregation or batch producing the full result | Hash join | Reads each input once without repeated searches |
| Screen showing only the first N rows | NL join + sorted index | Preserves order and stops at N rows |
- Even for a join of the same two tables, the optimal join method changes with how many rows are fetched. A query hint stays in place even when that situation changes.
- DB differences: Oracle makes the same comparison with
ROWNUMandUSE_NL, and PostgreSQL withLIMITandenable_hashjoin.