Sub topic 11
Choosing row group size and codec
This chapter assumes you know how a Parquet file is split into row groups, column chunks, and pages. It also assumes you know that the footer stores min, max, and null count statistics for each column chunk. Here you will use that knowledge to pick two write-time settings: the row group size and the compression codec.
Row group size trade-offs
A row group is the unit that readers skip and that engines process in parallel. Its size sets a balance between two costs.
| Larger row groups | Smaller row groups |
|---|---|
| Longer sequential reads per column chunk | More footer metadata to parse |
| Better compression and dictionary reuse | Finer skipping, because statistics cover fewer rows |
| Higher writer memory, since a whole group is buffered | Lower writer memory |
| Less parallelism on small files | More parallel tasks |
Writers buffer one full row group before flushing it, so memory use grows with group size and column count. Very small groups produce many tiny column chunks. These add footer overhead and cause many small reads on object storage.
A common starting point is 128 MB to 1 GB of uncompressed data per row group, or a few hundred thousand to a few million rows. PyArrow's row_group_size counts rows, not bytes. Estimate the row count from your average row width.
Skipping also depends on data order. If id is sorted, each row group covers a narrow min/max range and many groups can be skipped. If values are random, every group's range spans nearly everything and nothing is skipped, whatever the group size.
Codec trade-offs
| Codec | Size | Speed | Typical use |
|---|---|---|---|
| none | Largest | Fastest | Local scratch data, very fast storage |
| Snappy | Moderate | Very fast | Frequently read data, low CPU budget |
| Zstandard | Small | Fast, tunable by level | General default for analytics |
| Gzip | Small | Slow to write and read | Compatibility with older tools |
Compression happens per page, after encoding. Columns with few distinct values are already shrunk by dictionary and run-length encoding, so the codec adds less for them.
Measuring on your own data
Test with a sample of real data, since the results depend on it. This script writes the same table with several settings and reports file size and write time.
import os
import time
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"id": list(range(1_000_000)),
"category": ["a", "b", "c", "d"] * 250_000,
})
for codec in ["none", "snappy", "zstd", "gzip"]:
for group_rows in [50_000, 500_000]:
path = f"events_{codec}_{group_rows}.parquet"
start = time.perf_counter()
pq.write_table(
table, path, compression=codec, row_group_size=group_rows
)
elapsed = time.perf_counter() - start
md = pq.ParquetFile(path).metadata
print(
f"{codec:7} rows/group={group_rows:7} "
f"groups={md.num_row_groups:3} "
f"bytes={os.path.getsize(path):9} "
f"write={elapsed:.2f}s"
)Also time a typical read, such as a filtered pq.read_table call, because decompression cost falls on every query.
Common mistakes
- Writing many small files, each with one tiny row group. Compact them instead of lowering
row_group_size. - Choosing Gzip for data that is read often. Decompression time can outweigh the storage savings.
- Tuning on synthetic data. Repetitive test data compresses far better than real data.
Exercise
A table of 400 million rows is filtered by event_date on most queries, but the files are written in random order. A teammate proposes cutting the row group size from 1,000,000 rows to 100,000 rows to skip more data. Why might this not help, and what would you try first?
Show answer
With random order, each row group's min/max for event_date spans nearly the whole date range, so statistics cannot rule out any group, whatever its size. Smaller groups would only add footer metadata and small reads. Try sorting or partitioning the data by event_date before writing, then check how many row groups a filtered read skips.