Overview
A MergeTree table is physically sorted by its ORDER BY key, and the primary key (by default the same columns) is a sparse index: it stores one entry per granule of 8,192 rows, not one per row. It is not a unique constraint. It lets ClickHouse skip granules that cannot match. Choosing the key is the single most important design decision (indexing-theory covers the contrast with B-trees).
Core concepts
- Granule: the smallest unit read (default
index_granularity = 8192rows). The index holds the key values of each granule’s first row (primary.idx, kept in memory). - Lookup uses binary search when the filter is on the key prefix, and generic exclusion search on later columns, which works well only if the earlier columns have few distinct values.
- Ordering columns from low to high cardinality usually gives the best pruning and compression, and put columns used in filters first.
- The key may differ from the primary key:
PRIMARY KEYmust be a prefix ofORDER BY; use it to keep the in-memory index small. - Unlike B-trees there are no point lookups by non-key column: a filter on a non-key column scans, unless a skipping index (ch-skipping-indexes) or projection (ch-projections) helps.
Worked example
events_a ordered by (country, event_type, user_id): 5,000,000 rows, 5 parts, 612 granules, primary index only 4.33 KiB in memory. Rows actually read for SELECT sum(amount):
| Filter | Rows read | Notes |
|---|---|---|
country='VN' AND event_type='buy' AND user_id=4242 | ~0 (5 of 612 granules) | prefix, binary search |
country='VN' AND event_type='buy' | 212,992 | two key columns |
country='VN' | 658,240 | first key column |
event_type='buy' (second column only) | 1,720,320 | generic exclusion, partial pruning |
user_id=4242 (third column only) | 393,216 | exclusion works within each (country, type) range |
url='/p/77' (not in key) | 5,000,000 | full scan |
| none | 5,000,000 |
EXPLAIN indexes = 1 prints the decision:
PrimaryKey Keys: country, event_type, user_id
Parts: 5/5 Granules: 5/612 Search Algorithm: binary searchwhile the second-column-only query showed Granules: 177/612 with generic exclusion search.
Pitfalls and production notes
- The key cannot be changed after creation (you can only add columns at the end); test candidate keys on real data before loading billions of rows.
- Do not start the key with a high-cardinality column (a timestamp or id) unless every query filters on it; time-ordered keys prune by time but not by the other columns.
- Primary key columns must be cheap to compare; very wide strings in the key bloat the index.
- Expression keys (
toDate(ts)) are allowed and often better than raw timestamps. - ClickHouse does not enforce uniqueness; duplicates need
ReplacingMergeTreeor application logic (ch-replacing-collapsing-engines).
Practice questions
- Why did
event_type='buy'alone prune only partly? - How would you reorder the key to speed up a query that filters only by
user_id? - What is stored in
primary.idx?
References
- ClickHouse documentation, A practical introduction to primary indexes
Related
ch-skipping-indexes · ch-projections · ch-query-optimization · indexing-theory