Apache Spark Big Data (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What are Spark's Join Strategies (Broadcast Hash Join vs Sort-Merge Join vs Shuffle Hash Join)?
senior LevelSystem Design
In Simple Words:
- Broadcast Hash Join (BHJ): If one table is small (< 10 MB default, configurable via
spark.sql.autoBroadcastJoinThreshold), Spark broadcasts the small table to all executors and performs an in-memory hash join with zero shuffle. Fastest join! - Sort-Merge Join (SMJ - Default for large tables): Shuffles both tables on the join key, sorts each partition, and merges matching keys sequentially. Handles arbitrary table sizes without memory overflow.
- Shuffle Hash Join: Shuffles both tables and builds an in-memory hash table for each partition. Used when tables are medium-sized and sorting is expensive.