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.pyFirst, 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.
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
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 read | Time |
|---|---|---|
url = '/product/777', no index | 20,000,000 | 167 ms |
same, bloom_filter on url | 16,318,464 | 147 ms |
user_id = 424242, bloom_filter on user_id | 1,146,880 | 8.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
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:
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 moreIt 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.