Joins: Intermediate
Every join you write with .join() is a logical join: it says which rows pair up. Catalyst turns it into 1 of 4 physical join strategies, and the strategy decides how many shuffles the join costs, how much memory it needs, and whether it runs at all. The beginner lesson showed 2 of them working, the broadcast join and the shuffled join. This section names all 4 and the conditions Spark checks, in order, to pick one.
The 4 strategies
| Strategy | Plan node | When Spark picks it | What it costs |
|---|---|---|---|
| Broadcast hash join | BroadcastHashJoin | An equality key, and one side's estimated size is under `spark.sql.autoBroadcastJoinThreshold` | Ships the small side to every executor once; the big side never moves |
| Sort-merge join | SortMergeJoin | An equality key, neither side small enough to broadcast | Shuffles both sides on the key, sorts each partition, merges; 2 shuffles |
| Shuffle hash join | ShuffledHashJoin | An equality key, one side far smaller but over the threshold, and a `shuffle_hash` hint or `spark.sql.join.preferSortMergeJoin` set to false | Shuffles both sides, hashes the smaller side per partition, no sort |
| Broadcast nested loop join | BroadcastNestedLoopJoin | No equality key (a range or inequality condition), and one side is under the threshold | Broadcasts one side, then compares every row of the other against all of it |
The planner asks 3 questions in a fixed order. First, does the condition contain at least one equality between a column on each side? orders.product_id == products.product_id does; events.ts.between(sessions.start, sessions.end) does not. Without an equality there is no key to hash or sort on, so only the nested loop family remains: broadcast nested loop if one side fits under the threshold. When neither side fits, an inner join falls to a full cartesian product, while an outer join with no equality key still runs as broadcast nested loop and broadcasts a side that is over the threshold. Second, with an equality key, is either side's estimated size under the threshold? If so, broadcast hash join, with no shuffle on either side. Third, if both sides are over the threshold, the default is sort-merge join; shuffle hash join runs only when you ask for it with a hint or turn the sort-merge preference off.
The cartesian product is the fifth outcome rather than a fifth strategy: it is what remains when no key exists and nothing fits in memory, and it pairs every row with every row.
What the choice costs
You can override the choice per join with a hint on either DataFrame. F.broadcast(products) and products.hint("broadcast") are the same request; merge asks for sort-merge join and shuffle_hash for shuffle hash join. On a join with no equality key, the broadcast hint is what produces a broadcast nested loop join, and shuffle_replicate_nl asks for the cartesian product. A hint that can't be honoured is dropped: a merge hint on a join with no equality key does nothing, because there is no key to merge on.
Run the cell below. The hint keeps order_items in place and ships products to each task. The rows that come back are the same as without the hint; what changed is the plan underneath, and section 3 shows how to see that.
units = (order_items
.join(F.broadcast(products), "product_id")
.groupBy("product_id")
.agg(F.max("product_name").alias("product_name"),
F.sum("quantity").alias("units"))
.orderBy(F.desc("units"), "product_id"))
units.show(5)orders is a 300GB fact table and stores is a 40MB table. orders.join(stores, "store_id") runs with default settings and no hints. Which strategy does Spark pick?
The threshold is spark.sql.autoBroadcastJoinThreshold, and its default is 10485760 bytes, 10MB. Spark compares it against a number it computes before reading a single row: the planner's size estimate for each side of the join, in bytes. Whether that estimate is right decides whether a 2MB dimension gets broadcast or shuffled.
Where the estimate comes from
| Operator above the scan | Estimate it reports | Why that is often wrong |
|---|---|---|
| `select` of fewer columns | Child size scaled by the width of the kept columns | Column widths are type defaults, not measured |
| `filter` | The child's size, unchanged | A filter that keeps 1% of rows still reports 100% |
| `join` | Left size times right size | Treats every row as matching every row |
| `groupBy` with keys | The child's size, unchanged | Collapsing 1B rows to 500 groups reports 1B rows' worth of bytes |
| `groupBy` with no keys | 1 row | The only operator whose estimate shrinks |
Read the filter row twice, because it is the one that bites. A products table of 300MB on disk, filtered to the 2MB of rows where active == 1, is still 300MB to the planner. That is 30 times over the threshold, so Spark picks sort-merge join and shuffles a 400GB fact table to meet a 2MB dimension. Nothing in the logs says the estimate was wrong; the job takes an hour instead of 4 minutes.
dim is 60MB on disk. fact.join(dim.filter(F.col("region") == "EU"), "id") runs with the default threshold, no statistics and no hint. The EU rows total 3MB. Which strategy runs?
Telling Spark what you know
When you know the side is small and the planner does not, wrap it in F.broadcast(). The hint is a promise, and Spark takes it over its own estimate, up to the hard caps in the next section. Apply the filter first and broadcast the filtered DataFrame. On an inner join the optimizer would push a later filter on products columns below the broadcast anyway, so this order does not change the plan; it makes the code show which rows travel to every executor.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)active = products.filter(F.col("active") == 1)orders.join(F.broadcast(active), "product_id").collect()
The animation sets the threshold to -1 first. That value turns automatic broadcasting off entirely, which is how you make a plan take the sort-merge path in a test; with it off, only the hint can produce a broadcast. products has 4 rows and the filter drops the 1 inactive row, Fax machine, before the 3 survivors are copied to each of the 2 tasks reading orders. The 6 order rows stay in their 2 input partitions through the whole join; no row of orders moves.
The other fix is to give the planner real numbers. Computing statistics for the table with an ANALYZE TABLE statement stores row counts and per-column histograms in the catalog, and with spark.sql.cbo.enabled on, the cost-based optimizer uses them to estimate how many rows a filter keeps. That makes the estimate right for every query on the table; the hint makes it right for 1 join. Statistics suit dimensions many jobs read; the hint suits a one-off.
Try it on the practice tables. products is filtered to in-stock rows before the hint, so only those rows travel; the aggregate afterwards groups on the join key.
in_stock = products.filter(F.col("in_stock") == 1)
stocked = (order_items
.join(F.broadcast(in_stock), "product_id")
.groupBy("product_id")
.agg(F.sum("quantity").alias("units"))
.orderBy("product_id"))
stocked.show(5)The first 2 are refusals: Spark throws an error naming the cap and the job dies at the moment the broadcast is built. The third is a clock: spark.sql.broadcastTimeout is how long the driver waits for the build side to be collected and shipped, and a broadcast that overruns it fails the job. None of those is the practical ceiling. The practical ceiling comes from what a broadcast makes the cluster do, and it lands at 1 to 2GB on disk.
What a broadcast makes the cluster do
| Build side on disk | Hash table in memory (about 4x) | Driver heap it needs | Network with 100 executors |
|---|---|---|---|
| 10MB | 40MB | 40MB | 4GB |
| 500MB | 2GB | 2GB | 200GB |
| 2GB | 8GB | 8GB | 800GB |
- Driver builds a 4GB hash table before anything ships
- Every executor holds 4GB for the life of the job
- 100 executors pull 400GB over the network
- The fact table never shuffles: 1 stage
- Driver holds nothing; executors do all the work
- A task holds 1 partition of each side, not the table
- 1GB plus the fact table cross the network once
- Both sides shuffle and sort: 3 stages
An engineer sets spark.sql.autoBroadcastJoinThreshold to 4GB so that a 3GB customers table broadcasts in a join to a 1TB fact table. The driver runs with its default memory. What fails first?
You've added a hint, or raised the threshold, and the job still takes an hour. The next step is to read the plan rather than guess. df.explain() prints the physical plan Catalyst settled on, and the join node in it names the strategy and the side that was built. The rule from the Catalyst lessons, counting Exchange nodes to count shuffles, applies to joins the same way.
The same 2 lines print different plans depending on what the planner believes about products. Below are the 2 plans side by side, with the null filters Spark adds under an inner join left out to keep the lines short.
| Physical plan line |
|---|
| BroadcastHashJoin [product_id#3], [product_id#12], Inner, BuildRight |
| :- FileScan parquet orders[order_id#2,product_id#3] |
| +- BroadcastExchange HashedRelationBroadcastMode(...) |
| +- FileScan parquet products[product_id#12,product_name#13] |
| Physical plan line |
|---|
| SortMergeJoin [product_id#3], [product_id#12], Inner |
| :- Sort [product_id#3 ASC NULLS FIRST], false, 0 |
| : +- Exchange hashpartitioning(product_id#3, 200) |
| : +- FileScan parquet orders[order_id#2,product_id#3] |
| +- Sort [product_id#12 ASC NULLS FIRST], false, 0 |
| +- Exchange hashpartitioning(product_id#12, 200) |
| +- FileScan parquet products[product_id#12,product_name#13] |
Read the join line first. BroadcastHashJoin ends with BuildRight: the right side of the join, the second DataFrame in orders.join(products), is the one turned into a hash table and shipped. Under it, the BroadcastExchange node sits on the branch that was broadcast, and the other branch runs from FileScan straight into the join with no Exchange at all: orders never moved. The right-hand plan has no build side to name. Both branches carry an Exchange hashpartitioning(..., 200), 1 shuffle each into 200 reducer partitions, and a Sort above each exchange, which is the merge's precondition.
- ▸
BroadcastHashJoin ... BuildLeftorBuildRight: strategy 1; the named side is the one broadcast, and aBroadcastExchangesits under it - ▸
SortMergeJoin: strategy 2; expect anExchangeand aSorton both branches, 2 shuffles - ▸
ShuffledHashJoin ... BuildLeftorBuildRight: strategy 3; anExchangeon both branches, noSort, the named side is hashed per partition - ▸
BroadcastNestedLoopJoin BuildRight, Inner: strategy 4; aBroadcastExchangeon the named side and a condition with no equality in it - ▸
CartesianProduct: the fallback; no key and nothing small enough to broadcast
The plan also predicts the stage count you will see once the job runs. Each Exchange ends a stage, so the left plan is 1 stage for the join itself plus a small separate job that collects products for the broadcast, and the right plan is 3 stages: 1 per scan feeding an exchange, and 1 that reads both exchanges and merges. If a job that should broadcast shows 3 stages for the join, the plan is where the mismatch shows first, before any stage has run.
Which side can be built
For an inner join Spark may build either side and picks the smaller estimate, so BuildLeft shows up whenever the first DataFrame is the small one. An outer join removes the choice. The side a join preserves must be the streamed side, because the hash table only holds rows that get probed, so a left join can only build the right side and a right join only the left; a full outer join cannot be a broadcast hash join at all. small.join(big, "id", "left") therefore runs as sort-merge even with a hint on small, and the plan tells you so with a SortMergeJoin line ending in LeftOuter.
customers is 4MB and orders is 200GB. The plan for the join below shows SortMergeJoin [customer_id#5], [customer_id#20], LeftOuter, even though customers carries a broadcast hint. Why did Spark not broadcast customers?
explain("formatted") prints the same tree with each node numbered and a details block under it, including the estimated size each side was planned with. When a dimension you expected to broadcast did not, that size is the first number to read.A join with 3 or more tables runs as a chain of 2-table joins, in the order you wrote them. Catalyst can reorder joins only when the cost-based optimizer is on and spark.sql.cbo.joinReorder.enabled is set, and both are off by default, so for most jobs the order in the code is the order Spark runs. Each link in the chain is 1 of the 4 strategies from section 0, and each one's cost depends on the size of its inputs, which is where ordering matters: the output of one join is the input to the next.
A worked chain
Take 3 tables. orders is 2 billion rows and 400GB. customers is 50 million rows and 5GB, past the broadcast ceiling. products is 200,000 rows and 20MB, and the job wants only the Toys category: 5% of products, which is 1MB, and 3% of orders. The same 3 tables can be joined in 2 orders.
| Step | Order A: customers first | Order B: toys first |
|---|---|---|
| Join 1 | `orders` to `customers`: sort-merge, shuffles 400GB + 5GB, output 2B rows | `orders` to broadcast(`toys`): no shuffle, output 60M rows, 12GB |
| Join 2 | 2B rows to broadcast(`toys`): no shuffle, output 60M rows | 12GB to `customers`: sort-merge, shuffles 12GB + 5GB |
| Shuffle total | 405GB | 17GB |
| Rows that are joined and then discarded | 1.94B | 0 |
The ordering rules
- Filter each table before it joins. Spark pushes a filter on one table's columns below an inner join anyway, but the fact table shrinks only at the join to that table.
- Join the filtered, broadcastable dimensions first; each one shrinks the fact table without a shuffle.
- Put the sort-merge joins last, when the fact side is as small as it is going to get.
- Keep joins on the same key adjacent: the second reuses the first's hash partitioning and skips a shuffle.
- Join 2 fact tables to each other only after both have been filtered and reduced by their dimensions.
Rule 4 comes from the shuffle lessons. orders.join(order_items, "order_id").join(shipments, "order_id") shuffles orders and order_items on order_id for the first join, and the result is already partitioned on order_id, so the second join shuffles only shipments. Put a sort-merge join on product_id between them and the order_id partitioning is gone; the third join shuffles the whole intermediate result again. A broadcast join on product_id in the same place keeps it, because the fact side never moves, so the third join still shuffles only shipments. The same reuse applies to a groupBy on the join key straight after the join.
(orders.join(F.broadcast(customers), "customer_id").join(F.broadcast(products), "product_id").collect())
The animation chains 2 broadcast joins. orders, 6 rows in 2 partitions, picks up segment from customers and then category from products; both dimensions have 3 rows and both arrive at every task whole. Watch the orders partitions: through 2 joins they never leave the task that read them, which is the shape to aim for in a dimension-heavy chain, every dimension broadcast and the fact table staying put or shrinking.
The cell below applies rules 1 and 2 to the practice tables. The filter on status sits on orders before anything joins it, and products arrives by broadcast. The practice tables are all far under 10MB, so Spark broadcasts the order_items join as well, and neither join shuffles. The groupBy on order_id then adds its own Exchange, because no earlier shuffle left the rows partitioned on that key, and the orderBy adds a range shuffle after it. With a production-sized order_items, the first join would be a sort-merge on order_id, and the groupBy would reuse that partitioning with no shuffle of its own.
completed = orders.filter(F.col("status") == "Completed")
revenue = (completed
.join(order_items, "order_id")
.join(F.broadcast(products), "product_id")
.groupBy("order_id")
.agg(F.sum(F.col("quantity") * F.col("unit_price")).alias("revenue"))
.orderBy(F.desc("revenue"), "order_id"))
revenue.show(5)- Write the filter on each table above its join, so the join sees fewer rows
- Join filtered dimensions by broadcast before any sort-merge join
- Group consecutive joins on one key so the partitioning carries over
- Confirm the order with explain() and count the Exchange nodes
- Join the 2 largest tables before a dimension join has cut either one down
- Rely on the optimizer to reorder joins; join reordering is off by default
- Interleave a sort-merge join on another key between 2 joins on the same key
- Expect a filter comparing columns of both sides to shrink the join beneath it
events is 1TB with user_id and device_id. users is 30GB with a country_code. devices is 3MB, and the job keeps 1 device type, 2% of devices. countries is 50KB and joins only through users.country_code. Which order moves the least data?
> An Uber team builds a daily table of trips enriched with city, driver and promotion details. Trips is 3 billion rows, drivers is 6GB, cities is 2MB, and promotions is 400MB on disk but the job wants only the 50 promotions active that day. The first version joins trips to drivers first and runs for 2 hours; the plan shows 2 SortMergeJoin nodes and 4 Exchange nodes, one of the joins being promotions.
F.broadcast() on the filtered DataFrame fixes that one join.SortMergeJoin with 2 Exchange nodes.BuildRight on each BroadcastHashJoin line confirms that cities and promotions, written as the right side of each .join(), were the sides shipped.F.broadcast() overrides the estimate.explain() names the strategy in the join node, and BuildLeft or BuildRight names the side that was broadcast.Four physical join strategies, one 10MB line, and a plan you can read to prove which one ran.
- Category
- Spark
- Difficulty
- intermediate
- Duration
- 14 minutes
- Challenges
- 19 hands-on challenges
Topics covered: The Four Join Strategies, The 10MB Broadcast Threshold, The Real Broadcast Ceiling, Verifying with explain(), Ordering a Multi-Table Join
Lesson Sections
- The Four Join Strategies
The 4 strategies The cartesian product is the fifth outcome rather than a fifth strategy: it is what remains when no key exists and nothing fits in memory, and it pairs every row with every row. What the choice costs Put sizes on it. A fact table of 400GB joins a dimension of 8MB on a cluster of 200 executors. Broadcast hash join reads the dimension once, sends 8MB to each executor, 1.6GB of network in total, and every task joins its own partition of the fact table where it sits: 1 stage and no
- The 10MB Broadcast Threshold (concepts: Broadcast Join)
Where the estimate comes from For a table read from files, the estimate starts as the sum of the file sizes in the directory listing: the compressed bytes on disk, which Spark gets from the storage system without opening a file. Each operator above the scan adjusts that figure by a fixed rule, and the rules are blunt. The table below is the whole set the planner uses when table statistics are absent, which they are by default. Telling Spark what you know
- The Real Broadcast Ceiling (concepts: Broadcast Join)
The config takes any number of bytes, so nothing stops you setting it to 5GB and letting every join broadcast. The cost of a broadcast doesn't scale the way the threshold suggests, and 3 fixed limits sit in the way before the config matters. What a broadcast makes the cluster do The second column is the one the threshold never mentions. Parquet stores columns compressed; the hash table holds every row decompressed, each with its own header, plus an index over the keys, and that comes to roughly
- Verifying with explain() (concepts: Broadcast Join)
Which side can be built
- Ordering a Multi-Table Join
A worked chain Both orders produce the same 60 million rows. Order B moves 24 times fewer bytes because the only filter in the job, Toys, is applied by the join that can be broadcast, and that join runs before the join that cannot. The rule generalises: run first the join that removes the most rows per byte it moves, and that is nearly always a broadcast join against a filtered dimension, because it shrinks the fact table with no shuffle at all. The ordering rules