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

StrategyPlan operatorWhat movesWhen Spark picks it
Broadcast hash joinBroadcastHashJoinThe small side is copied to every executor; the large side stays where it was readEquality key, and a side the join type lets Spark build is estimated at or under the broadcast threshold
Shuffle hash joinShuffledHashJoinBoth sides are shuffled by key; each task hashes its share of the smaller sideEquality 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 joinSortMergeJoinBoth sides are shuffled by key, then sorted by key inside each partitionEquality key on sortable types, with no side small enough to broadcast
Nested loop joinBroadcastNestedLoopJoin or CartesianProductEvery row of one side is tested against every row of the otherNo 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

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 typeSide Spark may broadcast
innerEither; the smaller estimate wins
left outer, left semi, left antiRight only
right outerLeft only
full outerNeither, 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()
orders
order_idregion
101West
102East
103West
104North
105East
106South
payments
order_idamount
10125
10340.5
10412
10560
10633.25
10718

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")

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.