A ClickHouse query got 10× slower after the data grew 10× — find the cause
analytics.events is a MergeTree table with PARTITION BY toYYYYMM(event_time) and ORDER BY (user_id, event_time). After a large client launched, its volume grew about tenfold within a month, and a month-to-date dashboard counter went from 0.4 s to 4.1 s: SELECT count() FROM events WHERE event_type = 'purchase' AND event_time >= toStartOfMonth(now()). Constraints: no new hardware, and the cause must follow from the output below.
-- system.query_log, this query, type = 'QueryFinish'
event_date query_duration_ms read_rows read_bytes result_rows
2026-10-05 412 80412352 1.05 GiB 1
2026-11-05 4127 789307520 10.31 GiB 1
-- EXPLAIN indexes = 1, today
ReadFromMergeTree (analytics.events)
Indexes:
MinMax
Keys:
event_time
Condition: (event_time in [1793491200, +Inf))
Parts: 41/530
Granules: 96480/1240117
Partition
Keys:
toYYYYMM(event_time)
Condition: (toYYYYMM(event_time) in [202611, +Inf))
Parts: 41/41
Granules: 96480/96480
PrimaryKey
Keys:
event_time
Condition: (event_time in [1793491200, +Inf))
Parts: 41/41
Granules: 96480/96480
-- system.parts, active parts per partition (last two)
partition parts
202611 41
202610 37Diagnose the cause.
read_rows grew tenfold with the data: pruning keeps the whole month and the primary key drops no granule, as event_type is outside the (user_id, event_time) key. Parts per partition are normal. Fix: a sort key or projection led by event_type.
- Blaming hardware or merges without comparing read_rows before and after the slowdown
- Reading successful partition pruning as proof that the primary key is used
- Assuming every column in the WHERE clause benefits from the sorting key
- How would a projection ordered by
event_typechange this query'sEXPLAINoutput? - Which
system.partsnumbers would point to merges falling behind instead?
Reading the output. result_rows is 1 both times, while read_rows and read_bytes grew by exactly the factor the data grew. The query did not get slower per row — it reads ten times as many rows.
MinMaxkept 41 of 530 parts andPartitionkept the same 41: pruning works, butevent_time >= toStartOfMonth(now())covers the whole current month by definition.PrimaryKey: 96480/96480 — not one granule inside the month was dropped. The key(user_id, event_time)starts withuser_id, which the filter does not mention, and the most selective predicate,event_type = 'purchase', is not in the key at all.system.parts: 41 and 37 active parts per partition are normal; merges are keeping up. That rules out a parts explosion.
Result: the query scans the whole month, and its cost grows linearly with the month's volume.
Fix. Let an index work on event_type:
-- A projection with a different sort order; the optimizer picks it on its own
ALTER TABLE analytics.events
ADD PROJECTION by_type (SELECT * ORDER BY event_type, event_time);
ALTER TABLE analytics.events MATERIALIZE PROJECTION by_type;Alternatives: a new table with ORDER BY (event_type, event_time, user_id) when per-type queries dominate, or an aggregating materialized view with counts per day and type. After the change, EXPLAIN indexes = 1 should show a small share of granules kept under PrimaryKey (or in the chosen projection).