真实对比按月和按天分区在同样数据量下产生的分区数差异,再故意撞上分区数上限,拿到ClickHouse官方对分区用途的真实定位说明。
代码CREATE TABLE demo.pmonth (d Date, k UInt64) ENGINE=MergeTree PARTITION BY toYYYYMM(d) ORDER BY k; CREATE TABLE demo.pday (d Date, k UInt64) ENGINE=MergeTree PARTITION BY toDate(d) ORDER BY k; INSERT INTO demo.pmonth SELECT toDate('2026-01-01')+number, number FROM numbers(90); INSERT INTO demo.pday SELECT toDate('2026-01-01')+number, number FROM numbers(90);
SELECT count(DISTINCT partition) FROM system.parts WHERE table='pmonth' AND active; → 3 ← 真实覆盖2026年1、2、3月,正好3个分区 SELECT count(DISTINCT partition) FROM system.parts WHERE table='pday' AND active; → 90 ← 真实每天一个分区
同样90天的数据,按月分区真实只产生3个分区目录,按天分区真实产生90个——分区数直接由分区键的粒度决定,跟数据量本身无关。
SELECT value FROM system.settings WHERE name='max_partitions_per_insert_block';
→ 100
INSERT INTO demo.pday2 SELECT toDate('2020-01-01')+number, number FROM numbers(1000);
→ 真实报错:
DB::Exception: Too many partitions for single INSERT block (more than 100).
Large number of partitions is a common misconception. It will lead to
severe negative performance impact, including slow server startup,
slow INSERT queries and slow SELECT queries. Recommended total number
of partitions for a table is under 1000..10000. Please note, that
partitioning is not intended to speed up SELECT queries (ORDER BY key
is sufficient to make range queries fast). Partitions are intended for
data manipulation (DROP PARTITION, etc). (TOO_MANY_PARTS)每一步都是本文上面用真实建表插入跑出来的原始结果。
system.parts(partition列)max_partitions_per_insert_block(默认100)拿到官方报错原文