ClickHouse
ClickHouse is a columnar DBMS for analytics: it reads only the needed columns and compresses them, so an aggregate over billions of rows comes back in seconds. The price is a storage model unlike PostgreSQL. Rows are not edited in place: each insert writes a new immutable part, and background merges later combine parts and do the engine's work — collapse duplicates, sum, drop what was marked.
Everything asked in interviews grows from this. Read speed comes from ORDER BY and the sparse index, not from partitions. There is no key uniqueness, and only a merge removes duplicates, at an unknown time. Frequent small inserts create parts faster than merges can combine them. Updates and deletes are expensive, so changes are modelled as inserts. Below is a map that leads from how MergeTree works to the special engines and mutations. Versions in the text are ClickHouse 26.x; where a default changed, the version is named.
Topic map
- MergeTree — parts and merges,
ORDER BYversusPRIMARY KEY, the sparse index and granules,PARTITION BYand its pitfalls, the Too many parts error. - Special engines — ReplacingMergeTree and
FINAL, including the partition rule, Aggregating-, Summing-, and CollapsingMergeTree in brief, materialized views as insert triggers. - Mutations and deletes —
ALTER … UPDATE/DELETEmutations versus lightweightDELETE, why ClickHouse avoids row-level edits, and how to model changes as inserts.
Common traps
| Mistake | Consequence |
|---|---|
Treating ORDER BY or PRIMARY KEY as unique | Duplicates are stored, and queries count them twice |
Partitioning by user_id or by hour | Thousands of partitions, an avalanche of parts, slow inserts and reads |
| Inserting one row at a time | Parts pile up faster than merges, inserts slow down, then fail with Too many parts |
| Expecting ReplacingMergeTree to drop the old version at once | Duplicates until a merge — and across partitions, forever |
| Partitioning a ReplacingMergeTree by a mutable column | A key's versions land in different partitions and never collapse on disk |
Editing rows with frequent ALTER … UPDATE | Every mutation rewrites whole parts |
Interview relevance
ClickHouse is asked about to check whether a candidate can design a table for the engine rather than carry over row-store habits. A strong answer explains behaviour through parts and merges: "the duplicates show because two inserts are two parts, and the merge has not run yet".
Typical checks:
- What a part is and what background merges do.
ORDER BYversusPRIMARY KEY, granules, and when the index skips almost nothing.- What
PARTITION BYis for and why a fine-grained partition hurts. - The Too many parts error and its fix.
- Duplicates in ReplacingMergeTree,
FINAL, and the partition rule. - Materialized views as an insert trigger.
- Mutations versus lightweight
DELETE.
Common wrong answer: "We partition by user_id so per-user queries read less." In fact a partition is a data-management unit, not an index. A per-user filter is sped up by ORDER BY (user_id, …), while millions of partitions split every insert into many parts.