Sub topic 7
Compression codecs and their trade-offs
This chapter assumes you know what row groups, column chunks, and pages are, and that a Parquet writer encodes values (dictionary, run-length, delta) before it compresses them. You should be comfortable running PyArrow scripts. By the end you will know where compression sits in the file, how the common codecs differ, and how to pick and verify a codec for a workload.
Where compression happens in a Parquet file
Parquet compresses each page on its own, after encoding. A column chunk is a sequence of pages, and each page body is encoded and then passed through the codec. The dictionary page, if the chunk has one, is compressed the same way.
Three consequences follow from this design:
- Codec is set per column chunk. The footer records the codec for every column chunk, so a reader needs no file-level setting. Different columns can use different codecs.
- Decompression is independent per page. A reader that skips a page or a row group never decompresses it.
- Metadata is not compressed. The footer and page headers, which hold statistics and offsets, stay readable without any codec.
Encoding exploits structure in the data, such as repeated values or sorted integers. A codec is a general-purpose byte compressor that finds repeated byte patterns. They stack. A low-cardinality column is usually already small after dictionary encoding, and the codec squeezes what remains.
The common codecs
| Codec | Compression ratio | Compression speed | Decompression speed | Typical use |
|---|---|---|---|---|
| UNCOMPRESSED | 1x | Fastest | Fastest | Debugging, data already compressed |
| SNAPPY | Low to moderate | Very fast | Very fast | Default in many tools, CPU-bound scans |
| LZ4 (LZ4_RAW) | Low to moderate | Very fast | Very fast | Similar to Snappy, often faster to decode |
| ZSTD | Moderate to high, tunable | Fast to slow by level | Fast at every level | Good general choice for storage and scans |
| GZIP | Moderate to high | Slow | Moderate | Compatibility with older readers |
| BROTLI | High | Slowest | Moderate | Rarely worth it for analytics |
Treat the ratios as rules of thumb. The real numbers depend on your data, so measure them (see below).
Snappy
Snappy is built for speed. It does little entropy coding, so it gives up ratio to keep both compression and decompression cheap. PyArrow's write_table uses it by default. It works well when queries are CPU-bound and storage is cheap.
Zstandard
Zstandard (zstd) offers a wide range of levels. Low levels compress about as fast as Snappy and usually produce smaller files. High levels spend much more write-time CPU for a smaller file, while decompression stays fast. This asymmetry suits write-once, read-many tables, where you pay the compression cost once.
Gzip
Gzip (DEFLATE) usually compresses well but decompresses more slowly than Snappy or zstd. Today it is mostly a compatibility choice. If every reader in your stack supports zstd, zstd is generally the better pick.
LZ4 and Brotli
Parquet has two LZ4 variants. The older LZ4 framing was implemented inconsistently across libraries, so the format added LZ4_RAW. Check that your readers support it before choosing it. Brotli reaches high ratios but is slow to write, and library support is uneven.
Comparing codecs on your own data
This script builds a table and writes it with each codec. It prints the file size, then reads the footer to get the compressed and uncompressed sizes of the first column chunk.
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)
size = os.path.getsize(path)
col = pq.ParquetFile(path).metadata.row_group(0).column(0)
print(
f"{codec:7} file={size:>10} "
f"compressed={col.total_compressed_size:>10} "
f"uncompressed={col.total_uncompressed_size:>10} "
f"codec_in_footer={col.compression}"
)Exact numbers vary by PyArrow version, but the pattern is stable. The none file is the largest, and total_compressed_size equals total_uncompressed_size for that codec. The other three files are smaller. This synthetic table is unusually regular, so it compresses far better than real data. Always repeat the test on a sample of your own data.
The ratio total_uncompressed_size / total_compressed_size for a column chunk tells you how much the codec (together with encoding) saved. "Uncompressed" here means the size after encoding but before the codec, not the raw in-memory size.
Tuning levels and per-column codecs
write_table accepts a compression_level and lets you set the codec per column with a dictionary.
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,
})
# One codec, higher level
pq.write_table(table, "events_zstd9.parquet",
compression="zstd", compression_level=9)
# Different codec per column
pq.write_table(
table,
"events_mixed.parquet",
compression={"id": "zstd", "category": "snappy"},
)
md = pq.ParquetFile("events_mixed.parquet").metadata
rg = md.row_group(0)
for i in range(rg.num_columns):
col = rg.column(i)
print(col.path_in_schema, col.compression)The valid level range depends on the codec. Zstd accepts a wide range, gzip accepts 1 to 9, and Snappy has no levels. An unsupported level raises an error.
Compression and skipping
Compression does not stop a reader from skipping data. Statistics live in uncompressed metadata, and the reader decompresses only the pages it decides to read. A filtered read such as filters=[("id", ">=", 950_000)] can skip whole row groups using min/max statistics without touching their compressed bytes.
Compression does affect how much you read when you do not skip. Smaller column chunks mean less disk and network I/O, but each byte read must be decompressed. The balance depends on where your bottleneck is.
- I/O-bound (object storage, slow network, cold cache): a higher ratio often wins, because bytes transferred dominate.
- CPU-bound (local NVMe, warm cache, many cores busy): a faster decompressor such as Snappy, LZ4, or low-level zstd often wins.
Choosing a codec
Use this checklist as a starting point and verify with a measurement.
- Default for analytics on object storage: zstd at a low to moderate level. It usually beats Snappy on size and keeps decode fast.
- Latency-sensitive, CPU-bound scans on local disk: Snappy or LZ4_RAW.
- Long-term archive, written once: zstd at a higher level, if write time is acceptable.
- Old readers in the pipeline: Snappy or gzip, whichever every reader supports.
- Already-compressed payloads (images, compressed blobs stored in a binary column):
UNCOMPRESSEDfor that column, since a codec adds CPU cost and saves little.
Common mistakes
- Judging a codec on tiny files. Small samples hide the fixed overhead per page and per row group. Test with data sized like production.
- Ignoring encoding. A column that is poorly encoded, for example high-cardinality strings where dictionary encoding falls back to plain, may benefit more from sorting the data than from a stronger codec. Sorting groups similar values together, which helps both encoding and the codec.
- Comparing file sizes only. A smaller file that doubles query CPU time is not a win if your queries are CPU-bound.
- Forgetting compatibility. Check the oldest reader before adopting a newer codec.
Exercise
A table is written once per night to object storage and queried many times during the day by several engines. All engines support zstd. Which codec and level strategy would you try first, and how would you confirm it was the right choice?
Show answer
Start with zstd at a low to moderate level, since it is written once, read often, and I/O to object storage is likely the bottleneck, so a better ratio than Snappy pays off while zstd decompression stays fast. Confirm by writing a representative sample with Snappy and zstd at two or three levels, comparing file sizes and the per-column totalcompressedsize versus totaluncompressedsize from the footer, and timing the real queries for each. Keep the choice that gives the best query time within an acceptable nightly write time.
Exercise
In a Parquet file written with compression="zstd", why can a filtered read skip a row group without decompressing any of its pages?
Show answer
Min/max statistics and page offsets are stored in the footer and page headers, which are not compressed. The reader checks the statistics against the filter and, if the row group cannot match, never reads or decompresses its compressed pages.