<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Shuffle_hash_join</id>
	<title>Shuffle hash join - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Shuffle_hash_join"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Shuffle_hash_join&amp;action=history"/>
	<updated>2026-12-06T00:13:35Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Shuffle_hash_join&amp;diff=178&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Shuffle_hash_join&amp;diff=178&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:50Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Shuffle hash join&lt;br /&gt;
| also_known_as   = Shuffled hash join, SHJ, &amp;lt;code&amp;gt;ShuffledHashJoin&amp;lt;/code&amp;gt;&lt;br /&gt;
| field           = Distributed query processing&lt;br /&gt;
| key_parts       = Shuffle exchange, build side, stream side, per-partition hash table&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Sort-merge join]], [[Broadcast join]], [[Shuffle]], [[Adaptive Query Execution]], [[Cost-based optimizer (Spark)]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|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)}}&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;shuffle hash join&amp;#039;&amp;#039;&amp;#039; 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.&amp;lt;ref name=&amp;quot;shjexec&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledHashJoinExec.scala |title=ShuffledHashJoinExec.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; In [[Apache Spark]], the physical operator is named &amp;lt;code&amp;gt;ShuffledHashJoin&amp;lt;/code&amp;gt;. 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.&amp;lt;ref name=&amp;quot;strategies&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala |title=SparkStrategies.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does a shuffle hash join work? ==&lt;br /&gt;
A Spark shuffle hash join has three steps:&amp;lt;ref name=&amp;quot;shjexec&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Shuffle both inputs.&amp;#039;&amp;#039;&amp;#039; Spark uses compatible hash partitioning so equal join keys from both sides reach the same partition number.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Build a local hash table.&amp;#039;&amp;#039;&amp;#039; In every join task, Spark reads the selected &amp;#039;&amp;#039;&amp;#039;build-side&amp;#039;&amp;#039;&amp;#039; partition and creates a &amp;lt;code&amp;gt;HashedRelation&amp;lt;/code&amp;gt; keyed by the join columns.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Probe the table.&amp;#039;&amp;#039;&amp;#039; The task streams rows from the other partition, called the &amp;#039;&amp;#039;&amp;#039;stream side&amp;#039;&amp;#039;&amp;#039;, and looks each key up in the local hash table.&lt;br /&gt;
&lt;br /&gt;
Unlike a [[broadcast join]], shuffle hash join does not collect the complete build side at the [[Driver (Apache Spark)|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.&lt;br /&gt;
&lt;br /&gt;
== What does ShuffledHashJoin mean in EXPLAIN? ==&lt;br /&gt;
A representative plan forced with a &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; hint has this shape:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
*(3) ShuffledHashJoin [customer_id#1L], [customer_id#8L], Inner, BuildRight&lt;br /&gt;
:- Exchange hashpartitioning(customer_id#1L, 200), ENSURE_REQUIREMENTS&lt;br /&gt;
:  +- Scan parquet orders&lt;br /&gt;
+- Exchange hashpartitioning(customer_id#8L, 200), ENSURE_REQUIREMENTS&lt;br /&gt;
   +- Scan parquet customers&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two &amp;lt;code&amp;gt;Exchange hashpartitioning&amp;lt;/code&amp;gt; nodes show that both sides shuffle. &amp;lt;code&amp;gt;BuildRight&amp;lt;/code&amp;gt; means each task builds its hash table from the right input partition and streams the left input partition through it. The absence of &amp;lt;code&amp;gt;Sort&amp;lt;/code&amp;gt; operators is the main visual difference from a &amp;lt;code&amp;gt;SortMergeJoin&amp;lt;/code&amp;gt; plan.&lt;br /&gt;
&lt;br /&gt;
== When does Spark pick shuffle hash join automatically? ==&lt;br /&gt;
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:&amp;lt;ref name=&amp;quot;joins&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala |title=joins.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;strategies&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;spark.sql.join.preferSortMergeJoin&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;; it defaults to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* 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.&lt;br /&gt;
* That input is estimated to be much smaller than the other. Spark multiplies the candidate build-side size by &amp;lt;code&amp;gt;spark.sql.shuffledHashJoinFactor&amp;lt;/code&amp;gt; and requires the result to be no larger than the other side.&lt;br /&gt;
* The equality-key data types support Spark&amp;#039;s binary hash equality, and the join type permits the selected build side.&lt;br /&gt;
&lt;br /&gt;
Because the default prefers sort-merge join, a static plan commonly uses &amp;lt;code&amp;gt;SortMergeJoin&amp;lt;/code&amp;gt; for two non-broadcast inputs even when one is smaller. This preference reflects the planner&amp;#039;s source comment that sort-merge join consumes less memory, while shuffled hash join can improve performance when one input is much smaller.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does AQE choose shuffle hash join? ==&lt;br /&gt;
[[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 &amp;lt;code&amp;gt;spark.sql.adaptive.maxShuffledHashJoinLocalMapThreshold&amp;lt;/code&amp;gt;, which defaults to 0 bytes. For this preference to apply, the threshold must be at least &amp;lt;code&amp;gt;spark.sql.adaptive.advisoryPartitionSizeInBytes&amp;lt;/code&amp;gt;, and every relevant partition must be no larger than the threshold.&amp;lt;ref name=&amp;quot;perf&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-performance-tuning.html |title=Performance Tuning (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Spark 4.3.0 also has &amp;lt;code&amp;gt;spark.sql.adaptive.convertSortMergeJoinToShuffledHashJoin.enabled&amp;lt;/code&amp;gt;, on by default, as the master switch for this conversion.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
== How can a query request shuffle hash join? ==&lt;br /&gt;
The &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; hint requests the strategy and identifies the preferred build relation:&amp;lt;ref name=&amp;quot;hints&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-syntax-qry-select-hints.html |title=Hints (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT /*+ SHUFFLE_HASH(customers) */&lt;br /&gt;
       orders.order_id, customers.region&lt;br /&gt;
FROM orders&lt;br /&gt;
JOIN customers&lt;br /&gt;
  ON orders.customer_id = customers.customer_id;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If both relations carry &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; 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.&amp;lt;ref name=&amp;quot;hints&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;strategies&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What are the tradeoffs? ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Shuffle hash join !! Sort-merge join !! Broadcast hash join&lt;br /&gt;
|-&lt;br /&gt;
! Network work&lt;br /&gt;
| Shuffles both inputs&lt;br /&gt;
| Shuffles both inputs&lt;br /&gt;
| Broadcasts one complete input; normally leaves the streamed input in place&lt;br /&gt;
|-&lt;br /&gt;
! CPU work after shuffle&lt;br /&gt;
| Builds and probes a hash table; no key sort&lt;br /&gt;
| Sorts both inputs and merges ordered streams&lt;br /&gt;
| Probes a hash table already distributed to each executor&lt;br /&gt;
|-&lt;br /&gt;
! Memory scope&lt;br /&gt;
| One build partition per concurrent task&lt;br /&gt;
| Primarily sort and match buffers, which can spill&lt;br /&gt;
| The complete built relation on the driver and a copy available to executors&lt;br /&gt;
|-&lt;br /&gt;
! Best fit&lt;br /&gt;
| Build partitions are reliably much smaller than stream partitions and fit in task memory&lt;br /&gt;
| Both sides are large or build-side memory is uncertain&lt;br /&gt;
| One complete side is small enough to broadcast&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;code&amp;gt;HashedRelation&amp;lt;/code&amp;gt; is memory-intensive, and skew can concentrate many build rows in one task.&amp;lt;ref name=&amp;quot;strategies&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;shjexec&amp;quot; /&amp;gt; Increasing task concurrency can multiply this pressure because several tasks may build hash tables in the same executor at once.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Apache Spark]]&lt;br /&gt;
* [[Sort-merge join]]&lt;br /&gt;
* [[Broadcast join]]&lt;br /&gt;
* [[Adaptive Query Execution]]&lt;br /&gt;
* [[Shuffle]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>