Lesson 1 of 9 · 21m
The Columnar Mental Model
Why column stores are fast for analytics, why they are miserable as primary application databases, and how to predict query cost before you run it.
The first mistake teams make with ClickHouse is treating it like Postgres with better compression. It is not a row database with an analytics mode. It is a scan engine built around one observation: analytical queries touch many rows but few columns, while transactional queries touch few rows and many columns. If you keep that sentence in your head, most of ClickHouse's strange behavior stops being strange.
Rows versus columns
A row store lays a record out contiguously: event_id, timestamp, user_id, country, bytes, and fifty other fields sit beside one another on disk. That is ideal for SELECT * WHERE event_id = ..., and terrible for SELECT country, sum(bytes) FROM events GROUP BY country, because the database has to drag entire rows through memory just to use two columns.
A columnar store writes each column separately:
events/
timestamp.bin
user_id.bin
country.bin
bytes.binNow a query that needs only country and bytes reads only those streams. On a wide event table, that can mean reading 3% of the bytes a row store would read. Compression gets better too, because neighboring values in a column look alike: timestamps cluster, country codes repeat, and low-cardinality dimensions compress beautifully with codecs such as LZ4, ZSTD(3), Delta, or DoubleDelta.
I/O is the currency
ClickHouse performance is mostly a story about bytes read:
EXPLAIN ESTIMATE
SELECT toDate(event_time) AS day, sum(bytes)
FROM events
WHERE event_time >= now() - INTERVAL 7 DAY
GROUP BY day;When you look at a slow query, ask how many rows and columns it scanned before you ask whether the SQL is clever. A query that scans one compressed column for a week is often faster than a beautiful query that scans the entire table for a year.
What ClickHouse is bad at
ClickHouse can update and delete rows, but mutations rewrite whole parts in the background. It can join tables, but huge many-to-many joins are not its favorite meal. It can serve point lookups, but a design built around primary-key lookups belongs in Postgres, MySQL, or a key-value store.
The healthy split is boring and durable:
- application state lives in the transactional database;
- immutable events, metrics, logs, and facts stream into ClickHouse;
- product and operational questions are answered by scans over those facts.
Field notes
SELECT *is not a harmless habit in a columnar database; it is a direct instruction to inflate I/O.- If analysts routinely need forty columns in one query, the table shape is probably wrong or the workload is not analytical.
- Compression is not just storage savings. Less compressed data read usually means less CPU spent decoding and less memory bandwidth burned.
- A fast analytics system is mostly a system that declines to read data it does not need.
Was this lesson helpful?