When one side of a join is small enough to fit in memory, Spark can perform a broadcast join by sending a copy of the small DataFrame to every executor, eliminating the need to shuffle the large DataFrame across the cluster. Since country_lookup_df has fewer than 10,000 rows, it is the ideal candidate to broadcast using the broadcast() hint (e.g., events_df.join(broadcast(country_lookup_df), "country_code")), which converts the operation into a broadcast hash join and avoids the costly shuffle exchange that occurs with a standard sort-merge join. Broadcasting the billions-row events_df would be impractical and counterproductive, and simply caching or persisting the DataFrames does not remove the shuffle step inherent in a non-broadcast join.
Community Discussion