A simple, hands-on Databricks notebook that demonstrates broadcast joins in PySpark — a performance optimization technique used when joining a large DataFrame with a much smaller one.
- Creates two sample DataFrames:
orders_df— a set of order records (order_id,customer_id,amount)customers_df— a small lookup table of customers (customer_id,customer_name)
- Performs a regular join between
orders_dfandcustomers_df. - Performs the same join using
broadcast()on the smaller DataFrame. - Uses
.explain(True)to compare the physical execution plans and show how Spark avoids a costly shuffle when broadcasting.
In distributed Spark joins, data usually needs to be shuffled across the cluster so matching keys land on the same node — this is expensive for large datasets.
A broadcast join avoids this by sending a small DataFrame to every executor node in full, so the join happens locally without shuffling the large DataFrame. It's ideal when:
- One DataFrame is small enough to fit in memory on each executor (commonly < ~10MB, tunable via
spark.sql.autoBroadcastJoinThreshold) - You're joining a large fact table with a small dimension/lookup table
- Apache Spark (PySpark)
- Databricks Notebook
- Import this notebook into a Databricks workspace.
- Attach it to a running cluster.
- Run all cells sequentially.
- Check the output of
result.explain(True)to see the difference between the shuffle join and the broadcast join execution plans (look forBroadcastHashJoinvsSortMergeJoin).
├── broadcast_join.py # Databricks source notebook (regular join vs broadcast join)
└── README.md