Join strategy hints
| Also known as | Join hints (Spark SQL) |
|---|---|
| Field | Query optimization |
| Introduced | BROADCAST before Spark 3.0; MERGE, SHUFFLE_HASH and SHUFFLE_REPLICATE_NL in Apache Spark 3.0.0
|
| Key parts | BROADCAST, MERGE, SHUFFLE_HASH, SHUFFLE_REPLICATE_NL
|
| Open source implementations | Apache Spark |
| Related | Shuffle, Adaptive Query Execution, Data skew, Broadcast join |
In Apache Spark SQL, join strategy hints are comments of the form /*+ HINT(table) */, or DataFrame.hint() calls, that ask the planner to use a specific physical join algorithm for a relation instead of the one it would pick from statistics.[1] Spark supports four: BROADCAST, MERGE, SHUFFLE_HASH and SHUFFLE_REPLICATE_NL. A hint is a suggestion, not a command: Spark ignores it when the hinted strategy does not support the join type.[1]
Which join hints does Spark support?
Before Spark 3.0 only the BROADCAST hint existed. Spark 3.0.0 added MERGE, SHUFFLE_HASH and SHUFFLE_REPLICATE_NL, one for each of Spark's other join strategies.[1][2]
| Hint | Aliases | Physical join it asks for | Notes |
|---|---|---|---|
BROADCAST
|
BROADCASTJOIN, MAPJOIN
|
Broadcast hash join (with equi-join keys) or broadcast nested loop join (without) | The hinted side is broadcast even if it is larger than spark.sql.autoBroadcastJoinThreshold (default 10 MB). If both sides are hinted, the smaller one by statistics is broadcast.[1][3]
|
MERGE
|
SHUFFLE_MERGE, MERGEJOIN
|
Shuffle sort-merge join | Needs sortable join keys.[4] |
SHUFFLE_HASH
|
none | Shuffle hash join | If both sides are hinted, the smaller side by statistics becomes the build side.[1] |
SHUFFLE_REPLICATE_NL
|
none | Shuffle-and-replicate nested loop join (cartesian product) | Used only for inner-like joins.[4] |
The BROADCAST hint is still bounded by hard limits: Spark fails a broadcast whose table is 8 GiB or larger (spark.sql.maxBroadcastTableSize, 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 LongType column it is BytesToBytesMap.MAX_CAPACITY / 1.5, that is 229 / 1.5 or 357,913,941 rows, and in other cases it is 512,000,000 rows.[5][6][7]
How to write a join hint
In Spark SQL the hint goes in a comment right after SELECT and names the table or alias it applies to. In PySpark the same hint is set with hint() on the DataFrame that should get the strategy, or with broadcast() from pyspark.sql.functions.[3]
SELECT /*+ BROADCAST(d) */ f.order_id, d.region
FROM sales f JOIN dim_store d ON f.store_id = d.store_id;
SELECT /*+ SHUFFLE_HASH(c) */ * FROM orders o JOIN customers c ON o.cust_id = c.cust_id;
from pyspark.sql.functions import broadcast
orders.join(broadcast(stores), "store_id")
orders.join(customers.hint("merge"), "cust_id")
Which hint wins when several are given?
When the two sides of a join carry different strategy hints, Spark prefers BROADCAST over MERGE over SHUFFLE_HASH over SHUFFLE_REPLICATE_NL, and logs a warning that the overridden hint will not take effect.[1]
Spark'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.[4] A MERGE or SHUFFLE_HASH hint on a join without equi-join keys cannot be honored, and Spark reports it as unsupported.[4]
How do join hints interact with Adaptive Query Execution?
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.[3]
- Sort-merge join to broadcast join. AQE turns a sort-merge join into a broadcast hash join when a side's runtime size is under
spark.sql.adaptive.autoBroadcastJoinThreshold(which defaults tospark.sql.autoBroadcastJoinThreshold).[3] AQE also has a rule that marks a side with many empty partitions as "do not broadcast", but the rule leaves a side alone if the user already gave it a strategy hint.[8] - Sort-merge join to shuffled hash join. AQE can switch a sort-merge join to a shuffled hash join when every partition is under
spark.sql.adaptive.maxShuffledHashJoinLocalMapThreshold(default 0, so off). It skips joins that carry aMERGEhint.[3][9] - Skew join splitting. AQE splits skewed partitions of sort-merge and shuffled hash joins, whichever strategy chose them, so a
MERGEorSHUFFLE_HASHhint does not turn off skew handling.[10] See data skew.
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.[3]
Join hints compared with partitioning hints
Spark SQL has a second family of hints that does not pick a join algorithm. Partitioning hints (COALESCE, REPARTITION, REPARTITION_BY_RANGE, REBALANCE and REBALANCE_BY_SIZE) set how a query's output is partitioned, for example to control the number of files written to a Delta Lake or Apache Iceberg table. REBALANCE and REBALANCE_BY_SIZE only work with AQE on.[1]
In Trino, the closest equivalent is the join_distribution_type session property, which chooses between partitioned and broadcast joins for the whole query rather than per table.[11]
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 1.5 1.6 Hints (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SPARK-27225: Implement join strategy hints. Apache Software Foundation JIRA.Retrieved 2026-11-23.
- ↑ 3.0 3.1 3.2 3.3 3.4 3.5 Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 4.0 4.1 4.2 4.3 SparkStrategies.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ BytesToBytesMap.java (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ BroadcastExchangeExec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ DemoteBroadcastHashJoin.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ ConvertSortMergeJoinToShuffledHashJoin.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ OptimizeSkewedJoin.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Cost-based optimizations (Trino 484). Trino Software Foundation.Retrieved 2026-11-23.