Sub topic 7
Compression codecs and their trade-offs
This chapter assumes you know what row groups, column chunks, and pages are, and that you can write and read Parquet files with PyArrow. Compression is applied to each page inside a column chunk, after encoding (dictionary, run-length, delta) has already shrunk the values. The codec you pick sets a balance between file size, write speed, and read speed.
Where compression happens
Parquet compresses each data page on its own. The footer records the codec per column chunk, so one file can use different codecs for different columns. A reader decompresses only the pages it needs, which keeps skipping effective.
The common codecs
| Codec | Compression ratio | Speed | Typical use |
|---|---|---|---|
| none | None | Fastest | Data already compressed, or very fast local scratch storage |
| snappy | Moderate | Very fast | Default in many tools, balanced for general analytics |
| zstd | High, tunable by level | Fast | Good default when storage and network cost matter |
| gzip | High | Slow | Compatibility with older tools, rarely best today |
Other codecs such as LZ4 and Brotli exist, but support varies by reader. Check that every tool in your pipeline can read a codec before you use it.
Comparing codecs on your data
The best way to choose is to measure. This example builds a table and writes it with each codec. It is self-contained, so you can run it as shown.
import os
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"]:
path = f"events_{codec}.parquet"
pq.write_table(table, path, compression=codec)
print(codec, os.path.getsize(path))Exact sizes depend on your PyArrow version, but the pattern is stable. The none file is largest. Snappy is smaller. Zstandard and gzip are smallest. Time the writes and reads too, because a smaller file is not always the faster one to process.
Why the gap can be small
Encoding runs before compression. The category column has only four distinct values, so dictionary and run-length encoding already remove most of the redundancy. The codec then has little left to squeeze. Gains from a stronger codec are largest on columns with high-entropy data, such as free text, long strings, or random identifiers.
The id column is a sequence of increasing integers. Delta encoding can shrink it a lot, but PyArrow's default writer uses dictionary encoding where it can, so the exact result depends on the writer settings.
Tuning a codec
You can set the codec per column and, for some codecs, the level:
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"id": list(range(100_000)),
"note": [f"event number {i}" for i in range(100_000)],
})
pq.write_table(
table,
"mixed.parquet",
compression={"id": "snappy", "note": "zstd"},
compression_level={"id": None, "note": 9},
)Higher zstd levels shrink files further but slow writes. Decompression speed stays high at every level, so a high level suits data that is written once and read many times.
Choosing a codec
Use these questions to decide:
- Is the data written once and read often? Prefer zstd, with a moderate to high level.
- Is write speed the bottleneck, as in streaming ingestion? Prefer snappy or a low zstd level.
- Is storage or network transfer the main cost? Prefer zstd.
- Must older tools read the file? Use snappy or gzip, which have the widest support.
Common errors
- Picking a codec from a benchmark on different data. Ratios depend heavily on the column types and values.
- Using gzip for hot data. Its slow decompression raises query time.
- Expecting compression to fix poor layout. Compression works inside a page, so it does not replace good sorting or row group sizing.
- Assuming a codec is available. Some builds of a reader omit optional codecs, and reading fails with an error.
Exercise
A table is written once each night and queried thousands of times a day from cloud storage. Which codec fits best, and why?
Show answer
Zstandard, usually at a moderate or high level. The higher write cost is paid once, while the smaller files reduce storage and network transfer on every query. Zstandard also decompresses quickly, so read speed stays good.