The Four Join Strategies
A join in your code names a type, such as inner or left outer. Catalyst's physical planning then decides how each join runs on the cluster, choosing one of 4 strategies. The strategy decides whether rows cross the network, how many of them cross, and how much memory one task needs. 2 queries that return identical rows can differ by an hour of runtime because the planner chose differently.
The 4 strategies side by side
| Strategy | Plan operator | What moves | When Spark picks it |
|---|---|---|---|
| Broadcast hash join | BroadcastHashJoin | The small side is copied to every executor; the large side stays where it was read | Equality key, and a side the join type lets Spark build is estimated at or under the broadcast threshold |
| Shuffle hash join | ShuffledHashJoin | Both sides are shuffled by key; each task hashes its share of the smaller side | Equality key, and either sort-merge is not preferred and the smaller side is under about 2GB, or the key type cannot be sorted |
| Sort-merge join | SortMergeJoin | Both sides are shuffled by key, then sorted by key inside each partition | Equality key on sortable types, with no side small enough to broadcast |
| Nested loop join | BroadcastNestedLoopJoin or CartesianProduct | Every row of one side is tested against every row of the other | No equality key, as in a range or inequality condition |
The broadcast hash join is the strategy the planner tries before anything else, because it adds no shuffle and no stage boundary. The sort-merge join is the default for 2 large inputs. Each side passes through an exchange that hash-partitions it on the join key into spark.sql.shuffle.partitions partitions, 200 by default, so equal keys from both sides land in the same task, which sorts both of its inputs by key before matching them. Spark's documentation for spark.sql.join.preferSortMergeJoin, which defaults to true, gives the reason it is preferred: it uses less memory than a shuffle hash join and works efficiently when both tables are large.
The shuffle hash join shuffles both sides the same way and skips the sort. Each task builds a hash table from its partition of the smaller side, so that table has to fit in the task's memory. With default settings and no hint, Spark picks it only when the join key's type cannot be sorted. The nested loop join is the last resort. With no equality between the sides there is no key to hash or sort on, so rows are compared pair by pair: Spark broadcasts one side when it can (BroadcastNestedLoopJoin), and an inner join with neither side small enough runs as a CartesianProduct.
The order the planner checks
With no hint, Spark asks these questions of each join, in order
- 1. Is there an equality key? If the condition has no a = b between the 2 sides, only nested loop strategies remain.
- 2. Can a side be broadcast? If a side the join type lets Spark build has an estimate at or under spark.sql.autoBroadcastJoinThreshold, 10MB by default, Spark plans a broadcast hash join.
- 3. Is a shuffle hash join allowed? Only if spark.sql.join.preferSortMergeJoin is false and the smaller side is at least 3 times smaller than the other and estimated under the threshold times spark.sql.shuffle.partitions, about 2GB by default, or if the key cannot be sorted.
- 4. Otherwise, sort-merge. Every equality join on sortable keys that reaches this question gets a sort-merge join.
The join type limits the build side
In an outer join, an unmatched row from the preserved side must still come out, padded with NULLs. If the preserved side were the broadcast copy, every task would hold all of its rows but see only a slice of the other side, so no task could tell whether a row went unmatched everywhere. A broadcast hash join therefore builds only the side that is not preserved:
| Join type | Side Spark may broadcast |
|---|---|
| inner | Either; the smaller estimate wins |
| left outer, left semi, left anti | Right only |
| right outer | Left only |
| full outer | Neither, so no broadcast hash join |
The animation below sets the threshold to -1 in its first line, so 2 small tables join the way 2 large ones would. Watch orders and payments both leave their read tasks and cross the shuffle by order_id into 3 reducer tasks, where equal keys meet. Order 102 has no payment and payment 107 has no order, so the inner join returns 5 rows.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
orders.join(payments, "order_id").collect()| order_id | region |
|---|---|
| 101 | West |
| 102 | East |
| 103 | West |
| 104 | North |
| 105 | East |
| 106 | South |
| order_id | amount |
|---|---|
| 101 | 25 |
| 103 | 40.5 |
| 104 | 12 |
| 105 | 60 |
| 106 | 33.25 |
| 107 | 18 |
Hints override the questions above. F.broadcast(df) or df.hint("broadcast") requests a broadcast, df.hint("merge") a sort-merge join, df.hint("shuffle_hash") a shuffle hash join and df.hint("shuffle_replicate_nl") a Cartesian product. When the sides carry different hints, Spark's documented priority runs in that same order, broadcast first. A hint the join type cannot honour is dropped with a warning in the driver log.
regions is 2MB and orders is 400GB, both read from Parquet, with default settings. Which strategy does Spark plan for this join?
report = regions.join(orders, "region_id", "left")- Broadcast hash join, broadcasting regions, since it is far under the 10MB threshold
- Sort-merge join, since a left outer join can broadcast only its right side, and orders is far too large
- Broadcast nested loop join, since the small table sits on the preserved side of an outer join
- Shuffle hash join, since regions is more than 3 times smaller than orders
To predict a strategy, ask the 4 questions in order and check the join type against the side you hope to build. The next section takes question 2 apart.
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.