ClickHouse PARTITION BY day vs month: every partition adds a granule to every lookup
ClickHouse never merges across partitions, so each part adds about one granule to every key lookup. In the docs' own example: 671 granules vs 241, 7.5× slower.
PARTITION BY toYYYYMMDD(ts) looks like free speed: queries for one day only touch one day. The ClickHouse docs give a concrete counter-example, and its numbers say more than the docs spell out. On their own example table, the same query reads 671 granules on the partitioned table and 241 on the unpartitioned one: 5.48 million rows against 1.97 million, in 0.090 s against 0.012 s (Table partitions).
Do the subtraction and a rule falls out: 671 = 240 + 431, and 431 is the number of parts the query touched. Every part costs roughly one extra granule (8,192 rows) per key lookup, and merges never cross partitions, so every partition is at least one part. A table partitioned by day for a year makes a single-user lookup read about 365 granules, 3 million rows, where the unpartitioned table reads one.
This is day 8 of the ClickHouse series. Everything below comes from the ClickHouse docs, the source at tag v26.10.2.103-stable, and the measurements ClickHouse publishes in its docs. The day/month/hour tables are arithmetic on those rules, not benchmarks.
The short answer
| No partition key | toYYYYMM (month) |
toYYYYMMDD (day) |
Hour, user ID, tenant | |
|---|---|---|---|---|
| Fewest parts after 1 year | 1 (more above 150 GiB) | 12 | 365 | 8,760+ |
| Key lookup, 1,000 rows over the year | ~1 granule | ~12 granules | ~365 granules | ~8,760 granules |
| Retention | TTL rewrites parts | drop a month | drop a day | — |
| Gets the ZSTD(3) default codec (26.9+) | yes, once merges pass 100 MiB | if a month passes 100 MiB | only if a day passes 100 MiB | rarely |
| ClickHouse’s advice | “In most cases you do not need a partition key” | “in most other cases” the finest you need | “common” for observability | “never” |
The quotes are from the custom partitioning key docs. Rule of thumb from the arithmetic below: a partition costs you nothing extra once it holds 150 GiB or more, because merges stop at that size anyway. Below that, every partition is a part the table wouldn’t otherwise have.
What a partition actually is
A partition is a group of parts that share one value of the PARTITION BY expression. Two rules from the Table partitions page do all the work:
- An insert writes one part per partition value it contains. “ClickHouse creates one new data part for each unique partition key value among the inserted rows.”
- Merges stay inside a partition. “ClickHouse only merges data parts within, but not across partitions.”
So the number of partitions is a floor on the number of parts. A table with no partition key can merge down to one part, until merges hit max_bytes_to_merge_at_max_space_in_pool (150 GiB by default, “roughly the maximum possible part size created by an automatic background merge”, MergeTreeSettings.cpp). A table partitioned by day can never have fewer parts than days.
Each part also gets a small MinMax index over the columns used in the partition expression. That index is the whole query benefit of partitioning: it lets a query skip parts whose ts range can’t match the filter.
Where the extra granule comes from
ClickHouse’s docs build the same UK property-price table twice, sorted by (town, street), once without partitioning and once with PARTITION BY toStartOfMonth(date). Then they run:
SELECT MAX(price) AS highest_price
FROM uk.uk_price_paid_simple_partitioned -- or uk.uk_price_paid_simple
WHERE town = 'LONDON';
| Parts read | Granules read | Rows read | Time | |
|---|---|---|---|---|
| No partitions (1 part) | 1 of 1 | 241 of 3,083 | 1.97 million | 0.012 s |
| By month | 431 of 436 | 671 of 3,257 | 5.48 million | 0.090 s |
All values are from the EXPLAIN and client output on the docs page; it doesn’t say which version ran them. The docs conclude that “querying across all partitions is typically slower”. The numbers also show by how much, and why.
In a part sorted by town, London’s rows are one contiguous run. Reading a run of L rows takes about L/8,192 granules, plus one: the run almost never starts and ends exactly on granule boundaries, so the first and last granules are read even though they’re only partly London. With one part, London’s ~1.97 million rows are 240 granules of data plus that one boundary granule: 241. With 431 parts, London’s rows are split into 431 shorter runs. The data is still 240 granules’ worth, but each run brings its own partial granule:
240 + 431 = 671, the exact number in the EXPLAIN output.
Two more things in those numbers:
- The table itself got bigger in granules: 3,257 vs 3,083. Every part ends in a partial last granule. 436 parts carry 436 partial granules instead of one, about 174 more granule entries for the same rows.
- Time grew 7.5×, rows only 2.8×. Throughput dropped from 162 to 61 million rows/s. Rows alone don’t explain it. Every part is a separate read task with its own index, mark and file setup, so it has a fixed cost. Spread over the 431 parts, the leftover time is about 0.13 ms per part. That’s an inference from one published run, not a measured per-part cost.
Day vs month vs none: the arithmetic
Take a typical events table, ORDER BY (user_id, ts), and look up one user with 1,000 rows spread over a year. The rule from above: each part the lookup touches costs at least one granule, plus about one granule per 8,192 matching rows. A part that doesn’t hold the user at all usually still reads one granule. The sparse index only knows the first key of each granule, so it can’t rule out a value that falls between two marks. In the docs example, only 5 of 436 parts were excluded.
Best case, every partition fully merged into one part:
| Partition key | Parts after 1 year | Granules read for 1,000 rows | Rows read | For a user with 100,000 rows |
|---|---|---|---|---|
| none | 1 | ~1 | ~8k | ~13 granules |
| month | 12 | ~12 | ~99k | ~24 |
| day | 365 | ~365 | ~3.0M | ~377 |
| hour | 8,760 | ~8,760 | ~72M | ~8,771 |
Granules = parts × (1 + max(rows per part − 1, 0) / 8,192). Real partitions are rarely one part: today’s partition is still being merged, and large ones stop at 150 GiB. Both only push the numbers up.
When a partition is free: the 150 GiB rule
The unpartitioned table doesn’t stay at one part forever. Background merges stop at 150 GiB, so a big table has many parts anyway. What matters is whether partitioning adds parts the table would have had regardless:
| Compressed data per year | No partitions: parts at least | Month: parts at least | Day: parts at least |
|---|---|---|---|
| 100 GiB | 1 | 12 | 365 |
| 1 TiB | 7 | 12 | 365 |
| 10 TiB | 69 | 72 (12 × 6) | 365 |
| 100 TiB | 683 | 684 (12 × 57) | 730 (365 × 2) |
Ceil(size / 150 GiB) per partition, using the merge cap above. At 10 TiB a year, monthly partitions cost almost nothing: each month is 853 GiB and would be split into ~6 parts anyway. Daily partitions only come close to free when each day is well above 150 GiB compressed: at 100 TiB a year (280 GiB a day) they add 7% more parts, at 1 TiB a year 52×. That’s why “day” is fine for big observability tables and expensive for everything else.
Where partitioning does win
The same docs page shows the case partitioning is for: filtering on a column that is not a prefix of ORDER BY. Add a date filter to the London query and the MinMax index on date keeps 1 of 436 parts and 11 of 3,257 granules. The primary index then narrows those to 1 granule: 8.19 thousand rows in 0.006 s (Table partitions).
The docs don’t run that date + town query on the unpartitioned table. With ORDER BY (town, street) it would have to read London’s granules and filter date row by row, so partitioning wins here. Putting date into the sort key is the other way to get that pruning without extra parts.
The general rule, also in the docs: partitioning helps a query “if the partitioning key isn’t in the primary key and you’re filtering by it”. If your ORDER BY already starts with the time column, a time filter is already pruned at granule level, and partitioning by time adds parts and nothing else.
What else partitions change
Inserts fan out. One insert block that spans k partitions writes k parts. Backfilling a year of history into a daily-partitioned table writes up to 365 parts per block. Since max_partitions_per_insert_block = 100 and throw_on_max_partitions_per_insert_block = true by default, a block with more than 100 partitions fails with “Too many partitions for single INSERT block” (MergeTreeDataWriter.cpp). The same message adds: “Recommended total number of partitions for a table is under 1000..10000. Please note, that partitioning is not intended to speed up SELECT queries”. With async inserts (on by default since 26.2, see day 3), each flush is one block: a buffer holding late events from three days writes three parts.
“Too many parts” looks at your worst partition. The source computes the active-part count of the partition with the most parts, not the one you’re inserting into (getMaxPartsCountAndSizeForPartition in MergeTreeData.cpp). Inserts slow down at 1,000 parts (parts_to_delay_insert) and fail at 3,000 (parts_to_throw_insert). The check is skipped when that partition’s average part is above 1 GiB (max_avg_part_size_for_too_many_parts). Separately, all partitions together may hold at most 100,000 active parts (max_parts_in_total). That error says outright: “This indicates wrong choice of partition key.”
Small partitions keep the old codec. Since 26.9 a part is recompressed with ZSTD(3) only when a merge’s inputs reach 100 MiB, and inserts always write LZ4 (day 2). Merges don’t cross partitions, so a table that writes less than 100 MiB a day, partitioned by day, stays LZ4 forever. In day 2’s example, a monthly-partitioned table stayed 49% larger than its unpartitioned twin for exactly this reason.
Retention is the real reason to partition. ALTER TABLE ... DROP PARTITION removes a whole partition as a metadata operation. With TTL ts + INTERVAL 30 DAY and ttl_only_drop_parts = 1, a part is dropped only “when all rows in that part have expired”. Without it, expired rows are deleted by merges that rewrite parts, retried at most every 4 hours (merge_with_ttl_timeout). Partition granularity sets how precise retention is:
| Partition key | 30-day TTL with ttl_only_drop_parts = 1: oldest row kept |
|---|---|
| day | ~31 days |
| month | up to ~61 days (a merged month part goes when its last day expires) |
| none | parts span many months and rarely expire as a whole; use the default row-level TTL |
Arithmetic on the documented rule, assuming fully merged partitions.
How to choose
- Start with no partition key and a good
ORDER BY, as the docs recommend. Put the columns you filter on first; day 1 shows how much that order matters. - Partition by month when you need to drop or move data by time, or when you often filter on time but time isn’t at the front of
ORDER BY. - Partition by day only when retention is counted in days and each day is large: tens of GiB at least, ideally near the 150 GiB merge cap. Logs and traces are the usual case.
- Never partition by an ID (user, tenant, client). The docs’ advice: “make a client identifier or name the first column in the ORDER BY expression” instead.
- Check what you have:
SELECT partition,
count() AS parts,
formatReadableSize(sum(bytes_on_disk)) AS size
FROM system.parts
WHERE active AND database = 'default' AND table = 'events'
GROUP BY partition
ORDER BY partition DESC
LIMIT 20;
If most rows show one small part (a few hundred MiB or less), the partitions are adding parts without buying anything. Run EXPLAIN indexes = 1 on a typical lookup and compare Parts: with Granules:; when the two are close, you’re paying the partition tax.
What I couldn’t verify
- The fixed cost per part (~0.13 ms) is inferred from one published run on ClickHouse’s playground, version not stated. No independent measurement found.
- The docs page also says the partitioned table “has over 600 partitions”; its own EXPLAIN shows 436 active parts, which can’t hold more than 436 partitions. I used the EXPLAIN numbers.
- The 1,000-row lookup tables assume an even spread and fully merged partitions. A user active in only a few days has fewer matching parts, but as shown above, parts without the key usually still cost a granule unless the key falls outside the part’s min/max range.
FAQ
Should I partition ClickHouse tables by day or by month? Usually by month, or not at all. Each partition is at least one part, and every part adds about one granule to every key lookup. Day partitions pay off for large log tables with day-level retention.
Does PARTITION BY make ClickHouse queries faster?
Only when you filter on the partition column and it isn’t already a prefix of ORDER BY. Queries that don’t filter on it read more: 671 vs 241 granules, 0.090 vs 0.012 s in the docs’ example.
How many partitions is too many? The docs suggest “fewer than 100 - 1,000 distinct values” (partitioning best practices). The insert error says “under 1000..10000”, and 100,000 active parts across a table is a hard limit by default.
Why do I get “Too many partitions for single INSERT block”?
One insert block contained more than 100 partition values, typically a backfill into a daily or hourly partitioned table. Insert in time order in smaller batches, or rethink the key. Raising max_partitions_per_insert_block hides the problem.
Can I change the partition key later?
Not in place. Create a new table with the new key and copy the data with INSERT ... SELECT, batched by time so each block stays under the partition limit.
Tomorrow, day 9: index_granularity at 1,024, 8,192 and 65,536, and what a granule actually costs.