ClickHouse Cheat Sheet
ClickHouse columnar OLAP database covering table engines, MergeTree ordering keys, materialized views, and analytical SQL.
2 PagesIntermediateFeb 26, 2026
MergeTree Table Definition
The core table engine for analytical workloads, with ordering and partitioning.
sql
CREATE TABLE events( event_date Date, event_time DateTime, user_id UInt64, event_type LowCardinality(String), properties String)ENGINE = MergeTreePARTITION BY toYYYYMM(event_date)ORDER BY (event_type, user_id, event_time)TTL event_date + INTERVAL 90 DAYSETTINGS index_granularity = 8192;
Analytical Query Patterns
Common ClickHouse SQL idioms for fast aggregation over huge tables.
sql
-- Approximate distinct count (fast, low memory)SELECT uniqCombined(user_id) FROM events WHERE event_date >= today() - 7;-- Array-based funnel-style aggregationSELECT event_type, count() AS cntFROM eventsWHERE event_date >= today() - 1GROUP BY event_typeORDER BY cnt DESCLIMIT 10;-- Quantiles in one passSELECT quantiles(0.5, 0.95, 0.99)(response_ms) FROM requests;-- FINAL forces merge-time dedup for ReplacingMergeTree readsSELECT * FROM users FINAL WHERE id = 42;
Materialized View for Real-Time Rollups
Incrementally aggregate incoming rows into a summary table as they're inserted.
sql
CREATE TABLE events_by_hour( hour DateTime, event_type LowCardinality(String), cnt AggregateFunction(count))ENGINE = AggregatingMergeTreeORDER BY (event_type, hour);CREATE MATERIALIZED VIEW events_by_hour_mv TO events_by_hour ASSELECT toStartOfHour(event_time) AS hour, event_type, countState() AS cntFROM eventsGROUP BY hour, event_type;-- Query the rollupSELECT hour, event_type, countMerge(cnt) FROM events_by_hour GROUP BY hour, event_type;
Table Engine Cheat Sheet
Which MergeTree variant to reach for.
- MergeTree- the base engine; use for general append-only analytical data
- ReplacingMergeTree- deduplicates rows with the same ORDER BY key on merge, needs `FINAL` or `OPTIMIZE` for guaranteed correctness
- SummingMergeTree- automatically sums numeric columns for rows sharing an ORDER BY key
- AggregatingMergeTree- stores partial aggregate states, paired with materialized views for rollups
- CollapsingMergeTree- uses a sign column to cancel out old versions of a row, for mutable-record patterns
- Distributed- a virtual table that fans queries out across shards in a cluster
Pro Tip
Design your ORDER BY key around your most common WHERE/GROUP BY filters, not around uniqueness — unlike a primary key in Postgres, ClickHouse's ORDER BY key is a sparse sort index, and getting it wrong means full-partition scans instead of skipped granules.
Was this cheat sheet helpful?
Explore Topics
#ClickHouse#ClickHouseCheatSheet#Database#Intermediate#MergeTreeTableDefinition#AnalyticalQueryPatterns#Materialized#View#Databases#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance