<?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=Join_strategy_hints</id>
	<title>Join strategy hints - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Join_strategy_hints"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Join_strategy_hints&amp;action=history"/>
	<updated>2026-12-06T00:07:48Z</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=Join_strategy_hints&amp;diff=154&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=Join_strategy_hints&amp;diff=154&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:43Z</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            = Join strategy hints&lt;br /&gt;
| also_known_as   = Join hints (Spark SQL)&lt;br /&gt;
| field           = Query optimization&lt;br /&gt;
| introduced      = &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; before Spark 3.0; &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;SHUFFLE_REPLICATE_NL&amp;lt;/code&amp;gt; in Apache Spark 3.0.0&lt;br /&gt;
| key_parts       = &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SHUFFLE_REPLICATE_NL&amp;lt;/code&amp;gt;&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Shuffle]], [[Adaptive Query Execution]], [[Data skew]], [[Broadcast join]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/sql-ref-syntax-qry-select-hints.md, docs/sql-performance-tuning.md, SparkStrategies.scala, adaptive/DemoteBroadcastHashJoin.scala, adaptive/ConvertSortMergeJoinToShuffledHashJoin.scala, adaptive/OptimizeSkewedJoin.scala, exchange/BroadcastExchangeExec.scala, SQLConf.scala, core unsafe/map/BytesToBytesMap.java) and SPARK-27225}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]] SQL, &amp;#039;&amp;#039;&amp;#039;join strategy hints&amp;#039;&amp;#039;&amp;#039; are comments of the form &amp;lt;code&amp;gt;/*+ HINT(table) */&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;DataFrame.hint()&amp;lt;/code&amp;gt; calls, that ask the planner to use a specific physical join algorithm for a relation instead of the one it would pick from statistics.&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; Spark supports four: &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;SHUFFLE_REPLICATE_NL&amp;lt;/code&amp;gt;. A hint is a suggestion, not a command: Spark ignores it when the hinted strategy does not support the join type.&amp;lt;ref name=&amp;quot;hints&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Which join hints does Spark support? ==&lt;br /&gt;
Before Spark 3.0 only the &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; hint existed. Spark 3.0.0 added &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;SHUFFLE_REPLICATE_NL&amp;lt;/code&amp;gt;, one for each of Spark&amp;#039;s other join strategies.&amp;lt;ref name=&amp;quot;hints&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;spark27225&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-27225 |title=SPARK-27225: Implement join strategy hints |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Hint !! Aliases !! Physical join it asks for !! Notes&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;BROADCASTJOIN&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MAPJOIN&amp;lt;/code&amp;gt;&lt;br /&gt;
| Broadcast hash join (with equi-join keys) or broadcast nested loop join (without)&lt;br /&gt;
| The hinted side is broadcast even if it is larger than &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; (default 10 MB). If both sides are hinted, the smaller one by statistics is broadcast.&amp;lt;ref name=&amp;quot;hints&amp;quot; /&amp;gt;&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;
| &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;SHUFFLE_MERGE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MERGEJOIN&amp;lt;/code&amp;gt;&lt;br /&gt;
| Shuffle sort-merge join&lt;br /&gt;
| Needs sortable join keys.&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;
| &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt;&lt;br /&gt;
| none&lt;br /&gt;
| Shuffle hash join&lt;br /&gt;
| If both sides are hinted, the smaller side by statistics becomes the build side.&amp;lt;ref name=&amp;quot;hints&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;SHUFFLE_REPLICATE_NL&amp;lt;/code&amp;gt;&lt;br /&gt;
| none&lt;br /&gt;
| Shuffle-and-replicate nested loop join (cartesian product)&lt;br /&gt;
| Used only for inner-like joins.&amp;lt;ref name=&amp;quot;strategies&amp;quot; /&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; hint is still bounded by hard limits: Spark fails a broadcast whose table is 8 GiB or larger (&amp;lt;code&amp;gt;spark.sql.maxBroadcastTableSize&amp;lt;/code&amp;gt;, added in 4.1.0) or that has too many rows. The row limit depends on the broadcast mode: for a hash join whose key is not a single &amp;lt;code&amp;gt;LongType&amp;lt;/code&amp;gt; column it is &amp;lt;code&amp;gt;BytesToBytesMap.MAX_CAPACITY / 1.5&amp;lt;/code&amp;gt;, that is 2&amp;lt;sup&amp;gt;29&amp;lt;/sup&amp;gt; / 1.5 or 357,913,941 rows, and in other cases it is 512,000,000 rows.&amp;lt;ref name=&amp;quot;btbm&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java |title=BytesToBytesMap.java (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;bee&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/exchange/BroadcastExchangeExec.scala |title=BroadcastExchangeExec.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&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;
&lt;br /&gt;
== How to write a join hint ==&lt;br /&gt;
In Spark SQL the hint goes in a comment right after &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt; and names the table or alias it applies to. In PySpark the same hint is set with &amp;lt;code&amp;gt;hint()&amp;lt;/code&amp;gt; on the DataFrame that should get the strategy, or with &amp;lt;code&amp;gt;broadcast()&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;pyspark.sql.functions&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
SELECT /*+ BROADCAST(d) */ f.order_id, d.region&lt;br /&gt;
FROM sales f JOIN dim_store d ON f.store_id = d.store_id;&lt;br /&gt;
&lt;br /&gt;
SELECT /*+ SHUFFLE_HASH(c) */ * FROM orders o JOIN customers c ON o.cust_id = c.cust_id;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql.functions import broadcast&lt;br /&gt;
&lt;br /&gt;
orders.join(broadcast(stores), &amp;quot;store_id&amp;quot;)&lt;br /&gt;
orders.join(customers.hint(&amp;quot;merge&amp;quot;), &amp;quot;cust_id&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Which hint wins when several are given? ==&lt;br /&gt;
When the two sides of a join carry different strategy hints, Spark prefers &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; over &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt; over &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; over &amp;lt;code&amp;gt;SHUFFLE_REPLICATE_NL&amp;lt;/code&amp;gt;, and logs a warning that the overridden hint will not take effect.&amp;lt;ref name=&amp;quot;hints&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Spark&amp;#039;s join selection looks at hints before it looks at sizes. For an equi-join, it tries the hinted strategies in the order above; only if there is no hint, or no hint applies, does it fall back to its default order: broadcast hash join if a side is under the broadcast threshold, then shuffle hash join, then sort-merge join, then cartesian product, then broadcast nested loop join.&amp;lt;ref name=&amp;quot;strategies&amp;quot; /&amp;gt; A &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; hint on a join without equi-join keys cannot be honored, and Spark reports it as unsupported.&amp;lt;ref name=&amp;quot;strategies&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do join hints interact with Adaptive Query Execution? ==&lt;br /&gt;
[[Adaptive Query Execution]] (AQE), on by default since Spark 3.2.0, re-plans joins at runtime from the sizes of completed shuffle stages. Three AQE rules change joins, and each treats hints differently.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Sort-merge join to broadcast join.&amp;#039;&amp;#039;&amp;#039; AQE turns a sort-merge join into a broadcast hash join when a side&amp;#039;s runtime size is under &amp;lt;code&amp;gt;spark.sql.adaptive.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; (which defaults to &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt;).&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt; AQE also has a rule that marks a side with many empty partitions as &amp;quot;do not broadcast&amp;quot;, but the rule leaves a side alone if the user already gave it a strategy hint.&amp;lt;ref name=&amp;quot;demote&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/adaptive/DemoteBroadcastHashJoin.scala |title=DemoteBroadcastHashJoin.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Sort-merge join to shuffled hash join.&amp;#039;&amp;#039;&amp;#039; AQE can switch a sort-merge join to a shuffled hash join when every partition is under &amp;lt;code&amp;gt;spark.sql.adaptive.maxShuffledHashJoinLocalMapThreshold&amp;lt;/code&amp;gt; (default 0, so off). It skips joins that carry a &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt; hint.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;smj2shj&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/adaptive/ConvertSortMergeJoinToShuffledHashJoin.scala |title=ConvertSortMergeJoinToShuffledHashJoin.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Skew join splitting.&amp;#039;&amp;#039;&amp;#039; AQE splits skewed partitions of sort-merge and shuffled hash joins, whichever strategy chose them, so a &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;SHUFFLE_HASH&amp;lt;/code&amp;gt; hint does not turn off skew handling.&amp;lt;ref name=&amp;quot;skew&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/adaptive/OptimizeSkewedJoin.scala |title=OptimizeSkewedJoin.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; See [[data skew]].&lt;br /&gt;
&lt;br /&gt;
Hints and AQE work from different information. A hint records a choice the user knows in advance; AQE uses the measured size of each shuffle stage. With AQE on, a table that is small only after filtering can be broadcast without a hint, because AQE sees its runtime size; this is less efficient than planning the broadcast up front, but avoids the sort on both sides.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Join hints compared with partitioning hints ==&lt;br /&gt;
Spark SQL has a second family of hints that does not pick a join algorithm. &amp;#039;&amp;#039;&amp;#039;Partitioning hints&amp;#039;&amp;#039;&amp;#039; (&amp;lt;code&amp;gt;COALESCE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;REPARTITION&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;REPARTITION_BY_RANGE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;REBALANCE&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;REBALANCE_BY_SIZE&amp;lt;/code&amp;gt;) set how a query&amp;#039;s output is partitioned, for example to control the number of files written to a [[Delta Lake]] or [[Apache Iceberg]] table. &amp;lt;code&amp;gt;REBALANCE&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;REBALANCE_BY_SIZE&amp;lt;/code&amp;gt; only work with AQE on.&amp;lt;ref name=&amp;quot;hints&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In [[Trino]], the closest equivalent is the &amp;lt;code&amp;gt;join_distribution_type&amp;lt;/code&amp;gt; session property, which chooses between partitioned and broadcast joins for the whole query rather than per table.&amp;lt;ref name=&amp;quot;trinocbo&amp;quot;&amp;gt;{{Cite web |url=https://trino.io/docs/current/optimizer/cost-based-optimizations.html |title=Cost-based optimizations (Trino 484) |publisher=Trino Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Shuffle]]&lt;br /&gt;
* [[Adaptive Query Execution]]&lt;br /&gt;
* [[Broadcast join]]&lt;br /&gt;
* [[Data skew]]&lt;br /&gt;
* [[Broadcast variable]]&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>