Ordering a Multi-Table Join
A chain of joins runs in the order you wrote it. a.join(b, ...).join(c, ...) builds the tree (a join b) join c, and the output of the first join becomes the left input of the next. Catalyst moves a join only to avoid a Cartesian product, when the written order would pair tables that share no key. It reorders by cost only when the tables have statistics and cost-based optimization is on with spark.sql.cbo.joinReorder.enabled set to true, a setting that is false by default. In every other case, the order on the page is the order the cluster pays for.
What a join's position costs
Take a click report. clicks holds 2 billion rows. campaigns holds 4,000 rows, of which 400 are active, and clicks on active campaigns are 10% of all clicks. users holds 80 million rows, far too large to broadcast. Both joins are inner joins, so both orders return the same rows.
| Order | First join | Next join | Rows shuffled |
|---|---|---|---|
| A: clicks, users, campaigns | Sort-merge with users: 2B + 80M rows shuffled | Broadcast active campaigns: no shuffle, 2B rows in, 200M out | 2.08B |
| B: clicks, campaigns, users | Broadcast active campaigns: no shuffle, 2B rows in, 200M out | Sort-merge with users: 200M + 80M rows shuffled | 280M |
- 7.4x
- 1.8B
Order B runs the filtering join first, so 9 of every 10 clicks are dropped in the scan stage before anything crosses the network, and the sort-merge join that follows shuffles a tenth of the clicks.
When the lookups that shrink or label the fact table can all broadcast, they run before any shuffle at all. In the animation, orders joins a broadcast customers and then a broadcast products: both small tables reach every task, the order rows never move, and both joins pipeline into the scan's stage.
(orders
.join(F.broadcast(customers), "customer_id")
.join(F.broadcast(products), "product_id")
.collect())| order_id | customer_id | product_id |
|---|---|---|
| 1 | c1 | 11 |
| 2 | c2 | 12 |
| 3 | c1 | 13 |
| 4 | c3 | 11 |
| 5 | c2 | 13 |
| 6 | c3 | 12 |
| customer_id | segment |
|---|---|
| c1 | Retail |
| c2 | Wholesale |
| c3 | Retail |
| product_id | category |
|---|---|
| 11 | Kitchen |
| 12 | Garden |
| 13 | Toys |
events holds 3 billion rows. It joins sessions (900 million rows, too large to broadcast) on session_id, and test_groups (2,000 rows) on user_id, an inner join that keeps 5% of events. Settings are the defaults. Which order shuffles the fewest rows?
- events with sessions first, so the largest join runs while every row is still available
- test_groups shuffled with events first, so the small side is partitioned like the large one
- Either order, since Catalyst reorders joins by estimated cost unless told not to
- test_groups first as a broadcast, then a sort-merge join of the 150M survivors with sessions
4 rules for ordering
- Filter before joining: Apply each table's own filters before its first join. Catalyst pushes most single-table filters down, but a condition comparing columns of 2 tables waits until both have joined.
- Most selective join first: Start with the join that drops the most rows of the large table, usually an inner join to a filtered dimension. Broadcast, it shrinks the data before any shuffle.
- Multiplying joins last: A one-to-many join, such as orders to order items, raises the row count, and every later join carries the extra rows.
- Same key, side by side: After a sort-merge join on order_id, the result is hash partitioned on order_id, so a following join on order_id shuffles only the new table.
Counting shuffles in a chain
Rule 4 shows in the shuffle count. Say none of the 4 tables orders, payments, shipments and customers is small enough to broadcast. payments and shipments join on order_id, while customers joins on customer_id:
| Written order | Shuffles | What gets shuffled |
|---|---|---|
| orders, payments, shipments, customers | 5 | orders, payments and shipments once each; then the joined result and customers on customer_id |
| orders, payments, customers, shipments | 6 | orders and payments; the result and customers on customer_id; that result again and shipments on order_id |
In the costlier order the joined result crosses the network 2 times instead of once, and on its last trip it carries the columns of 3 tables.
Run the cell below. It filters orders to completed orders before the first join, brings in the one-to-many order_items after that filter, and broadcasts only the 2 columns of products it needs.
Before writing a chain of joins, estimate the rows each join leaves, and put the joins that leave the fewest first. Then read the plan and confirm the exchanges land where you expected.
About this lesson
This section is part of the Joins: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.