Top-N join: hash join vs NL join in index order

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

SQL

Situation

The admin home screen shows "the latest 20 paid orders" with customer name and grade. A developer who learned "hash join for bulk joins" while tuning batch queries added a hash join hint to this query too.

Tables

TableColumnTypeDescription
ORDERSSTATUSVARCHAR2Order status (DONE 90%, PAID 7%, CANCELED 3%)
ORDERSORD_DTDATEOrder date and time
CUSTOMERSCUST_NM, GRADEVARCHAR2Customer name, grade
IndexColumns
ORDERS_X2STATUS, ORD_DT, ORD_ID
CUSTOMERS_PKCUST_ID

There are about 140,000 paid (PAID) orders.

Query

BEFORE · Hash join then sort

SELECT * FROM (
  SELECT /*+ LEADING(o c) USE_HASH(c) */ 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
) WHERE ROWNUM <= 20

Run it and see how much larger than 20 the A-Rows (actual rows) of the join operation are.

Approach

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

The result is 20 rows; check whether the join and sort ran over all 140,000 paid orders. A hash join cannot start Probe until Build finishes, and ORDER BY needs the whole result before it can sort.

2. Check whether the index already holds the sort order

WHERE status = 'PAID' ORDER BY ord_dt DESC matches the order of ORDERS_X2(STATUS, ORD_DT, …). Reading the index from the end returns rows newest first without a sort (scenario 2).

3. Check whether the join preserves the order

The order must survive the join of rows produced in index order for the query to stop at 20 rows. Consider which join method keeps the order of the driving set as is.

Explanation

Hash join → NL join in index order

AFTER · Index descending + NL join

SELECT * FROM (
  SELECT /*+ LEADING(o c) USE_NL(c) INDEX_DESC(o orders_x2) */ 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
) WHERE ROWNUM <= 20

Why it got faster

  • Before uses about 14,500 Buffers. It hash-joined every paid order with customers (HASH JOIN A-Rows about 140,000), sorted with SORT ORDER BY STOPKEY, and kept only 20 rows.
  • After uses about 48 Buffers. It reads ORDERS_X2 newest first with INDEX RANGE SCAN DESCENDING and attaches each customer one row at a time through CUSTOMERS_PK. When 20 rows are filled, COUNT STOPKEY stops. There is no SORT operation.
  • An NL join emits results in the order of the driving rows, so the order produced by the index survives the join. A hash join keeps the Probe order, but it starts only after Build finishes, so it has already read one side in full before emitting the first row.

Why this is optimal

  • It read 20 orders and did 20 customer lookups. It did only as much work as the rows shown on screen.
  • The remaining cost is the index and table blocks of 20 customers. It cannot be avoided as long as the result includes customer names.

Variation: removing all hints

TRY · No hints

SELECT * FROM (
  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
) WHERE ROWNUM <= 20
  • The optimizer sees ROWNUM <= 20 and chooses a plan tuned for the first 20 rows (NL join + index descending) on its own. The problem in Before was an unnecessary hint blocking a good plan. Keep in mind that a hint stays in place even when the situation changes.

Key points

SituationBetter joinReason
Aggregation or batch that builds the whole resultHash joinReads each side once without repeated lookups
Screen that shows 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 optimal join method changes with how much is fetched.
  • Sort + Top-N gets partial range processing only when index order and an NL join work together. This continues scenarios 2 and 6.