NL join vs hash join
Shows how the repeated searches of an NL join explode in a large aggregation and switches to a hash join. SQL Server forces the join method with query hints such as OPTION (LOOP JOIN).
SQL
Situation
A batch aggregates monthly sales by region for 2025. OPTION (LOOP JOIN) was attached to the query earlier on the grounds that "hash joins take a lot of memory", and as data accumulated the batch became several times slower.
Tables
| Table | Column | Description |
|---|---|---|
customers | cust_id, region | 100,000 customers, 5 regions |
orders | cust_id, ord_dt, amt | 2,000,000 orders |
| Index | Columns |
|---|---|
customers_pk (clustered) | cust_id |
orders_pk (clustered) | ord_id |
Query
BEFORE · LOOP JOIN hint
SELECT c.region, DATEFROMPARTS(YEAR(o.ord_dt), MONTH(o.ord_dt), 1) AS mm, SUM(CAST(o.amt AS bigint)) AS amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= CAST('2025-01-01' AS datetime2(0))
GROUP BY c.region, DATEFROMPARTS(YEAR(o.ord_dt), MONTH(o.ord_dt), 1)
OPTION (LOOP JOIN)
Run it and check the execution count (Starts) and Buffers of the customers_pk search under Nested Loops.
Approach
1. Check the Starts of the inner operator
An NL join searches the inner side once per outer row. If the Starts of Clustered Index Seek equals the outer row count (538,000), the B-tree was descended separately that many times.
2. Ask whether the whole result must be produced
The aggregation produces a result only after reading every 2025 order. The NL join's advantage of returning the first few rows fast is useless here.
3. Find out why the hint is there
OPTION (LOOP JOIN) is a constraint that removes options from the optimizer. Check what it picks when the hint is removed.
Explanation
Hint removed → hash join
AFTER · No hint (hash join)
SELECT c.region, DATEFROMPARTS(YEAR(o.ord_dt), MONTH(o.ord_dt), 1) AS mm, SUM(CAST(o.amt AS bigint)) AS amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= CAST('2025-01-01' AS datetime2(0))
GROUP BY c.region, DATEFROMPARTS(YEAR(o.ord_dt), MONTH(o.ord_dt), 1)
Why it got faster
- In Before, the
customers_pkClustered Index Seekhas Starts 538,000 and total Buffers are about 1,055,000. - After builds a hash table from the small
customers(Build) and matches pairs while readingordersonce (Hash Match). Buffers about 13,800. - Check After's memory grant in the Trace tab. The memory that the hash join and hash aggregate will use is granted before execution.
Why this is optimal
- Reading each of the two tables once is the minimum for this aggregation.
- The remaining cost is the full read of
orders(about 13,200 pages). There is no index that starts withord_dt.
Key points
NL join (Nested Loops) | Hash join (Hash Match) | |
|---|---|---|
| Suited to | Small volume, first N rows, OLTP | Large volume, full result, batch |
| Requires | Index on the join column on the inner side | Equality condition, memory grant |
| Repeated searches | Per outer row | None |
OPTION (LOOP JOIN)applies to every join in the statement. To change only one join, use a join hint such asINNER LOOP JOIN.- DB differences: Oracle enforces the same with
USE_NL/USE_HASHhints, and PostgreSQL withenable_*planner settings.