Module 4 · Query optimization
Episode 29 Join Ordering
▶ This video is coming to YouTube soon.
The same four-table query took 112 ms in the order we wrote it and 8 ms in the order Postgres chose. This episode shows why join order matters, counts how fast the search space explodes, runs System R's dynamic programme with interesting orders, compares left-deep and bushy trees, and proves each step on a real Postgres with join_collapse_limit, from_collapse_limit and geqo_threshold.
Chapters
- 0:00Intro
- 0:14Same 30 rows, 14× slower
- 0:38Same answer, very different work
- 1:34All 24 orders of four tables
- 2:15The search space explodes
- 2:56Dynamic programming: one best plan per subset
- 4:13The code: System R's dynamic programme
- 4:53Interesting orders: keep the sorted plan too
- 5:53Left-deep or bushy
- 6:37Where Postgres stops searching
- 7:16Proof: Postgres rewrites the join order
- 7:42Proof: join_collapse_limit = 1 keeps the written order
- 8:13Proof: the sorted plan wins under ORDER BY
- 8:35Trade-offs
- 9:15Recap
- 9:47Next: Reading EXPLAIN