The full answer
Neither side fits in a broadcast, so the join will be a shuffle join (normally sort-merge). The work is to shuffle as little data as possible, size the shuffle partitions for about 375 GB, deal with skew, and, if the join runs regularly, store the data so the shuffle can be avoided altogether.
1. Shrink both sides before the join
- Select only the columns needed; columnar formats then read much less than 125 GB and 250 GB.
- Filter as early as possible, including partition filters. If one side is filtered by a small dimension table, dynamic partition pruning can skip partitions of the other at runtime.
- Pre-aggregate if the result only needs totals per key.
- Check for duplicate keys that would multiply rows.
2. Size the shuffle With the default 200 shuffle partitions, each partition would be close to 2 GB, causing spills and memory errors. Aim for about 100–200 MB per partition:
spark.conf.set("spark.sql.adaptive.enabled", "true") # default
spark.conf.set("spark.sql.adaptive.coalescePartitions.initialPartitionNum", "2000") # about 375 GB / 200 MB
spark.conf.set("spark.sql.adaptive.coalescePartitions.parallelismFirst", "false")
AQE then merges partitions that turn out smaller than the target.
3. Handle skew
- AQE's skew join handling (on by default) splits partitions more than 5 times the median and larger than 256 MB.
- Check the top keys; null or placeholder keys (
0,"unknown") are common culprits: filter them and handle them separately. - For extreme hot keys, salt them: add a random number from 0 to n−1 to the key on the larger side and replicate the matching rows n times on the smaller side.
4. Choose the right strategy
from pyspark.sql import functions as F
a = spark.table("sales.events").select("customer_id", "event_ts", "amount").filter("event_ts >= '2026-01-01'")
b = spark.table("crm.interactions").select("customer_id", "channel", "score")
joined = a.join(b, "customer_id") # sort-merge join, tuned by AQE
- If after filtering one side becomes small (tens to a few hundred MB), broadcast it with
F.broadcast(...), as long as executors have the memory. SHUFFLE_HASHcan beat sort-merge when one side's partitions comfortably fit in memory, since it avoids sorting.
5. For repeated joins: avoid the shuffle
- Bucketing: write both tables bucketed on
customer_idwith the same number of buckets; Spark can then join them without shuffling either side. - Storage-partitioned joins with table formats such as Iceberg do the same with partition transforms.
- On Databricks, liquid clustering on the join key improves data skipping and file layout.
6. Cluster resources The shuffle writes several hundred GB to local disk, so use workers with fast local SSDs and enough memory per core (memory-optimised instances help). Enough total cores to run the 2,000 tasks in a few waves keeps the run time down.
7. Verify In the Spark UI, check the join type in the SQL plan, shuffle read and write sizes, spill, and the spread of task durations. Compare before and after each change.
Last updated 16 October 2026
Sources