knaxuslearn · build · ship

Projects / Web analytics / Wrap up

Wrap up

Keep going

  • TTL. Add a rule to delete events after 90 days: TTL event_time + INTERVAL 90 DAY.
  • Projections. Add a projection ordered by url, and run Step 3.5 again. How many granules now?
  • Sessions. Group events into visits by a gap of 30 minutes between events from the same user. Look up window functions.
  • Replication. Read about ReplicatedMergeTree and how a cluster keeps copies.

What this version leaves out, and what a real system does

  • We run one server. Real clusters have shards, to spread the data, and replicas, to survive a failure.
  • Updates and deletes are expensive in ClickHouse, because parts are immutable. Real designs append events, and use special engines or a new row to correct things.
  • We loaded with one INSERT SELECT. Real ingestion comes from Kafka or a collector that batches events.
  • The sizes and timings of a laptop container are not those of a production machine. The ideas hold; the numbers will not.
  • Privacy is missing. Real analytics systems decide what to store about a person, and for how long.

Check yourself

Answer each question in your head first, then open it to check.

  1. Why does a column store read less than a row store for a query on two of six columns?
    Each column lives in its own file, so the query opens only the files for the columns it uses. A row store keeps all six together and reads them even when only two are needed.
  2. What is a part, and why is inserting one row at a time a bad idea?
    A part is an immutable folder of column files that one insert creates. Many tiny parts slow down reads and force many merges, so inserts should be batched.
  3. In Step 3.4 the plan read 2 of 136 granules, and in Step 3.5 it read 245 of 245. What decided the difference?
    The ORDER BY key. The first query filtered on the leading columns of the sort key, which allowed a binary search. The second filtered on a column that is not part of the key, so nothing could be skipped.
  4. Why must you read a SummingMergeTree table with sum and GROUP BY?
    Rows with the same key are only added together when parts are merged, which happens in the background. Until then the same key can appear in several rows, and the sum gives the right answer regardless.
  5. When would you choose uniqExact over uniq?
    When the number is used for billing, a limit or a check, where a small error is a problem. For a dashboard an estimate is enough and costs much less memory.
  6. Teach it back: explain to a teammate what a sparse index is and why it is small.
    A sparse index keeps one entry per block of 8,192 rows instead of one per row. It is small enough to sit in memory, and because the data is sorted, it lets the database jump close to the rows it needs and read a few blocks.

Sources