TECHNOLOGIESTieInn Technologies

Partitions and broadcast joins

Spark / PySpark · 11 questions

Short notes

Key idea

Partitions are the units of parallel work. Too few and executors sit idle; too many tiny ones and overhead dominates. A broadcast join sends a small table to every executor so the big table doesn't have to shuffle.

Syntax

df.rdd.getNumPartitions()
df.repartition(200, "city")     # full shuffle, by key
df.coalesce(10)                 # merge partitions, no full shuffle
spark.conf.set("spark.sql.shuffle.partitions", 200)

from pyspark.sql.functions import broadcast
orders.join(broadcast(stores), "store_id")
  • Spark broadcasts automatically below spark.sql.autoBroadcastJoinThreshold (10 MB by default).

Example

1 billion orders joined to 300 stores: a normal join shuffles both sides. Broadcasting the stores table copies 300 rows to each executor, and the orders never move. Minutes become seconds.

Traps

  • Broadcasting a big table runs the driver or executors out of memory.
  • coalesce(1) before writing squeezes all work into one task.
  • Aim for partitions of about 100–200 MB.
  • Writing with many partitions creates many small files; coalesce sensibly first.

Want the full lesson with step-by-step examples, a project and a certificate? Spark / PySpark course, ₹120 →

Questions

Open in Code Lab5 free questions
5 questions

    Partitions and broadcast joins

  1. Question 1easy

    You want to reduce 2,000 partitions to 20 before writing, with the least data movement. Which?

  2. Partitions and broadcast joins

  3. Question 2medium

    Which join is a good candidate for a broadcast?

  4. Partitions and broadcast joins

  5. Question 3medium

    What does a broadcast join avoid?

  6. Partitions and broadcast joins

  7. Question 4medium

    A daily job writes 5,000 files of 50 KB each. Why is that a problem?

  8. Partitions and broadcast joins

  9. Question 5hard

    Before write.partitionBy("order_date"), why might you call repartition("order_date")?

6 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.