<?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=Broadcast_join</id>
	<title>Broadcast 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=Broadcast_join"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Broadcast_join&amp;action=history"/>
	<updated>2026-12-06T00:08:15Z</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=Broadcast_join&amp;diff=126&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=Broadcast_join&amp;diff=126&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:34Z</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            = Broadcast join&lt;br /&gt;
| also_known_as   = Broadcast hash join, map-side join, &amp;lt;code&amp;gt;BroadcastHashJoin&amp;lt;/code&amp;gt;&lt;br /&gt;
| field           = Distributed query processing&lt;br /&gt;
| key_parts       = Build side (small table), stream side (large table), &amp;lt;code&amp;gt;BroadcastExchange&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; hint&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Sort-merge join]], [[Shuffle hash join]], [[Shuffle]], [[Adaptive Query Execution]], [[Driver (Apache Spark)]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/sql-performance-tuning.md, docs/sql-ref-syntax-qry-select-hints.md, SQLConf.scala, SparkStrategies.scala, joins.scala, BroadcastExchangeExec.scala, QueryExecutionErrors.scala) and EXPLAIN output and configuration defaults from a PySpark 4.3.0 run}}&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;broadcast join&amp;#039;&amp;#039;&amp;#039; is a join strategy in which the smaller table is copied in full to every [[executor]], so that each task can join its part of the larger table locally, without a [[shuffle]] of the larger table.&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; In [[Apache Spark]] it appears in plans as &amp;lt;code&amp;gt;BroadcastHashJoin&amp;lt;/code&amp;gt; (for equality joins) or &amp;lt;code&amp;gt;BroadcastNestedLoopJoin&amp;lt;/code&amp;gt; (for other conditions), fed by a &amp;lt;code&amp;gt;BroadcastExchange&amp;lt;/code&amp;gt;. Spark picks it automatically when one side is estimated below &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; (default 10 MB). It is usually the fastest join when one side is small, and the most common cause of driver memory errors when that side is larger than expected.&lt;br /&gt;
&lt;br /&gt;
== How does a broadcast join work in Spark? ==&lt;br /&gt;
A broadcast hash join in Spark SQL has three steps, all visible in the Spark 4.3.0 source:&amp;lt;ref name=&amp;quot;bex&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;&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Collect.&amp;#039;&amp;#039;&amp;#039; Spark runs the small side (the &amp;#039;&amp;#039;&amp;#039;build side&amp;#039;&amp;#039;&amp;#039;) and collects its rows to the [[Driver (Apache Spark)|driver]].&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Build.&amp;#039;&amp;#039;&amp;#039; The driver builds a hash table (a &amp;lt;code&amp;gt;HashedRelation&amp;lt;/code&amp;gt;) keyed on the join columns.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Broadcast.&amp;#039;&amp;#039;&amp;#039; The driver broadcasts the hash table to the executors. Each task of the large side (the &amp;#039;&amp;#039;&amp;#039;stream side&amp;#039;&amp;#039;&amp;#039;) probes the table for each of its rows.&lt;br /&gt;
&lt;br /&gt;
In a physical plan from PySpark 4.3.0, a join of a 1,000,000-row range with a 100-row range looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
*(2) BroadcastHashJoin [k#9L], [k#11L], Inner, BuildRight, false, false&lt;br /&gt;
:- *(2) Project [id#8L AS k#9L]&lt;br /&gt;
:  +- *(2) Range (0, 1000000, step=1, splits=2)&lt;br /&gt;
+- BroadcastExchange HashedRelationBroadcastMode(List(input[0, bigint, false]),false)&lt;br /&gt;
   +- *(1) Project [id#10L AS k#11L]&lt;br /&gt;
      +- *(1) Range (0, 100, step=1, splits=2)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;BuildRight&amp;lt;/code&amp;gt; means the right input is the broadcast side. There is no &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; on the large side, so the scan, the join and any later narrow operators run in one stage (codegen stage 2 above).&lt;br /&gt;
&lt;br /&gt;
== When does Spark choose a broadcast join? ==&lt;br /&gt;
For an equality join with no hint, Spark&amp;#039;s join selection picks a broadcast hash join first, if one side is small enough to broadcast and the join type allows that side to be the build side. If both sides are small, Spark broadcasts the smaller one.&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; &amp;quot;Small enough&amp;quot; means an estimated size in bytes at or below the threshold.&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;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Setting !! Default (4.3.0) !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; || 10 MB (10485760 bytes) || Maximum estimated size of a table to broadcast automatically; -1 disables automatic broadcast&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.adaptive.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; || (none; same as the above) || The threshold used by [[Adaptive Query Execution]] with runtime sizes&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.broadcastTimeout&amp;lt;/code&amp;gt; || 300 seconds || How long to wait for the broadcast to complete&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.maxBroadcastTableSize&amp;lt;/code&amp;gt; || 8 GiB || Hard limit on the built table size; above it the query fails (since 4.1.0)&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;
&lt;br /&gt;
The decision uses estimated sizes. For a table without statistics, Spark assumes &amp;lt;code&amp;gt;Long.MaxValue&amp;lt;/code&amp;gt; bytes and does not broadcast it unless it knows the table is small.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt; &amp;lt;code&amp;gt;ANALYZE TABLE&amp;lt;/code&amp;gt; and the [[Cost-based optimizer (Spark)|cost-based optimizer]] improve these estimates. If the planner did not choose a broadcast join, [[Adaptive Query Execution]] can still switch a sort-merge join to a broadcast join once a side&amp;#039;s runtime size is below the adaptive threshold, although the Spark documentation notes this is less efficient than planning it from the start.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The build side must be the side whose unmatched rows do not need to be returned. For a left outer join, Spark can broadcast only the right side; for a right outer join, only the left side.&amp;lt;ref name=&amp;quot;joins&amp;quot; /&amp;gt; A full outer join cannot use a broadcast hash join.&amp;lt;ref name=&amp;quot;joins&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Broadcast hints ==&lt;br /&gt;
A hint asks Spark to broadcast a relation even when its estimated size is above the threshold. In SQL, &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;BROADCASTJOIN&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MAPJOIN&amp;lt;/code&amp;gt; are accepted; in the DataFrame API, &amp;lt;code&amp;gt;hint(&amp;quot;broadcast&amp;quot;)&amp;lt;/code&amp;gt; or the &amp;lt;code&amp;gt;broadcast()&amp;lt;/code&amp;gt; function.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&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 /*+ BROADCAST(r) */ *&lt;br /&gt;
FROM sales s JOIN regions r ON s.region_id = r.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;
sales.join(broadcast(regions), sales.region_id == regions.id)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When both sides of a join carry hints, &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; takes priority over &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;. Spark does not guarantee that the hinted strategy is used, because a strategy may not support the join type.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt; A hint also overrides &amp;lt;code&amp;gt;autoBroadcastJoinThreshold=-1&amp;lt;/code&amp;gt;: in PySpark 4.3.0, a join with &amp;lt;code&amp;gt;hint(&amp;quot;broadcast&amp;quot;)&amp;lt;/code&amp;gt; was planned as &amp;lt;code&amp;gt;BroadcastHashJoin&amp;lt;/code&amp;gt; with automatic broadcast disabled.&lt;br /&gt;
&lt;br /&gt;
== When does broadcasting backfire? ==&lt;br /&gt;
A broadcast join moves the whole build side through the driver and into every executor, so its cost grows with the build side and with the cluster size. The problems this causes are documented in Spark&amp;#039;s source and settings:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Driver out of memory.&amp;#039;&amp;#039;&amp;#039; The build side is collected and the hash table is built on the driver.&amp;lt;ref name=&amp;quot;bex&amp;quot; /&amp;gt; If that runs out of memory, Spark fails the query with an error that it does not have enough memory to build and broadcast the table, and suggests disabling broadcast by setting &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; to -1 or increasing &amp;lt;code&amp;gt;spark.driver.memory&amp;lt;/code&amp;gt;; for file tables it also suggests running &amp;lt;code&amp;gt;ANALYZE TABLE&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;qee&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/errors/QueryExecutionErrors.scala |title=QueryExecutionErrors.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;Hard limits.&amp;#039;&amp;#039;&amp;#039; A broadcast fails if the built table reaches &amp;lt;code&amp;gt;spark.sql.maxBroadcastTableSize&amp;lt;/code&amp;gt; (8 GiB), or if the collected side has too many rows: 512,000,000 in general, and a lower limit derived from the hash map&amp;#039;s maximum capacity when the join key is not a single &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; column.&amp;lt;ref name=&amp;quot;bex&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Timeouts.&amp;#039;&amp;#039;&amp;#039; If building and sending the table takes longer than &amp;lt;code&amp;gt;spark.sql.broadcastTimeout&amp;lt;/code&amp;gt; (300 seconds), the query fails.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Wrong estimates.&amp;#039;&amp;#039;&amp;#039; A table that looks small to the planner, for example after a filter whose selectivity is overestimated, or a forced &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; hint on a table that has grown, can be far larger at run time.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Executor memory.&amp;#039;&amp;#039;&amp;#039; Every executor holds a copy of the table for the duration of the join, in addition to its own work.&lt;br /&gt;
&lt;br /&gt;
The usual fixes are to remove the hint, lower &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; or set it to -1, filter the build side before the join, or give the driver more memory.&amp;lt;ref name=&amp;quot;qee&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Broadcast join compared with the other join strategies ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Broadcast hash join !! [[Sort-merge join]] !! [[Shuffle hash join]]&lt;br /&gt;
|-&lt;br /&gt;
! Shuffles&lt;br /&gt;
| None on the large side; the small side goes through the driver&lt;br /&gt;
| Both sides&lt;br /&gt;
| Both sides&lt;br /&gt;
|-&lt;br /&gt;
! Memory need&lt;br /&gt;
| Whole build side in the driver and in every executor&lt;br /&gt;
| Low; sorts spill to disk&lt;br /&gt;
| Build side of one partition in one task&amp;#039;s memory&lt;br /&gt;
|-&lt;br /&gt;
! Chosen by default when&lt;br /&gt;
| One side is below &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt;&lt;br /&gt;
| No side is small enough to broadcast and the join keys are sortable&lt;br /&gt;
| Only when &amp;lt;code&amp;gt;spark.sql.join.preferSortMergeJoin&amp;lt;/code&amp;gt; is false, or by hint or AQE&lt;br /&gt;
|-&lt;br /&gt;
! Full outer join&lt;br /&gt;
| Not supported&lt;br /&gt;
| Supported&lt;br /&gt;
| Supported&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Sources: SparkStrategies.scala and joins.scala at v4.3.0.&amp;lt;ref name=&amp;quot;strategies&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;joins&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Sort-merge join]]&lt;br /&gt;
* [[Shuffle hash join]]&lt;br /&gt;
* [[Adaptive Query Execution]]&lt;br /&gt;
* [[Driver (Apache Spark)]]&lt;br /&gt;
* [[Shuffle]]&lt;br /&gt;
* [[Cost-based optimizer (Spark)]]&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>