Sub topic 15
Choosing partition keys and avoiding small files
This chapter assumes you can write and read a Hive-partitioned dataset with ds.write_dataset and ds.dataset. It also assumes you know that a filter on a partition column lets PyArrow skip whole directories.
What makes a good partition key
Every distinct value of a partition column becomes a directory. Each directory holds at least one Parquet file. The key you choose therefore decides how many files you create and how big each one is.
A good key has these properties:
- Low to moderate cardinality. Cardinality is the number of distinct values. Dozens or hundreds of values work well. Millions do not.
- Used in filters. Pruning only helps if queries filter on the column. Partitioning by
countrydoes nothing for a query that filters oncustomer_id. - Even distribution. If one value holds 90% of the rows, that directory is still huge and the others are tiny.
- Stable. Values such as dates or regions rarely change. Values that change force rewrites.
| Candidate key | Cardinality | Verdict |
|---|---|---|
| `year` | Few | Good for time-based queries |
| `year`, `month` | Tens to hundreds | Good for event data |
| `country` | Around 200 | Good if queries filter by country |
| `user_id` | Millions | Poor, creates one tiny file per value |
| `timestamp` | Nearly unique | Poor, store as a normal column |
The small-files problem
Each file has fixed costs: listing it, opening it, and reading its footer metadata. On remote storage such as S3, each of these is a network request. Thousands of files that hold a few kilobytes each make a scan slow, even when the total data is small. Small files also give Parquet little room for compression and row group statistics.
A common target is files of roughly 100 MB to 1 GB, with row groups of about 100,000 to 1,000,000 rows. Treat these as starting points and measure on your own data.
Seeing the effect
The code below writes the same table twice. It uses year as the key the first time and user_id the second time. The second run sets max_partitions higher because write_dataset stops with an error by default when it would create more than 1024 partitions.
import pyarrow as pa
import pyarrow.dataset as ds
n = 2000
table = pa.table({
"user_id": list(range(n)),
"year": [2023 + (i % 2) for i in range(n)],
"amount": [float(i) for i in range(n)],
})
def write_and_count(path, keys):
ds.write_dataset(
table,
path,
format="parquet",
partitioning=keys,
partitioning_flavor="hive",
existing_data_behavior="overwrite_or_ignore",
max_partitions=5000,
)
dataset = ds.dataset(path, format="parquet", partitioning="hive")
return len(dataset.files)
print("by year: ", write_and_count("by_year", ["year"]))
print("by user_id:", write_and_count("by_user", ["user_id"]))The first call produces 2 files of 1000 rows each. The second produces 2000 files of one row each. The data is identical.
Fixing a bad layout
- Drop the high-cardinality column from the partition keys and keep it as a regular column. Parquet row group statistics can still skip data for filters on it.
- Use a coarser key. Replace a full date with
yearandmonth. - Write fewer, larger files by setting
max_rows_per_fileandmin_rows_per_group. - Compact existing small files by reading them with a scanner and writing the result to a new location.
Exercise
A table of web events has 50 million rows and a session_id column with about 8 million distinct values. Queries almost always filter on session_id and on the event date. Which partition keys would you choose, and what would you do about session_id?
Show answer
Partition by a coarse date key such as year and month, or by day if each day holds enough data for files in the 100 MB range. Keep session_id as a regular column rather than a partition key, because 8 million directories would create millions of tiny files. Sorting the rows by session_id within each partition before writing gives row groups tight min/max statistics, so filters on it can still skip row groups.