Learn Labs
Query Speed

Schema Lab: ORDER BY, Indexes, Codecs and Precomputing

The same 20 million rows under different schemas: rows read, bytes on disk, and query time

labs/schema.py and labs/query_speed.py load 20 million synthetic web events (30 days, 1 million users, 40 countries with a skewed distribution, 20,000 URLs) and change one thing at a time.

docker compose -f compose_prod.yaml run --rm labs python schema.py
docker compose -f compose_prod.yaml run --rm labs python query_speed.py

First, a measuring mistake

The first run said that looking up one user in a table sorted by ts read 1.7 million rows. It can't: user_id isn't in the sort key, so ClickHouse has to scan all 20 million. The lab ran every query three times and kept the fastest, and the second run was being helped by the query condition cache (on by default in recent versions). It remembers which granules failed a WHERE clause and skips them next time.

first run51.9 ms, 20,000,000 rows readsecond run (cache hit)4.6 ms, 1,703,936 rows readthird run, cache off28.3 ms, 20,000,000 rows read

That cache is great in production: dashboards repeat the same filters. But it makes a benchmark measure the cache, not the schema. The lab's timing helper now sets use_query_condition_cache = 0. Every number below is without it.

ORDER BY decides what a query reads

ORDER BY (ts)one user's events20,000,000 rowsIN, one day, by hour671,744errors, last 24 h666,880ORDER BY (user_id, ts)one user's events8,192IN, one day, by hour20,000,000errors, last 24 h20,000,000ORDER BY (country, ts)one user's events20,000,000IN, one day, by hour204,800errors, last 24 h1,002,752

Each sort order makes one kind of query read a few thousand rows and another read all 20 million. There is no order that is good for everything. Pick it for the queries you run most: by time and country for a dashboard, by user for a per-user page. A per-user lookup on (user_id, ts) read one granule, 8,192 rows, in 1.3 ms. On (ts) it read every row in 26 ms.

A skip index only helps rare, clustered values

Query on ORDER BY (country, ts)Rows readTime
url = '/product/777', no index20,000,000167 ms
same, bloom_filter on url16,318,464147 ms
user_id = 424242, bloom_filter on user_id1,146,8808.8 ms

The URL index skipped almost nothing. Each of the 20,000 URLs appears about 1,000 times, spread evenly through the table, so nearly every block of granules contains /product/777. A user appears about 20 times, so most blocks don't contain them and the bloom filter rules those out.

Codecs and LowCardinality

ts (DateTime)LZ4 (default)60.0 MBDelta + ZSTD(3)13.9 MBduration_ms (UInt32)LZ4 (default)54.3 MBZSTD(3)36.1 MBuser_agent (String)LZ4 (default)163.9 MBZSTD(3)67.6 MBcountry, GROUP BY timeString93.4 msLowCardinality(String)22.8 ms

Delta stores the difference between neighbouring timestamps, which in a sorted column are tiny: 4.3x smaller. LowCardinality(String) stores a dictionary and small integers: the GROUP BY read 19 MB instead of 95 MB and ran 4x faster.

Materialized views: counts shrink, quantiles don't

An incremental materialized view keeps per-minute aggregates as rows arrive. It made inserts slower (10 million rows: 0.9 s without the view, 1.6 s with it), and then:

hits and p95 per hourraw rows (2.5 M read)48.3 msview (98 k rows, 13.1 MB)45.4 mshits per hour onlyraw rows (2.5 M read)12.2 msview (98 k rows, 2.0 MB)2.4 ms

For counts, the view is 5x faster. For counts plus p95 it is barely faster, though it reads 25 times fewer rows. The view's table explains it: of its 61 MB, 57.8 MB is the quantileState column. Each per-minute state holds a sample of values, and merging 86,000 of them costs nearly as much as computing p95 from the raw rows. Pre-aggregate what shrinks (counts, sums, uniq sketches at a coarser grain), and check the size of every state column.

Projections: a second copy, sorted differently

A projection sorted by user_id on the (country, ts) table:

one user's events (ORDER BY country, ts)     25.4 ms  read   20,000,000 rows
building the projection took 4.1s
same query, with projection by_user           1.2 ms  read        8,192 rows
table 195.9 MB, projection by_user 145.5 MB more

It works like a second sort key, at the price of most of a second copy of the table. The first version of the lab reported no extra disk use at all: system.parts doesn't count projection parts; system.projection_parts does.

On this page