Part 16 / Topic 4 of 5
Join algorithms
Three ways to join, each with a home turf. Hash join — build a hash table from the smaller input, stream the bigger one through it — is the default for "join lots to lots":
CREATE TABLE dim_customers AS
SELECT g AS customer_id, 'Customer ' || g AS name
FROM generate_series(1, 5000) AS g;
ALTER TABLE dim_customers ADD PRIMARY KEY (customer_id);
ANALYZE dim_customers;
EXPLAIN (COSTS OFF)
SELECT d.name, sum(b.amount)
FROM big_orders AS b
JOIN dim_customers AS d ON d.customer_id = b.customer_id
GROUP BY d.name;
SELECT 5000
ALTER TABLE
ANALYZE
QUERY PLAN
----------------------------------------------------
HashAggregate
Group Key: d.name
-> Hash Join
Hash Cond: (b.customer_id = d.customer_id)
-> Seq Scan on big_orders b
-> Hash
-> Seq Scan on dim_customers d
(7 rows)
(Read inner-first: seq-scan the 5,000 customers into a Hash, stream 200,000 orders past it.) Nested loop — for each outer row, probe the inner side — wins when the outer side is tiny and the inner side has an index:
EXPLAIN (COSTS OFF)
SELECT b.*
FROM big_orders AS b
JOIN dim_customers AS d ON d.customer_id = b.customer_id
WHERE d.name = 'Customer 42';
QUERY PLAN
----------------------------------------------------------
Nested Loop
-> Seq Scan on dim_customers d
Filter: (name = 'Customer 42'::text)
-> Bitmap Heap Scan on big_orders b
Recheck Cond: (customer_id = d.customer_id)
-> Bitmap Index Scan on idx_big_orders_customer
Index Cond: (customer_id = d.customer_id)
(7 rows)
With one qualifying customer, the loop runs once and probes the orders index. Merge join — walk two sorted inputs in lockstep — shines when both sides are already ordered (indexes, previous sorts) or enormous. Our hash-friendly sizes never tempt the planner, so ask it to show the shape by taking hash joins off the table for one statement:
BEGIN;
SET LOCAL enable_hashjoin = off;
EXPLAIN (COSTS OFF)
SELECT b.id, s.shipped_on
FROM big_orders AS b
JOIN (SELECT g AS order_id, DATE '2024-01-03' + (g % 365) AS shipped_on
FROM generate_series(1, 200000) AS g) AS s ON s.order_id = b.id;
ROLLBACK;
BEGIN
SET
QUERY PLAN
------------------------------------------------------
Merge Join
Merge Cond: (b.id = g.g)
-> Sort
Sort Key: b.id
-> Seq Scan on big_orders b
-> Materialize
-> Sort
Sort Key: g.g
-> Function Scan on generate_series g
(9 rows)
ROLLBACK
(enable_* flags are diagnostic levers for exactly this kind of inspection — never production settings.) The algorithms explain a classic surprise: the same join flips from nested loop to hash as a WHERE loosens, because the algorithms' costs cross as row counts grow. The plan didn't "randomly change" — the estimates did.
🧪 Quick quiz — answer from memory, then check.
- Which join builds an in-memory table from one input?
- When does a nested loop beat everything?
Show the answers
- Hash join — hashes the smaller side, streams the larger.
- Tiny outer input probing an indexed inner side — a few indexed lookups beat building any hash.
Done here? The practice set for Part 16 — 4 exercises with solutions — is on the part page.