ClickHouse default compression codec, measured: the 100 MiB rule that picks LZ4 or ZSTD(3)
Since 26.9 ClickHouse picks LZ4 or ZSTD(3) per part by a 100 MiB rule. Inserts stay LZ4; one big merge cut 10M rows from 166 to 112 MiB, reads cost 40-70% more CPU.
On this page
- The rule, from the source
- Experiment 1: 10 million rows, one merge
- Experiment 2: finding the edge, byte by byte
- Experiment 3: the partitioned table that never switches
- Experiment 4: what a normal insert stream looks like
- What ZSTD(3) buys and what it costs
- The oddity: shrinking rewrites keep ZSTD(3)
- Checking and controlling your own tables
- Frequently asked questions
Ask what ClickHouse compresses your data with and you will get two answers. The docs say LZ4 for self-managed servers and ZSTD for ClickHouse Cloud. The 26.9 changelog says the default moved from LZ4 to ZSTD(3). Both are out of date in a useful way: since 26.9 the default is size-aware. Small parts get LZ4, parts above a threshold get ZSTD(3), and the same table can hold both at once.
This is day 2 of a daily ClickHouse series built on experiments rather than docs. Yesterday’s post on parts, granules and ORDER BY pinned every table to LZ4 for exactly this reason. Today I trace the rule in the source, then measure where the switch happens, which tables never reach it, and what ZSTD(3) buys and costs on 10 million rows. Everything ran on ClickHouse 26.10.3.7 in Docker with default settings.
The rule, from the source
The server’s config.xml describes the default in one line:
If nothing matches, the size-aware built-in default is used:
LZ4 below 100 MB and ZSTD(3) at or above it.
The implementation is CompressionCodecSelector::choose(part_size, part_size_ratio). With no <compression> cases configured it is a single comparison:
static constexpr size_t min_part_size_for_default_codec = 100 * 1024 * 1024;
CompressionCodecPtr res = part_size < min_part_size_for_default_codec
? factory.get("LZ4", {})
: factory.getDefaultCodec(); // ZSTD(3) since 26.9
Three details decide everything that follows:
- The threshold is 100 MiB, 104,857,600 bytes, even though the config comment says “100 MB”.
part_sizeis an estimate known before writing. The codec has to be picked before the first block is compressed, so the resulting part’s size cannot be used.- Each write path passes a different estimate. In 26.10,
MergeTreeDataWriterpasses0for everyINSERT, so inserted parts are always LZ4. A merge passes the summed on-disk size of the parts it merges. A mutation passes the on-disk size of the part it rewrites.
The choice is recorded in each part, and system.parts exposes it as default_compression_codec. That column is the measuring instrument for the rest of this post.
Experiment 1: 10 million rows, one merge
The table is the page-views schema from day 1, filled deterministically with hashes so every run produces the same bytes:
CREATE TABLE pv
(
site_id UInt32,
event_time DateTime,
user_id UInt64,
url String,
duration_ms UInt32
)
ENGINE = MergeTree
ORDER BY (site_id, event_time);
SYSTEM STOP MERGES pv;
INSERT INTO pv
SELECT
cityHash64(number, 1) % 100,
toDateTime('2026-08-01 00:00:00') + cityHash64(number, 2) % (90 * 86400),
cityHash64(number, 3) % 1000000,
concat('/articles/', toString(cityHash64(number, 4) % 5000)),
cityHash64(number, 5) % 60000
FROM numbers(10000000);
SELECT name, rows, formatReadableSize(bytes_on_disk) AS on_disk, default_compression_codec AS codec
FROM system.parts WHERE table = 'pv' AND active;
name rows on_disk codec
all_1_1_0 1111953 18.49 MiB LZ4
all_2_2_0 1111953 18.49 MiB LZ4
...
all_9_9_0 1104376 18.37 MiB LZ4
Nine parts, 166.30 MiB in total, every one LZ4. Now one merge:
SYSTEM START MERGES pv;
OPTIMIZE TABLE pv FINAL;
name rows on_disk codec
all_1_9_2 10000000 111.52 MiB ZSTD(3)
The merge saw 174 MB of input parts, far above 100 MiB, and wrote the result with ZSTD(3). The same rows shrank from 166.3 MiB to 111.5 MiB, 33% less, without anyone touching a codec.
Experiment 2: finding the edge, byte by byte
To hit the threshold precisely I used random printable strings, which LZ4 cannot compress, so the on-disk size per row is stable at about 205 bytes. Two tables, each with two parts, merges stopped during the inserts:
CREATE TABLE t_below (id UInt64, s String) ENGINE = MergeTree ORDER BY id;
CREATE TABLE t_above (id UInt64, s String) ENGINE = MergeTree ORDER BY id;
SYSTEM STOP MERGES t_below; SYSTEM STOP MERGES t_above;
INSERT INTO t_below SELECT number, randomPrintableASCII(200) FROM numbers(255000);
INSERT INTO t_below SELECT number, randomPrintableASCII(200) FROM numbers(255000);
INSERT INTO t_above SELECT number, randomPrintableASCII(200) FROM numbers(257000);
INSERT INTO t_above SELECT number, randomPrintableASCII(200) FROM numbers(257000);
SELECT table, sum(bytes_on_disk) AS input_bytes
FROM system.parts WHERE table IN ('t_below', 't_above') AND active GROUP BY table;
SYSTEM START MERGES t_below; SYSTEM START MERGES t_above;
OPTIMIZE TABLE t_below FINAL; OPTIMIZE TABLE t_above FINAL;
| Table | Input parts on disk | vs 100 MB (10^8) | vs 100 MiB (104,857,600) | Codec after merge |
|---|---|---|---|---|
t_below |
104,479,285 bytes (99.64 MiB) | above | below | LZ4 |
t_above |
105,298,572 bytes (100.42 MiB) | above | above | ZSTD(3) |
Both tables are above 100 MB in the decimal sense. Only the one above 100 MiB switched. If you plan capacity around this rule, use 104,857,600 bytes, not 100,000,000.
Experiment 3: the partitioned table that never switches
Merges never cross partitions, so the size a merge sees is the size of its partition’s parts, not of the table. The same 10 million rows with a monthly partition:
CREATE TABLE pv_month (/* same columns */)
ENGINE = MergeTree
PARTITION BY toYYYYMM(event_time)
ORDER BY (site_id, event_time);
INSERT INTO pv_month SELECT * FROM pv;
OPTIMIZE TABLE pv_month FINAL;
partition name rows on_disk codec
202608 202608_2_27_2 3443892 57.24 MiB LZ4
202609 202609_3_29_2 3333546 55.41 MiB LZ4
202610 202610_1_30_2 3222562 53.56 MiB LZ4
| Table | Partitioning | Size after OPTIMIZE FINAL | Codec |
|---|---|---|---|
pv |
none | 111.52 MiB | ZSTD(3) |
pv_month |
by month | 166.21 MiB | LZ4 |
Each month is 55 MiB of input, so every merge stays under the threshold and the table is 49% larger than its unpartitioned twin, forever. This is the trap in the new default: tables whose partitions stay under 100 MiB (daily partitions, small tenants, lookup tables) still behave exactly like the old LZ4 default. It also explains a puzzle from day 1, where a partitioned 106 MiB table stayed LZ4 after OPTIMIZE FINAL: 106 MiB over two partitions is two merges of about 53 MiB each.
Experiment 4: what a normal insert stream looks like
Experiments 1 to 3 forced merges. What happens with background merges on? I inserted the same 10 million rows as 100 batches of 100,000 rows into a fresh table and let the server do what it wanted.
SELECT default_compression_codec AS codec, count() AS parts, sum(rows) AS rows,
formatReadableSize(sum(bytes_on_disk)) AS on_disk
FROM system.parts WHERE table = 'pv_stream' AND active GROUP BY codec;
codec parts rows on_disk
LZ4 9 10000000 166.32 MiB
Forty seconds after the last insert the background merges had collapsed 100 parts into 9, all LZ4. system.part_log shows why. Seventeen merges ran, and the biggest ones took 7 or 8 parts of about 5 MiB each:
| Merge result | Inputs | Input size | Result | Codec |
|---|---|---|---|---|
all_1_6_1 … all_88_92_1 (13 merges) |
5–6 | 8.4–10.1 MiB | 8.3–10.0 MiB | LZ4 |
all_1_23_2, all_24_46_2, all_47_69_2 |
8 | 38.4 MiB | 38.2 MiB | LZ4 |
all_70_95_2 |
7 | 43.3 MiB | 43.2 MiB | LZ4 |
No merge saw more than 43.3 MiB of input, so no part became ZSTD(3). The four 38 MiB parts would qualify once merged together, but ClickHouse’s merge selector only merges parts of similar size when there are enough of them or they are old enough. I repeated the whole run on a fresh table and left it alone: six minutes after the last insert it was still five parts and 100% LZ4. It switches only when a merge finally combines those parts, which I did not wait for.
Two consequences for a real table:
- Compression arrives late and unevenly. Fresh data is LZ4, old data that has been through a large merge is ZSTD(3). Disk usage per day of data drops as the data ages.
- Small tables never get there. A table that stays under 100 MiB per partition is an LZ4 table, whatever the changelog says.
A note on the future: the development branch of ClickHouse already changes the insert path to pass the uncompressed size of the inserted block instead of 0, so in a later release a single large INSERT will get ZSTD(3) immediately. In 26.10 it does not.
What ZSTD(3) buys and what it costs
To compare codecs cleanly I pinned each one with the table setting default_compression_codec, which bypasses the size rule, loaded identical data and ran OPTIMIZE TABLE ... FINAL so each table is one part.
CREATE TABLE cmp_lz4 (/* same columns */) ENGINE = MergeTree ORDER BY (site_id, event_time)
SETTINGS default_compression_codec = 'LZ4';
CREATE TABLE cmp_zstd3 (/* same columns */) ENGINE = MergeTree ORDER BY (site_id, event_time)
SETTINGS default_compression_codec = 'ZSTD(3)';
Size per column from system.parts_columns, with ZSTD(1), the ClickHouse Cloud default, for reference:
| Column | Uncompressed | LZ4 | ZSTD(1) | ZSTD(3) | ZSTD(3) vs LZ4 |
|---|---|---|---|---|---|
url (String) |
207.69 MiB | 46.39 MiB | 28.09 MiB | 25.63 MiB | −45% |
user_id (UInt64) |
76.29 MiB | 44.05 MiB | 33.20 MiB | 31.57 MiB | −28% |
event_time (DateTime) |
38.15 MiB | 38.31 MiB | 30.56 MiB | 30.50 MiB | −20% |
duration_ms (UInt32, random) |
38.15 MiB | 37.23 MiB | 23.79 MiB | 23.77 MiB | −36% |
site_id (UInt32, sorted) |
38.15 MiB | 176 KiB | 28 KiB | 28 KiB | −84% |
| Whole part | 398.42 MiB | 166.18 MiB | 115.71 MiB | 111.52 MiB | −33% |
The write side, from wall-clock time and system.part_log:
| Step | LZ4 | ZSTD(3) | Difference |
|---|---|---|---|
INSERT of 10M rows, wall clock |
1.88 s | 2.16 s | +15% |
Time writing the 9 insert parts (part_log) |
1,439 ms | 1,731 ms | +20% |
CPU in OPTIMIZE FINAL merges |
1,885 ms | 2,806 ms | +49% |
The read side matters more, because every query decompresses. I ran each query with max_threads = 1 and use_uncompressed_cache = 0 so the number is pure scan-and-decompress work, and took the median of 5 to 10 runs:
| Query (single thread) | LZ4 | ZSTD(3) | Difference |
|---|---|---|---|
SELECT sum(duration_ms) |
30 ms | 46 ms | +53% |
GROUP BY site_id with count(), avg(duration_ms) |
55 ms | 75 ms | +36% |
WHERE url LIKE '%/articles/42%' |
151 ms | 222 ms | +47% |
SELECT * of all 10M rows |
258 ms | 444 ms | +72% |
With 8 threads the wall-clock gap mostly disappears (a full SELECT * took 73 ms on LZ4 and 78 ms on ZSTD(3)) because decompression parallelises, but the CPU bill stays: 417 ms versus 499 ms of CPU for that query. On a busy server CPU is the budget, so the real trade is one third less disk for 40 to 70% more decompression CPU on scans. For a table that is read far more than it is written, that is the right trade only when disk or I/O, not CPU, is the limit. ClickHouse’s size rule makes it for you at 100 MiB.
The oddity: shrinking rewrites keep ZSTD(3)
Because the estimate is the input size, a rewrite that discards most of its input is classified as large even when its output is tiny:
ALTER TABLE pv DELETE WHERE site_id != 7 SETTINGS mutations_sync = 2;
name rows on_disk codec
all_1_9_2_10 100206 1.13 MiB ZSTD(3)
A 1.13 MiB part with ZSTD(3), because the mutation read a 111 MiB part. The reverse holds too: deleting half of the 99 MiB LZ4 table from experiment 2 produced a 49.7 MiB part that is still LZ4. The same applies to TTL DELETE merges and to collapsing or deduplicating merges. It is harmless, just surprising when you see a tiny ZSTD(3) part next to much larger LZ4 ones.
Checking and controlling your own tables
See where you stand:
SELECT database, table, default_compression_codec AS codec,
count() AS parts, formatReadableSize(sum(bytes_on_disk)) AS on_disk
FROM system.parts
WHERE active AND database NOT IN ('system')
GROUP BY database, table, codec
ORDER BY database, table, codec;
Three ways to override the rule, from narrow to wide:
- Per column:
url String CODEC(ZSTD(3)). Beats everything else for that column. - Per table:
SETTINGS default_compression_codec = 'ZSTD(3)'(or'LZ4'). Replaces the size rule for all columns without an explicit codec. This is what I used for the comparison tables. - Per server:
<compression><case>blocks inconfig.xmlwithmin_part_sizeandmin_part_size_ratio. Matching cases override the built-in default; non-matching sizes still fall through to the size rule.
And one way to apply the rule retroactively: OPTIMIZE TABLE t FINAL (or any merge of 100 MiB or more of parts) rewrites parts with the codec the rule gives them, as experiment 1 showed.
Frequently asked questions
Is the ClickHouse default codec LZ4 or ZSTD?
Both, depending on part size. Since 26.9, an inserted part is LZ4; a part produced by a merge or mutation whose input parts total 100 MiB (104,857,600 bytes) or more is ZSTD(3). Explicit CODEC(...) clauses and the default_compression_codec table setting override this.
How do I see which codec a part uses?
system.parts.default_compression_codec holds the codec for all columns without an explicit codec. For per-block detail, including blocks picked by adaptive codec selection, use the mergeTreeCodecBlockCounts(db, table) table function.
Why is my table still LZ4 after the upgrade? Parts written before the change keep their codec until a merge rewrites them, and a merge only switches if its inputs sum to 100 MiB or more. Partitioned tables with partitions under 100 MiB never cross that line.
Does this change query speed? Yes. In this test, single-threaded scans on ZSTD(3) parts needed 36 to 72% more CPU than LZ4. With many threads the wall-clock difference shrinks, but the CPU is still spent.
Does the 100 MiB refer to the compressed or uncompressed size? For merges and mutations it is the compressed on-disk size of the input parts. For inserts 26.10 passes 0; the development branch passes the uncompressed size of the inserted block, so large inserts will switch to ZSTD(3) in a later release.
Can I switch the rule off?
Set default_compression_codec on the table, or add a <compression> case with min_part_size 0 in the server config. Either makes the choice unconditional.