Jump to content

Shuffle hash join

From Open Lakehouse Wiki
Shuffle hash join
Also known as Shuffled hash join, SHJ, ShuffledHashJoin
Field Distributed query processing
Key parts Shuffle exchange, build side, stream side, per-partition hash table
Open source implementations Apache Spark
Related Sort-merge join, Broadcast join, Shuffle, Adaptive Query Execution, Cost-based optimizer (Spark)
Last verified 23 November 2026 against the v4.3.0 tag of apache/spark (SparkStrategies.scala, joins.scala, ShuffledHashJoinExec.scala, SQLConf.scala, sql-performance-tuning.md and sql-ref-syntax-qry-select-hints.md).

A shuffle hash join is a distributed equality-join strategy that repartitions both inputs by the join keys, then builds a hash table from one input partition inside each join task.[1] In Apache Spark, the physical operator is named ShuffledHashJoin. Shuffle hash join avoids sorting both inputs, but it uses more task memory than a sort-merge join because every task constructs a hash table from its build-side partition.[2]

How does a shuffle hash join work?

A Spark shuffle hash join has three steps:[1]

  1. Shuffle both inputs. Spark uses compatible hash partitioning so equal join keys from both sides reach the same partition number.
  2. Build a local hash table. In every join task, Spark reads the selected build-side partition and creates a HashedRelation keyed by the join columns.
  3. Probe the table. The task streams rows from the other partition, called the stream side, and looks each key up in the local hash table.

Unlike a broadcast join, shuffle hash join does not collect the complete build side at the driver or send a full copy to every executor. Each task receives only its build partition. Unlike sort-merge join, shuffle hash join does not require the two shuffled partitions to be sorted before they are joined.

What does ShuffledHashJoin mean in EXPLAIN?

A representative plan forced with a SHUFFLE_HASH hint has this shape:

*(3) ShuffledHashJoin [customer_id#1L], [customer_id#8L], Inner, BuildRight
:- Exchange hashpartitioning(customer_id#1L, 200), ENSURE_REQUIREMENTS
:  +- Scan parquet orders
+- Exchange hashpartitioning(customer_id#8L, 200), ENSURE_REQUIREMENTS
   +- Scan parquet customers

The two Exchange hashpartitioning nodes show that both sides shuffle. BuildRight means each task builds its hash table from the right input partition and streams the left input partition through it. The absence of Sort operators is the main visual difference from a SortMergeJoin plan.

When does Spark pick shuffle hash join automatically?

Without a hint, Spark 4.3.0 considers shuffle hash join after broadcast hash join. The non-adaptive planner chooses it only when all of these conditions hold:[3][2]

  • spark.sql.join.preferSortMergeJoin is false; it defaults to true.[4]
  • One input is estimated small enough to build a local hash map. Spark tests its total estimated bytes against the automatic broadcast threshold multiplied by the number of shuffle partitions.
  • That input is estimated to be much smaller than the other. Spark multiplies the candidate build-side size by spark.sql.shuffledHashJoinFactor and requires the result to be no larger than the other side.
  • The equality-key data types support Spark's binary hash equality, and the join type permits the selected build side.

Because the default prefers sort-merge join, a static plan commonly uses SortMergeJoin for two non-broadcast inputs even when one is smaller. This preference reflects the planner's source comment that sort-merge join consumes less memory, while shuffled hash join can improve performance when one input is much smaller.[4]

How does AQE choose shuffle hash join?

Adaptive Query Execution can replace a planned sort-merge join after shuffle statistics reveal that every build-side partition is small enough for a local hash table. The threshold is spark.sql.adaptive.maxShuffledHashJoinLocalMapThreshold, which defaults to 0 bytes. For this preference to apply, the threshold must be at least spark.sql.adaptive.advisoryPartitionSizeInBytes, and every relevant partition must be no larger than the threshold.[5]

Spark 4.3.0 also has spark.sql.adaptive.convertSortMergeJoinToShuffledHashJoin.enabled, on by default, as the master switch for this conversion.[4] The zero-byte local-map threshold means that setting a useful positive threshold is still necessary before ordinary nonempty partitions qualify through this AQE rule.

How can a query request shuffle hash join?

The SHUFFLE_HASH hint requests the strategy and identifies the preferred build relation:[6]

SELECT /*+ SHUFFLE_HASH(customers) */
       orders.order_id, customers.region
FROM orders
JOIN customers
  ON orders.customer_id = customers.customer_id;

If both relations carry SHUFFLE_HASH hints, Spark chooses the build side from the join type and relation statistics. A hint is not a guarantee: Spark cannot use the strategy when the join has no equality keys or the requested build side is invalid for that join type.[6][2]

What are the tradeoffs?

Shuffle hash join Sort-merge join Broadcast hash join
Network work Shuffles both inputs Shuffles both inputs Broadcasts one complete input; normally leaves the streamed input in place
CPU work after shuffle Builds and probes a hash table; no key sort Sorts both inputs and merges ordered streams Probes a hash table already distributed to each executor
Memory scope One build partition per concurrent task Primarily sort and match buffers, which can spill The complete built relation on the driver and a copy available to executors
Best fit Build partitions are reliably much smaller than stream partitions and fit in task memory Both sides are large or build-side memory is uncertain One complete side is small enough to broadcast

Shuffle hash join can outperform sort-merge join by avoiding sorts, especially when the build side is much smaller. It can also fail or slow sharply when a build partition is larger than estimated: constructing HashedRelation is memory-intensive, and skew can concentrate many build rows in one task.[2][1] Increasing task concurrency can multiply this pressure because several tasks may build hash tables in the same executor at once.

AQE can reduce the risk when runtime partition sizes are available, but AQE cannot make a single oversized or highly skewed build partition cheap merely by changing the operator. Splitting skewed partitions, filtering earlier, improving statistics, or using sort-merge join may be safer when build-side memory is uncertain.

See also

References

  1. ↑ 1.0 1.1 1.2 ShuffledHashJoinExec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 2.2 2.3 SparkStrategies.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ joins.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ 4.0 4.1 4.2 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ 6.0 6.1 Hints (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.