Skip to main content
PYSPARK โ€ข LESSON 173

Reducing Partition Overhead with coalesce()

How do we reduce 200 small partitions down to 4 files before writing without triggering an expensive shuffle?

Expertโฑ 2 Minutesโญ 960 XP
๐Ÿค” THE QUESTION

How do we reduce 200 small partitions down to 4 files before writing without triggering an expensive shuffle?

๐Ÿ’ก WHAT IS IT?

coalesce(numPartitions) merges adjacent partitions on the same worker without performing a full shuffle.

๐ŸŽฏ WHAT IS IT USED FOR?

Preventing the 'small files problem' in data lakes when writing out filtered or aggregated DataFrames.

๐Ÿ’ป EXAMPLE
df_coalesced = df.coalesce(4)

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Reducing Partition Overhead with coalesce().

  • โœ“ Call coalesce() to reduce partition count
  • โœ“ Avoid expensive network shuffle
  • โœ“ Prevent small files problem in lakehouse