Sub topic 12
Choosing join strategies
This chapter assumes you can read a physical plan with EXPLAIN and know what a shuffle is. It also assumes you have run ANALYZE TABLE at least once. If not, review the earlier chapters on query plans and statistics first.
Why the join strategy matters
A join is often the most expensive step in a query. Spark picks a physical strategy for each join, and the choice decides how much data crosses the network. The three you will meet most often are:
- Broadcast hash join. Spark sends the small side to every executor. The large side is not shuffled.
- Shuffle hash join. Both sides are shuffled by the join key, then one side is built into a hash table in each task.
- Sort-merge join. Both sides are shuffled and sorted by the join key, then merged. This is the usual choice for two large tables.
Broadcast joins are usually the fastest. They avoid the shuffle of the large table, which is typically the dominant cost.
How Spark decides
The optimizer compares the estimated size of each side with a threshold. The setting is spark.sql.autoBroadcastJoinThreshold, and its default is 10 MB. If a side is estimated below the threshold, Spark broadcasts it.
The estimate comes from table and column statistics. This is why stale or missing statistics cause bad joins. With no statistics, Spark may assume a table is large and shuffle it. It may also underestimate a table that has grown, and try to broadcast too much data.
Run ANALYZE TABLE so the estimates are close to reality:
ANALYZE TABLE customers COMPUTE STATISTICS FOR ALL COLUMNS;Forcing a broadcast with a hint
When you know a table is small but Spark does not, add a hint. The BROADCAST hint names the table alias to send to every executor.
SELECT /*+ BROADCAST(c) */ c.region, SUM(o.amount) AS total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
GROUP BY c.region;A hint overrides the size threshold but not the limits of memory. Broadcasting a table that does not fit in executor memory can fail the query with an out-of-memory error. Hint only tables you know are small and will stay small.
Verifying the choice
Never assume the hint worked. Run EXPLAIN FORMATTED and look for the join operator:
BroadcastHashJoinmeans the broadcast happened.SortMergeJoinorShuffledHashJoinmeans both sides were shuffled.
In the Query Profile, check the join node for rows and time. A broadcast join should show no shuffle on the large table's side.
Common errors
- Hinting the wrong alias. The hint uses the alias (
c), not the table name. A misspelled alias is ignored without an error. - Broadcasting a large table. This causes memory pressure or driver failures.
- Trusting old statistics. After a big load, rerun
ANALYZE TABLE. - Joining on mismatched types. A type cast on the key can block efficient strategies. Make both keys the same type.
Exercise
A query joins a 2 TB orders table to a 5 MB customers table, but EXPLAIN FORMATTED shows SortMergeJoin. Name two likely causes and one fix.
Show answer
Likely causes are missing or stale statistics, so Spark misjudges the size of customers, and a join key with a mismatched type or an expression that blocks the broadcast. The fix is to run ANALYZE TABLE customers COMPUTE STATISTICS FOR ALL COLUMNS, or add /*+ BROADCAST(c) */. Then confirm that EXPLAIN FORMATTED shows BroadcastHashJoin.