Lesson 5 of 9 · 25m
Schema Design for Event Analytics
Types, codecs, low-cardinality dimensions, nested JSON, timestamps, and the practical table shapes that stay fast after a year of product churn.
A ClickHouse schema is a performance feature. Column types decide compression and CPU cost. Nullable columns add overhead. High-cardinality strings change memory behavior. JSON convenience today can become a tax you pay on every dashboard for years. Good schema design is less about normalization and more about making common questions cheap.
Use narrow, honest types
Do not store numbers as strings because ingestion was easier that afternoon. Use UInt64 for counters, UInt32 where ranges fit, DateTime or DateTime64 for time, and Decimal where exact decimal math matters. Narrower types mean fewer bytes and less decode work.
CREATE TABLE product_events
(
event_time DateTime64(3),
tenant_id UInt64,
user_id UInt64,
event_name LowCardinality(String),
country LowCardinality(FixedString(2)),
bytes UInt64,
properties String
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(event_time)
ORDER BY (tenant_id, event_name, event_time);LowCardinality(String) is one of the most useful types in analytics. It dictionary-encodes repetitive strings such as event names, countries, browsers, and plan tiers. It is not magic for unbounded IDs; if cardinality is high, the dictionary stops helping and can hurt.
Nullable is not free
Nullable(String) stores an extra null mask and changes execution paths. Use it when null is semantically meaningful. For event analytics, empty strings, zero values, or explicit sentinel categories are sometimes cheaper and clearer. Do not cargo-cult either answer — decide what the data means.
JSON is a contract problem
ClickHouse can store JSON as a String and extract fields at query time, and newer versions have stronger JSON support. Convenient ingestion is seductive: producers can add fields without a migration. The cost appears later when every query parses a blob and every analyst guesses at key names.
A pragmatic split works well:
- promote hot, query-critical properties into typed columns;
- keep the raw event or a compact properties payload for forensics;
- version event names deliberately, such as
checkout_started.v2, when semantics change.
Time deserves more respect than it gets
Store event time in UTC. Keep ingest time separate if late arrivals matter. A DateTime64(3) column gives millisecond precision without floating-point nonsense. Decide whether late events update aggregates or merely land in raw storage. That choice affects materialized views, backfills, and every argument about why a dashboard changed overnight.
Compression codecs are part of schema design
ClickHouse lets you choose codecs per column:
event_time DateTime CODEC(Delta, ZSTD(1)),
bytes UInt64 CODEC(T64, ZSTD(3))Delta is often excellent for monotonically increasing timestamps. T64 can help integer sequences. Test on realistic data, because compression ratios and CPU tradeoffs depend on shape. The best codec on a benchmark post may be wrong for your distribution.
Field notes
- Put tenant or customer identity in the sorting key when tenant isolation is the dominant query pattern.
- Keep high-cardinality fields such as request IDs out of low-cardinality encoding and usually out of leading sort position.
- A wide table with stable typed columns often beats an entity-attribute-value design. EAV in ClickHouse recreates the worst parts of row-oriented schema pain.
- Document units.
bytes,duration_ms, andamount_centscolumns prevent a special kind of analytical folklore. - Schema migrations are cheap for new partitions and painful for old parts. Plan rewrites explicitly instead of assuming type changes propagate backward.
Was this lesson helpful?