NL join vs hash join

See random access explode in an NL join during a large aggregation, and switch it to a hash join.

SQL

Situation

A batch aggregates 2025 monthly sales by region. It joins orders with customers and groups by region and month. The batch takes long, and there is a request to shorten it.

Tables

CUSTOMERS (100,000 rows) holds customers, with 5 regions at 20% each. ORDERS (2 million rows) holds orders accumulated at 2,000 per day.

TableColumnTypeDescription
CUSTOMERSCUST_IDNUMBERCustomer ID (PK)
CUSTOMERSREGIONVARCHAR2Region (SEOUL, BUSAN, DAEGU, INCHEON, GWANGJU)
ORDERSCUST_IDNUMBEROrdering customer ID
ORDERSORD_DTDATEOrder date and time
ORDERSAMTNUMBEROrder amount
IndexColumns
CUSTOMERS_PKCUST_ID
ORDERS_X1CUST_ID, ORD_DT

There is no index starting with ORD_DT.

Query

Sum the amounts of 2025 orders (about 538,000 rows) by region and month. The result has 5 regions × the number of months.

BEFORE · USE_NL

SELECT /*+ LEADING(o c) USE_NL(c) */
       c.region, TRUNC(o.ord_dt, 'MM') AS mm, SUM(o.amt) AS amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY c.region, TRUNC(o.ord_dt, 'MM')

Run it and check the join method and the Starts and Buffers of the operations on the CUSTOMERS side.

Approach

1. Look at the Starts of the inner operation

An NL join probes the inner side once for each row of the driving set. If the Starts of the index probe on the CUSTOMERS side equals the driving row count, the index and the table were visited separately that many times. Each probe reads a few blocks, so Buffers is several times Starts.

2. Ask whether the whole result must be produced

This query is an aggregation. The result appears only after every 2025 order has been read. It is not a screen that shows the first few rows quickly, so the advantage of an NL join (a fast first row) is of no use.

3. Consider whether changing the driving order helps

Even if the order is reversed and CUSTOMERS is read first, there are 100,000 customers, so the ORDERS_X1 probe repeats 100,000 times. When joining a large set to a large set, changing the NL join order does not reduce the repeat count much.

4. Choose a method that joins in one pass without repeated probes

Think of a join method that reads each table only once and matches pairs in memory. Two things need to be decided.

  • Whether the join condition is an equality (=)
  • Which of the two sets is smaller (the side loaded into memory)

Creating a new index is not an option. With any index, the structure of 538,000 probes remains.

Explanation

NL join → hash join

AFTER · USE_HASH

SELECT /*+ LEADING(c o) USE_HASH(o) FULL(c) FULL(o) */
       c.region, TRUNC(o.ord_dt, 'MM') AS mm, SUM(o.amt) AS amt
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY c.region, TRUNC(o.ord_dt, 'MM')

Why it got faster

  • In Before, the CUSTOMERS_PK INDEX UNIQUE SCAN has 538,000 Starts. Each row visits the index and the table separately.
  • After scans each table only once. It builds a hash table from the small CUSTOMERS (Build) and probes it with ORDERS (Probe).

Why this is optimal

  • The repeated probes in the join are gone. Reading each table once is the minimum this aggregation needs.
  • The remaining cost is the ORDERS FULL SCAN. Only 2025 orders are needed, but there is no index starting with ORD_DT, and visiting 538,000 rows one by one through an index costs more than a FULL SCAN. Even though the data is stored in date order, the leading part cannot be skipped without changing the table structure.
  • With the small CUSTOMERS on the Build side, the hash table is small. Whether it finishes within PGA is checked with Used-Tmp in the execution plan.

Variation: switching the Build input

TRY · SWAP_JOIN_INPUTS

SELECT /*+ LEADING(c o) USE_HASH(o) SWAP_JOIN_INPUTS(o) FULL(c) FULL(o) */
       c.region, TRUNC(o.ord_dt, 'MM') AS mm, SUM(o.amt) AS amt
FROM customers c
JOIN orders o ON o.cust_id = c.cust_id
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY c.region, TRUNC(o.ord_dt, 'MM')
  • The large ORDERS becomes the Build side. Compare the OMem / Used-Mem / Used-Tmp columns. When the Build input exceeds PGA, it uses TEMP and slows down.

Key points

NL joinHash joinSort merge join
Suitable forSmall volumes, partial range processing, OLTPLarge volumes, full range processing, batchAlready sorted sets, range (non-equi) joins
RequirementsJoin column index on the inner tableEquality (=) join condition, PGA memorySorting on both sides
First-row responseFastStarts after Build finishesStarts after sorting finishes
Random accessPer rowNoneNone
  • A hash join puts the smaller side on Build. "Smaller" means the size after applying conditions, not the table size.
  • A hash join is better when producing the whole result; an NL join is better for screens that show only the first few rows. This continues in scenario 2 and scenario 10.