Sub topic 11
Choosing row group size and codec
This chapter covers the two settings that most affect how a Parquet file behaves: the row group size and the compression codec. It assumes you know the file layout from earlier chapters (row groups, column chunks, pages, footer). It also assumes you know that min/max statistics let a reader skip row groups, and that you can run PyArrow code.
What the two settings control
The row group size is the number of rows written before the writer starts a new row group. It sets the unit of skipping and parallel reading, the memory the writer needs, and the size of the footer.
The codec is the algorithm that compresses each page after encoding. It sets file size, CPU cost on write, and CPU cost on read.
The two settings interact. Compression works on pages inside column chunks, so a very small row group gives each column chunk less data to compress and gives dictionary encoding fewer repeated values to exploit. A large row group compresses better but gives statistics a coarser resolution.
Row group size
In PyArrow, row_group_size counts rows, not bytes. The same row count produces very different byte sizes for a table of three integers and a table of fifty string columns. Judge the setting by the resulting row group size in bytes, which you can read from the footer.
| Row group size | Effect on reads | Effect on writes | Effect on footer |
|---|---|---|---|
| Small (thousands of rows) | Fine-grained skipping, but many small reads and more per-chunk overhead | Low memory use | Large: one entry per column per row group |
| Medium (about 100,000 to 1 million rows) | Good balance of skipping and sequential I/O | Moderate memory use | Small |
| Large (millions of rows) | Better compression and fewer seeks, but a skipped group saves more and a missed skip costs more | The writer buffers a whole row group per column | Smallest |
Four effects drive the choice:
- Skipping granularity. A reader decides per row group whether to read it. With 10 groups, a filter that matches one narrow range still reads about one tenth of the file. With 1,000 groups it reads far less, if the data is ordered so that statistics are tight.
- I/O efficiency. Each column chunk read is at least one request. On object storage such as S3, many tiny requests cost more than reading a bit of extra data. Row groups of roughly 128 MB to 1 GB on disk are common for analytics on object stores. Local SSD workloads tolerate smaller groups.
- Parallelism. Engines usually assign row groups to worker threads. A file with one huge row group cannot be read in parallel, and a directory of few files with few groups under-uses a cluster.
- Writer memory. The writer holds the current row group's data in memory until it flushes it. Wide tables with big row groups can exhaust memory.
Footer size matters more than it seems. A reader must fetch and parse the whole footer before reading any data. Ten thousand row groups across 200 columns means two million column chunk entries to parse.
Codec trade-offs
Parquet supports several codecs. The ones you will meet most often:
| Codec | Compression ratio | Speed | Typical use |
|---|---|---|---|
| none | None | Fastest | Data that is already compressed, or local scratch files |
| snappy | Modest | Very fast | Default in many tools, hot data read often |
| lz4 | Modest | Very fast | Similar to Snappy, where supported by all readers |
| zstd | Good, tunable by level | Fast at low levels, slower at high levels | General-purpose choice for most analytics tables |
| gzip | Good | Slow, especially to write | Compatibility with older tools, cold archives |
| brotli | Very good | Slow | Rarely worth the cost; reader support varies |
Zstandard exposes a compression level. Low levels (1 to 3) cost little more than Snappy and compress noticeably better. High levels (15 and up) cost a lot of write time for small additional gains. Reads stay fast at any level, because decompression speed barely depends on the level. This asymmetry suits data written once and read many times.
Encodings do much of the work before the codec runs. A dictionary-encoded column of four categories is already tiny, so the codec has little left to remove. A column of high-entropy values, such as random floats or hashes, barely compresses with any codec, and a heavier codec only wastes CPU.
Measuring both settings
Do not guess. This script builds a table, writes it with different row group sizes, and reports file size, number of row groups, and the time for a selective read.
import os
import time
import pyarrow as pa
import pyarrow.parquet as pq
n = 2_000_000
table = pa.table({
"id": pa.array(range(n)),
"category": pa.array(["a", "b", "c", "d"] * (n // 4)),
"value": pa.array([(i * 7919) % 10007 for i in range(n)]),
})
for rg_size in [10_000, 100_000, 1_000_000]:
path = f"rg_{rg_size}.parquet"
pq.write_table(table, path, row_group_size=rg_size, compression="zstd")
start = time.perf_counter()
result = pq.read_table(
path,
columns=["id"],
filters=[("id", ">=", n - 50_000)],
)
elapsed = time.perf_counter() - start
md = pq.ParquetFile(path).metadata
print(
f"rows/group={rg_size:>9,} groups={md.num_row_groups:>4} "
f"bytes={os.path.getsize(path):>10,} "
f"matched={result.num_rows:,} read={elapsed * 1000:.1f} ms"
)Expect the smallest row group size to produce the largest file and the largest footer. Timings on a local file with a warm cache will be noisy and small, so treat them as a rough guide. The effect is larger on remote storage.
Now compare codecs on one row group size. The compression_level argument applies to codecs that support levels, and None uses the codec's default.
import os
import time
import pyarrow as pa
import pyarrow.parquet as pq
n = 2_000_000
table = pa.table({
"id": pa.array(range(n)),
"category": pa.array(["a", "b", "c", "d"] * (n // 4)),
"value": pa.array([(i * 7919) % 10007 for i in range(n)]),
})
settings = [
("none", None),
("snappy", None),
("lz4", None),
("zstd", 1),
("zstd", 3),
("zstd", 15),
("gzip", 6),
]
for codec, level in settings:
path = f"codec_{codec}_{level}.parquet"
start = time.perf_counter()
pq.write_table(
table,
path,
row_group_size=500_000,
compression=codec,
compression_level=level,
)
write_s = time.perf_counter() - start
start = time.perf_counter()
pq.read_table(path)
read_s = time.perf_counter() - start
print(
f"{codec:<7} level={str(level):<5} "
f"bytes={os.path.getsize(path):>10,} "
f"write={write_s:.2f}s read={read_s:.2f}s"
)Look at two things in the output: how much smaller each codec is than none, and how little the read time changes between zstd levels while the write time grows.
Making the decision
The right setting depends on how the data is used.
Some typical starting points:
- Batch analytics on object storage. Use row groups of about 128 MB to 512 MB on disk,
zstdat level 3, and sort by the most common filter column. - Frequent small appends or streaming output. Use
snappyorlz4, accept smaller row groups, and compact into larger files on a schedule. - Cold archives. Use
zstdat a high level. The write cost is paid once. - Interchange with old tools. Use
snappyorgzip. Check what the consuming system supports before usingzstdorlz4.
Common errors
- Tuning row groups without sorting. Statistics only skip data if values in different row groups occupy different ranges. If
idis randomly spread across groups, every group has nearly the same min and max, and nothing is skipped no matter how small the groups are. - Treating
row_group_sizeas bytes. It counts rows. Checktotal_compressed_sizeper row group from the footer and adjust the row count to reach your byte target. - Writing many tiny files. Each file has its own footer and at least one row group. A thousand files of 1,000 rows each is worse than one file of one million rows, whatever the codec.
- Using the highest compression level by default. The extra write time rarely pays off unless the data is archived or read many times.
- Compressing already-compressed data. Columns holding compressed blobs or hashes gain almost nothing from a codec.
- Assuming all readers support all codecs. Some older tools cannot read
zstd,lz4, orbrotlifiles. Test with every consumer.
Exercise
A table of 400 million rows is stored as one Parquet file with a single row group, compressed with gzip. Queries filter on a timestamp column and run on a cluster of many workers. Name two changes you would make and explain why each helps.
Show answer
First, split the data into many row groups (for example 1 million rows each) after sorting by the timestamp column. This gives each row group a narrow min/max range, so filters can skip most groups, and it lets workers read different groups in parallel. Second, switch from gzip to zstd at a low or medium level (such as 3). It compresses about as well or better, and decompresses much faster, so reads use less CPU. A single row group cannot be skipped or split across workers, which is why the row group change matters most.
Exercise
You halve the row group size of a file whose rows are in random order with respect to the filter column. Queries on that column are not faster. Why?
Show answer
With random order, every row group contains values across the whole range, so each group's min and max cover nearly the same interval. Statistics cannot rule any group out, and no group is skipped. Smaller groups only add footer size and per-chunk overhead. Sorting or clustering the data by the filter column is needed first.