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.

OrderFirst joinNext joinRows shuffled
A: clicks, users, campaignsSort-merge with users: 2B + 80M rows shuffledBroadcast active campaigns: no shuffle, 2B rows in, 200M out2.08B
B: clicks, campaigns, usersBroadcast active campaigns: no shuffle, 2B rows in, 200M outSort-merge with users: 200M + 80M rows shuffled280M

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())
orders
order_idcustomer_idproduct_id
1c111
2c212
3c113
4c311
5c213
6c312
customers
customer_idsegment
c1Retail
c2Wholesale
c3Retail
products
product_idcategory
11Kitchen
12Garden
13Toys

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?

4 rules for ordering

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 orderShufflesWhat gets shuffled
orders, payments, shipments, customers5orders, payments and shipments once each; then the joined result and customers on customer_id
orders, payments, customers, shipments6orders 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.