What ClickHouse is and how it works
ClickHouse is a column-oriented database for analytical queries (OLAP) over large volumes of data: events, logs, metrics and reports spanning billions of rows. A query reads only the columns it needs and runs in parallel across all cores, so aggregations take fractions of a second.
ClickHouse suits data that is mostly appended and then counted: inserts arrive as a stream, while queries aggregate and group. It does not suit transactional (OLTP) workloads with frequent point changes: UPDATE and DELETE are rare and expensive, performed as background operations.
The secret of the speed is columnar storage: the values of each column lie on disk separately, row by row. A query that averages one column needs none of the others, so the system reads only that column from disk. Values of the same kind in neighbouring rows compress well, which further cuts I/O.
An insert is written to an immutable part of the data, and a background process continuously merges small parts into larger ones; rows inside a part are sorted by the primary key, and a sparse index points to granules — groups of 8,192 rows. A query finds granules by binary search and does not scan the rest. For details, see the sections “Table engines” and “Primary key, indexes and projections”.
Query execution is vectorised: operators process blocks of values rather than one row at a time, and a single query engages every CPU core. Values travel through the pipeline in groups, which lowers overhead and makes it possible to use SIMD processor instructions.
CREATE TABLE events ( ts DateTime, user_id UInt64, duration_ms UInt32)ENGINE = MergeTreeORDER BY (ts, user_id); SELECT toStartOfHour(ts) AS hour, avg(duration_ms) AS avg_msFROM eventsWHERE ts >= now() - INTERVAL 7 DAYGROUP BY hourORDER BY hour;How ClickHouse differs from PostgreSQL
PostgreSQL is a classic database for transactional (OLTP) workloads: each operation is carried out in full or rolled back, and its changes stay invisible to other transactions until commit. ClickHouse, by contrast, is built for analytics: data is mostly appended rather than modified, but aggregates over billions of rows are computed in real time.
| Feature | ClickHouse | PostgreSQL |
|---|---|---|
| Storage | by columns, one file per column | by rows, whole rows |
| Transactions | replaced by inserts, merges and mutations | ACID transactions are the foundation |
UPDATE and DELETE | rare mutations, performed in the background | ordinary operations |
| Typical query | aggregation over billions of rows | point reads and writes |
| Inserts | in batches of 1,000 or more rows | a few rows per query |
Choose ClickHouse when data arrives as a stream and needs to be counted: events, logs, metrics, dashboards, reports. Leave PostgreSQL to bookkeeping systems: an application needs frequent point changes and transactions. The two are often combined: transactions live in PostgreSQL, and its changes are replicated to ClickHouse for analytics.
What you need to know in advance
- SQL: SELECT, WHERE, GROUP BY — covered in the SQL reference; only the ClickHouse dialect specifics are covered here.
- Events and their dimensions: an event is a row, dimensions are columns; data types in ClickHouse are covered in the section “Data types and table schema”.
- The table engine determines how data is stored and merged; the examples throughout this reference all use
MergeTree— see the section “Table engines”.
Section sources
the section is written from these pages- What is ClickHouse?clickhouse.com/docs/get-started/about/intro(opens in a new tab)
- Architecture overviewclickhouse.com/docs/concepts/core-concepts/academic-overview(opens in a new tab)
- Comparing PostgreSQL and ClickHouseclickhouse.com/docs/get-started/migrate/postgres/overview(opens in a new tab)
- 13.1. Introductionpostgresql.org/docs/18/mvcc-intro.html(opens in a new tab)