Arman Pouyaei Start a conversation
Lesson 15  ·  Oct 22, 2026
Data Engineering Fundamentals

Only read the folders you need: partitioning

Yesterday’s table was a folder of Parquet files. Fine at gigabytes — but lakes hold terabytes, and a query that only needs one region and one month shouldn’t have to open every file in the folder. Partitioning organizes the folder into subfolders by the values of a column you filter on, using Hive-style folder names like region=us-northeast/month=2026-10/. When a query’s filter matches, the engine reads only the matching folders and skips the rest entirely — partition pruning.

The code

# write: one folder per region/month combo
df.write.partitionBy("region", "month").mode("append") \
  .parquet("s3://storm-lake/readings/")

# the lake on disk afterwards (Hive-style layout)
# readings/
#   region=us-northeast/month=2026-10/part-00000.parquet
#   region=us-northeast/month=2026-09/part-00000.parquet
#   region=us-southeast/month=2026-10/part-00000.parquet
#   ...

The partitionBy line does all the organizing: PySpark groups the rows by region and month and writes each combination into its own folder, named column=value. No manual file shuffling — you name the columns, the layout follows.

SELECT AVG(temp_c) FROM readings
WHERE region = 'us-northeast' AND month = '2026-10';

And here is the payoff. The query names the same two columns in its WHERE, so the engine lists only readings/region=us-northeast/month=2026-10/ and never opens the other ~119 folders. That is partition pruning — the bigger sibling of Lesson 14’s column pruning: skip columns you don’t need, and skip folders you don’t need.

In practice

Three rules of thumb cover most real lakes. Partition by the column you filter on most — region, month, station_id. Aim for dozens-to-hundreds of distinct values per partition column, and roughly 100MB–1GB of data per partition. And beware the failure mode at the other extreme: partition by something with millions of values (a raw timestamp, a sensor ID) and you get the small-files problem — millions of tiny files that make listing and planning slower than the scan you were trying to avoid.

The geospatial version of this is everywhere in your world: partition by region or grid cell, so a query over New Jersey never scans the whole country. The small-files problem is also exactly why plain folders aren’t the end of the story — table formats like Delta Lake and Iceberg add a metadata layer on top of this same layout. That’s tomorrow’s lesson.

Python bridge: partitionBy is groupby turned into folders. In pandas you’d loop for (region, month), part in df.groupby(["region", "month"]) and write each group to its own path by hand; PySpark does that automatically — and the WHERE filter then behaves like reading back only the groups you ask for.

Further reading

Data Lake Partitioning — RisingWave glossary (a short, clear rundown of partition pruning, partition design, and the trade-offs)

Try it

Practice question: your storm readings table has columns station_id, temp_c, humidity, region, month, spread across regions and months in the lake. (1) Write the PySpark line that saves df to s3://storm-lake/readings/, partitioned by region and month. (2) Write the SQL that gets AVG(temp_c) for us-northeast in 2026-10 — and name the single folder the engine actually reads.

Show answer
df.write.partitionBy("region", "month").mode("append").parquet("s3://storm-lake/readings/")

SELECT AVG(temp_c) FROM readings
WHERE region = 'us-northeast' AND month = '2026-10';

Result: the engine reads only readings/region=us-northeast/month=2026-10/ — ~119 of 120 folders skipped. That is partition pruning: the WHERE filter matches the folder layout, so everything outside the matching folder is never opened.

← All posts