Module 3 · Query execution
Episode 23 Joins II: Sort-Merge and Hash
▶ This video is coming to YouTube soon.
How does a database join 100,000 orders to 10,000 customers without a billion comparisons? We animate sort-merge join (with mark and restore for duplicate keys), hash join's build and probe, and the Grace hash join that partitions to disk at 3(M + N) I/O, then check it all in Postgres: Batches: 1, Batches: 8 at 64 kB, and two merge joins.
Chapters
- 0:00Intro
- 0:15A billion pairs, or 735 pages
- 0:32Why comparing pairs doesn't scale
- 1:11Sort-merge: sort, then one pass
- 2:22Duplicates on both sides: mark and restore
- 3:15The code: merge join
- 3:46Hash join: build, then probe
- 5:00The code: hash join
- 5:28Too big for memory: partition both inputs
- 6:31Then join each pair of partitions
- 7:12Proof: hash and merge joins in Postgres
- 8:51Trade-offs
- 9:21Recap
- 9:52Next: Aggregation