QuestionQ229

VerifiedData Processing & Transformations

A data engineer needs to join a very large events_df (containing billions of rows) with a small country_lookup_df (fewer than 10,000 rows) using the country_code column. The join is currently slow because of data shuffling.

What should the data engineer do to improve the performance of this join?

Explanation

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.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!