Projects / Web analytics / Phase 3
Phase 3 of 4 · about 2.5 hours
Questions and the sparse index
You end with five analytics queries, and a plan that shows why one is cheap and one is not.
Step 3.1: The most visited pages (about 25 minutes)
uniqExact(user_id) counts distinct visitors exactly:
SELECT url, count() AS views, uniqExact(user_id) AS visitors
FROM web.events
GROUP BY url
ORDER BY views DESC, url;
┌─url──────────────┬──views─┬─visitors─┐
│ /docs/install │ 250605 │ 49642 │
│ /blog/clickhouse │ 250541 │ 49673 │
│ /docs │ 250323 │ 49624 │
│ /signup │ 250042 │ 49647 │
│ /pricing │ 249965 │ 49667 │
│ / │ 249675 │ 49665 │
│ /login │ 249523 │ 49672 │
│ /blog │ 249326 │ 49635 │
└──────────────────┴────────┴──────────┘
The whole table was scanned, two million rows, and grouped. On a laptop this finishes in well under a second.
Step 3.2: Days and countries (about 30 minutes)
A per-day count and the speed of the site by country. quantile(0.5) is the median and quantile(0.95) is the time that 95% of events beat, a good way to see the slow tail:
SELECT
toDate(event_time) AS day,
count() AS views
FROM web.events
GROUP BY day
ORDER BY day
LIMIT 7;
SELECT
country,
count() AS views,
round(quantile(0.5)(duration_ms)) AS median_ms,
round(quantile(0.95)(duration_ms)) AS p95_ms
FROM web.events
GROUP BY country
ORDER BY views DESC;
┌────────day─┬─views─┐
│ 2026-10-01 │ 66667 │
│ 2026-10-02 │ 66667 │
│ 2026-10-03 │ 66666 │
│ 2026-10-04 │ 66667 │
│ 2026-10-05 │ 66667 │
│ 2026-10-06 │ 66666 │
│ 2026-10-07 │ 66667 │
└────────────┴───────┘
┌─country─┬──views─┬─median_ms─┬─p95_ms─┐
│ IN │ 700315 │ 2520 │ 4787 │
│ US │ 499646 │ 2514 │ 4807 │
│ DE │ 240102 │ 2519 │ 4790 │
│ BR │ 199952 │ 2576 │ 4796 │
│ GB │ 199775 │ 2567 │ 4799 │
│ JP │ 160210 │ 2596 │ 4796 │
└─────────┴────────┴───────────┴────────┘
Step 3.3: Shares (about 15 minutes)
A subquery inside the select gives the total, and we divide:
SELECT device, round(100 * count() / (SELECT count() FROM web.events), 1) AS percent
FROM web.events
GROUP BY device
ORDER BY percent DESC;
┌─device──┬─percent─┐
│ mobile │ 60 │
│ desktop │ 35 │
│ tablet │ 5 │
└─────────┴─────────┘
Step 3.4: Why one query is cheap (about 40 minutes)
ClickHouse does not have one index entry per row. It has a sparse index: one entry for every 8,192 rows, called a granule. Because the data is sorted by (country, event_time), a filter on those columns can skip most granules. EXPLAIN indexes = 1 shows the work:
EXPLAIN indexes = 1
SELECT count() FROM web.events WHERE country = 'JP' AND event_time >= '2026-10-10 00:00:00' AND event_time < '2026-10-11 00:00:00';
┌─explain──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Output: count() │
│ │
│ Aggregating │
│ │ Keys: │
│ │ Aggregates: count() │
│ │ Skip merging: 0 │
│ └──ReadFromMergeTree (web.events) │
│ Read type: Default │
│ Parts: 1 | Granules: 2 │
│ Output: Empty │
│ Prewhere filter │
│ Prewhere filter column: country = 'JP' AND event_time < '2026-10-11 00:00:00' AND event_time >= '2026-10-10 00:00:00' │
│ Indexes: │
│ Min-Max │
│ Keys: │
│ event_time │
│ Condition: and((event_time in (-Inf, 1791676799]), (event_time in [1791590400, +Inf))) │
│ Parts: 1/2 │
│ Granules: 136/245 │
│ Partition │
│ Keys: │
│ toYYYYMM(event_time) │
│ Condition: and((toYYYYMM(event_time) in (-Inf, 202610]), (toYYYYMM(event_time) in [202610, +Inf))) │
│ Parts: 1/1 │
│ Granules: 136/136 │
│ PrimaryKey │
│ Keys: │
│ country │
│ event_time │
│ Condition: and((event_time in (-Inf, 1791676799]), and((event_time in [1791590400, +Inf)), (country in ['JP', 'JP']))) │
│ Parts: 1/1 │
│ Granules: 2/136 │
│ Search Algorithm: binary search │
│ Ranges: 1 │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Find the PrimaryKey block in the output. It says Granules: 2/136: out of 136 granules in the October partition, the database needs to read 2. It found them with a binary search on the sort key.
Step 3.5: Why one query is not (about 25 minutes)
Now filter by url, which is not at the start of the sorting key:
EXPLAIN indexes = 1
SELECT count() FROM web.events WHERE url = '/pricing';
┌─explain─────────────────────────────────────────────────────────┐
│ Output: count() │
│ │
│ Aggregating │
│ │ Keys: │
│ │ Aggregates: count() │
│ │ Skip merging: 0 │
│ └──Filter ((WHERE + Change column names to column identifiers)) │
│ │ Filter column: url = '/pricing' │
│ └──ReadFromMergeTree (web.events) │
│ Read type: Default │
│ Parts: 2 | Granules: 245 │
│ Output: url │
│ Indexes: │
│ Min-Max │
│ Condition: true │
│ Parts: 2/2 │
│ Granules: 245/245 │
│ Partition │
│ Condition: true │
│ Parts: 2/2 │
│ Granules: 245/245 │
│ PrimaryKey │
│ Condition: true │
│ Parts: 2/2 │
│ Granules: 245/245 │
│ Ranges: 2 │
└─────────────────────────────────────────────────────────────────┘
Every number says 245/245. The index could not rule anything out, so every granule is read. The query still finishes fast, since scanning a single column is cheap, but with a billion rows the difference between 2 granules and all of them is the difference between a blink and a minute.
Step 3.6: The counts behind it (about 15 minutes)
Three counts to put in perspective how many rows each query had to deal with:
SELECT count() AS rows_read_all FROM web.events;
SELECT count() AS japan_rows FROM web.events WHERE country = 'JP';
SELECT count() AS pricing_rows FROM web.events WHERE url = '/pricing';
┌─rows_read_all─┐
│ 2000000 │
└───────────────┘
┌─japan_rows─┐
│ 160210 │
└────────────┘
┌─pricing_rows─┐
│ 249965 │
└──────────────┘
The Japan filter keeps 8% of the rows, and the index let us read that slice only. The pricing filter keeps 12.5%, but we could not find them without reading everything.
Real ClickHouse: the usual answer is to design the ORDER BY for your most common filters, put the column with the fewest distinct values first, and add a second table or a projection for another access pattern.
Phase 3 done
You asked five kinds of question and read, from the database itself, why the index helps one and not the other. Now we stop recomputing the same summary again and again.