1. Monthly partitions
toYYYYMM groups rows into one partition per month.
PARTITION BY toYYYYMM(event_date)Typorithm · medium
Step 1 of 8 · intro card
Indexing & Partitioning
PARTITION BY toYYYYMM(event_date) groups rows into monthly partitions, and a WHERE on the partition expression prunes whole partitions before any data is read.
PARTITION BY splits a table's parts by an expression, commonly toYYYYMM(date) for one partition per month.
When a query filters on the partition expression, ClickHouse compares the filter to each partition's min/max and skips partitions that cannot match — before reading any granules.
Pruning happens above the sparse index: first whole partitions are dropped, then the primary index skips granules inside the partitions that remain.
PARTITION BY groups parts by an expression.
Filters on it drop whole partitions.
Pruning happens before granule reads.
CREATE TABLE events
(
event_date Date,
user_id UInt64,
payload String
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(event_date)
ORDER BY (user_id, event_date);
SELECT count()
FROM events
WHERE event_date >= '2026-06-01';Connect each part of the code to the concept before you type.
toYYYYMM groups rows into one partition per month.
PARTITION BY toYYYYMM(event_date)A filter on the partition expression lets ClickHouse drop whole partitions before reading.
WHERE event_date >= '2026-06-01'ORDER BY still sorts and indexes rows within each surviving partition.
ORDER BY (user_id, event_date)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.