knaxuslearn · build · ship

Projects / Web analytics / Phase 2

Phase 2 of 4 · about 2.5 hours

Two million events

You end with two million events, and a table of how many bytes each column takes.

Step 2.1: Generate and load (about 40 minutes)

numbers(2000000) is a built-in table of the numbers 0 to 1,999,999. We turn each number into an event with arithmetic and a hash function, cityHash64, so the data looks random but is the same every time. The countries and devices are skewed on purpose, as real traffic is: about 35% from India and 60% mobile. First we empty the table with TRUNCATE.

TRUNCATE TABLE web.events;
INSERT INTO web.events
SELECT
    toDateTime('2026-10-01 00:00:00') + intDiv(number * 2592000, 2000000) AS event_time,
    toUInt32(cityHash64(number) % 50000) AS user_id,
    arrayElement(['/', '/pricing', '/docs', '/blog', '/signup', '/login', '/blog/clickhouse', '/docs/install'], 1 + cityHash64(number, 1) % 8) AS url,
    multiIf(cityHash64(number, 2) % 100 < 35, 'IN', cityHash64(number, 2) % 100 < 60, 'US', cityHash64(number, 2) % 100 < 72, 'DE', cityHash64(number, 2) % 100 < 82, 'BR', cityHash64(number, 2) % 100 < 92, 'GB', 'JP') AS country,
    multiIf(cityHash64(number, 3) % 100 < 60, 'mobile', cityHash64(number, 3) % 100 < 95, 'desktop', 'tablet') AS device,
    toUInt32(50 + cityHash64(number, 4) % 5000) AS duration_ms
FROM numbers(2000000);
SELECT count() AS events, uniqExact(user_id) AS users, min(event_time) AS first_event, max(event_time) AS last_event FROM web.events;
┌──events─┬─users─┬─────────first_event─┬──────────last_event─┐
│ 2000000 │ 50000 │ 2026-10-01 00:00:00 │ 2026-10-30 23:59:58 │
└─────────┴───────┴─────────────────────┴─────────────────────┘

Exactly 2,000,000 events from 50,000 users over 30 days. This is the data we will use for the rest of the project.

Step 2.2: What it costs on disk (about 30 minutes)

system.parts_columns has one row per column per part. We add up the sizes before and after compression:

SELECT
    column AS name,
    type,
    formatReadableSize(sum(column_data_uncompressed_bytes)) AS uncompressed,
    formatReadableSize(sum(column_data_compressed_bytes)) AS compressed
FROM system.parts_columns
WHERE database = 'web' AND table = 'events' AND active
GROUP BY column, type
ORDER BY sum(column_data_uncompressed_bytes) DESC;
┌─name────────┬─type───────────────────┬─uncompressed─┬─compressed─┐
│ url         │ String                 │ 29.81 MiB    │ 9.92 MiB   │
│ event_time  │ DateTime               │ 7.63 MiB     │ 7.66 MiB   │
│ user_id     │ UInt32                 │ 7.63 MiB     │ 7.39 MiB   │
│ duration_ms │ UInt32                 │ 7.63 MiB     │ 5.70 MiB   │
│ device      │ LowCardinality(String) │ 1.91 MiB     │ 997.95 KiB │
│ country     │ LowCardinality(String) │ 1.91 MiB     │ 9.46 KiB   │
└─────────────┴────────────────────────┴──────────────┴────────────┘

Look at three things. country has 2 million values and shrank to a few KiB, because it has six distinct values and the data is sorted by it. url is the biggest column, as it is long text. event_time barely shrinks. Where the values repeat and the data is sorted, compression is dramatic, and that is another reason columns are cheap to scan.

Think it through: device took much more space than country. They both have few values. What is the difference in how the table is sorted?

Step 2.3: Partitions (about 25 minutes)

All our events are in October, so there is one partition. Let's see how the rows and parts are laid out:

SELECT partition, count() AS parts, sum(rows) AS rows
FROM system.parts
WHERE database = 'web' AND table = 'events' AND active
GROUP BY partition
ORDER BY partition;
┌─partition─┬─parts─┬────rows─┐
│ 202610    │     2 │ 2000000 │
└───────────┴───────┴─────────┘

Phase 2 done

Two million events are in, and you know what each column costs. Now we ask questions.