As asked
You have a Spark job joining a 10TB fact table with a 200MB dimension table and the query plan shows a sort-merge join. Explain why a broadcast hash join would be more efficient here, how Spark decides which join strategy to use, and how you would force the broadcast join if Spark does not choose it automatically.
Sample answer outline
A broadcast join sends the entire small table to every executor, avoiding a shuffle of the large table entirely. Spark chooses broadcast joins when the smaller table is below spark.sql.autoBroadcastJoinThreshold (default 10MB). At 200MB, Databricks may not broadcast automatically. The candidate should mention the broadcast() hint in DataFrame API or SQL: SELECT /*+ BROADCAST(dim) */ ... or df.join(broadcast(dim_df), ...). They should note the risk: broadcasting too large a table causes OOM on executors. The tradeoff between shuffle cost (sort-merge) and memory cost (broadcast) should be explicit.
Expect these follow-ups
- How do you diagnose in the Spark UI whether a join became a sort-merge instead of a broadcast?
- What happens if you broadcast a table that is too large for executor memory?