Outer join direction and Hash Right Join
It is easy to think an outer join must read the preserved table first. That holds for an NL join, but a hash join can build the hash from the inner table and probe with the preserved table (Hash Right Join).
SQL
Situation
A screen shows "order count this month" by region. Customers without orders must not drop out, so it is written as customers LEFT JOIN orders. The session running this screen has hash join and merge join turned off (enable_hashjoin = off and so on), and it slowed down as customers grew.
Tables
| Table | Columns | Description |
|---|---|---|
customers | cust_id, region | 100,000 customers |
orders | cust_id, ord_dt | 52,000 orders since 2025-09-01 |
| Index | Columns |
|---|---|
orders_x1 | cust_id, ord_dt |
Query
BEFORE · NL outer join
SET LOCAL enable_hashjoin = off;
SET LOCAL enable_mergejoin = off;
SET LOCAL enable_memoize = off;
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 >= DATE '2025-09-01'
GROUP BY c.region
Run it and check the Starts and Buffers of the orders_x1 lookup.
Approach
1. Look at how many inner lookups happen
The preserved table customers has 100,000 rows, so the NL join looks up orders_x1 100,000 times. Even customers with no orders this month are visited each time to confirm that there are none.
2. Ask whether the order is really fixed in an outer join
In an NL join the preserved table (the left side of LEFT JOIN) must be outer. A hash join is different. It can build the hash from the inner table (orders) and probe with the preserved table while still keeping unmatched preserved rows in the result.
3. Decide which side goes into memory
After applying conditions, the smaller side, here the 52,000 orders this month, is the better one to build the hash from.
Explanation
NL outer join → Hash Right Join (default settings)
AFTER · Hash Right 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 >= DATE '2025-09-01'
GROUP BY c.region
Why it got faster
- In Before, the
orders_x1Index ScanunderNested Loop Lefthas Starts 100,000 and Buffers about 353,000. - After is a
Hash Right Join. It reads this month's orders to build the hash and probes with 100,000 customers. Buffers about 19,600. "Right" is only the direction relative to the side that built the hash; the result is stillcustomers LEFT JOIN orders. - In the execution plan view, the
Hashnode that builds the hash is lower down. PostgreSQL uses the input underHashas the Build side.
Why this is optimal
- There are no repeated lookups, and reading each table once is the minimum cost of this aggregation.
- Most of the remaining cost is reading all of
orders(about 18,750 blocks). There is no index starting withord_dt.
Key points
- In an NL outer join the preserved table is always outer. If the preserved table is large, the lookup count is just as large.
- A hash outer join can choose its Build side. PostgreSQL weighs costs and picks the smaller of
Hash Left Join(hash from the preserved table) andHash Right Join(hash from the inner table). - DB difference: Oracle swaps the Build side with the
SWAP_JOIN_INPUTShint, shown asHASH JOIN RIGHT OUTERin the execution plan (Oracle scenario 12). In PostgreSQL the planner decides.