Outer join direction and Hash Match Right Outer Join
It is easy to assume an outer join must read the preserved table first. Instead of an NL join driven by the large preserved table, switch the Build side of a hash join so the small side goes into memory.
SQL
Situation
A screen shows "this month's order count" by region. Customers without orders must not be dropped, so it is written as customers LEFT JOIN orders. Because of an OPTION (LOOP JOIN) attached earlier, it gets slower as customers grow.
Tables
| Table | Column | Description |
|---|---|---|
customers | cust_id, region | 100,000 customers, 5 regions |
orders | cust_id, ord_dt | 52,000 orders since 2025-09-01 |
| Index | Columns |
|---|---|
customers_pk (clustered) | cust_id |
orders_x1 | cust_id, ord_dt |
Query
BEFORE · NL outer join
SELECT c.region, COUNT(o.ord_id) AS cnt
FROM customers c
LEFT JOIN orders o ON o.cust_id = c.cust_id AND o.ord_dt >= CAST('2025-09-01' AS datetime2(0))
GROUP BY c.region
OPTION (LOOP JOIN)
Run it and check the Starts of the orders_x1 search.
Approach
1. Check how many inner searches happen
An NL outer join searches orders_x1 for every row of the preserved table (100,000 customers). Even customers with no orders this month are searched every time to confirm "none".
2. Ask whether the order is really fixed in an outer join
In an NL join the preserved table must be the outer side. A hash join has no such constraint. Even if the hash table is built from the inner side (orders) and probed with the preserved side (customers), unmatched customers can be kept in the result.
Explanation
NL outer join → Hash Match (Right Outer Join)
AFTER · Hash join
SELECT c.region, COUNT(o.ord_id) AS cnt
FROM customers c
LEFT JOIN orders o ON o.cust_id = c.cust_id AND o.ord_dt >= CAST('2025-09-01' AS datetime2(0))
GROUP BY c.region
OPTION (HASH JOIN)
Why it got faster
- In Before, the
orders_x1Index Seekhas Starts 100,000 and Buffers are about 301,000. - After is a
Hash Match (Right Outer Join). It builds the hash table from this month's orders (52,000 rows, reading all oforders_x1once) and probes it with the 100,000 customers. "Right Outer" is the direction seen from the Build input, so the outer join semantics are unchanged. Buffers about 5,600.
Why this is optimal
- Both inputs are read once and there are no repeated searches. The Build side (52,000 orders) is smaller than the 100,000 customers, so it also uses less memory.
Variation: removing the hint
TRY · No hint
SELECT c.region, COUNT(o.ord_id) AS cnt
FROM customers c
LEFT JOIN orders o ON o.cust_id = c.cust_id AND o.ord_dt >= CAST('2025-09-01' AS datetime2(0))
GROUP BY c.region
- The optimizer picks
Merge Join (Left Outer Join). Customers (clustered, incust_idorder) andorders_x1(incust_idorder) are already sorted in the same order, so they are matched without sorting. Buffers are the same as the hash join, about 5,600, and no hash table memory is needed.
Key points
- In an NL outer join the preserved table is always the outer side. If the preserved table is large, the searches are just as many.
- A hash outer join can pick the small side as Build (shown as
Right Outer Join). A merge join does the same without memory when both sides are sorted in join key order. - DB differences: Oracle switches the Build side with
SWAP_JOIN_INPUTS(HASH JOIN RIGHT OUTER), and PostgreSQL does the same withHash Right Join.