Overview

ClickHouse is an open-source column-oriented OLAP database. It stores each column in its own files, compresses them hard, and executes queries with vectorised, parallel pipelines. It is built to scan and aggregate billions of rows in seconds, not to update single rows. Everything in this section was run on ClickHouse 26.10.4 (the latest image at the time of testing) in Docker, using a 5-million-row events_a table unless stated.

Why it matters

Analytical queries touch few columns of many rows. A row store reads every column of every row it needs; a column store reads only the referenced columns, and similar values stored together compress far better. This is why ClickHouse excels at logs, events, metrics and reporting, and why it is the wrong tool for OLTP (ch-when-not-to-use).

Core concepts

  • Column files: each column is a separate file inside a data part (ch-mergetree-parts). Reading SUM(amount) opens one column.
  • Compression is per column with a codec (LZ4 by default, ZSTD, and special ones like Delta, DoubleDelta, Gorilla) (ch-data-types-codecs).
  • Sorted storage: in the MergeTree family rows are stored sorted by the ORDER BY key; a sparse primary index finds ranges (ch-sorting-key-sparse-index).
  • Vectorised execution: operators process blocks of thousands of rows using SIMD where possible; queries use all CPU cores by default (max_threads = auto(16) on the test machine).
  • Append-oriented writes: inserts create new immutable parts; background merges combine them. Updates and deletes are expensive rewrites or masks (ch-mutations).
  • SQL dialect is mostly standard with many extensions (arrays, -If/-State aggregate combinators, ARRAY JOIN, window functions).

Worked example

Five million events sorted by (country, event_type, user_id):

CREATE TABLE events_a (
  event_time DateTime, user_id UInt32,
  country LowCardinality(String), event_type LowCardinality(String),
  url String, amount Float64
) ENGINE = MergeTree ORDER BY (country, event_type, user_id);

Per-column storage from system.columns (compressed vs uncompressed):

ColumnCompressedUncompressedRatio
url (String)22.7 MiB70.5 MiB3.1
amount (Float64)21.0 MiB38.2 MiB1.8
user_id (UInt32)19.2 MiB19.1 MiB1.0
event_time (DateTime)17.1 MiB19.1 MiB1.1
event_type (LowCardinality)25.6 KiB4.8 MiB191
country (LowCardinality)23.8 KiB4.8 MiB205

The whole table is 80 MiB on disk for 5M rows. SELECT sum(amount) read 38 MiB (just that column), while SELECT sum(amount) ... WHERE url = '/p/77' without a helpful index read 72 MiB (two columns). Low-cardinality columns shrink about 200 times; random numeric columns barely compress.

Pitfalls and production notes

  • Row-by-row thinking (many tiny inserts, point updates, SELECT *) works against the engine. Select only needed columns.
  • Wide denormalised tables are normal here; joins exist but are costlier than in OLTP engines (normalization explains what you give up).
  • Design is query-driven: choose the ORDER BY key for your most common filters.
  • ClickHouse has no multi-statement transactions comparable to OLTP databases (transactions-acid); treat inserts as atomic per block.

Practice questions

  1. Why does a column store compress better than a row store?
  2. Which query reads fewer bytes: SELECT * limited to 10 rows or SELECT sum(amount)? Why?
  3. What does LowCardinality do to the storage of a column with eight distinct values?

References

ch-mergetree-parts · ch-sorting-key-sparse-index · ch-when-not-to-use · indexing-theory · pg-architecture