Using Materialized Views in ClickHouse
ClickHouse materialized views act as insert-time triggers that write the result of a SELECT into a target table, letting expensive aggregations be precomputed. On a 1B-row Wikistat dataset, a SummingMergeTree-backed view cuts a top-projects query from 15 seconds to 3 ms, and AggregatingMergeTree with *State()/*Merge() combinators handles min/max/avg the same way. The same mechanism supports compacting history (with source-table TTL), validating and filtering rows via a Null-engine source table, routing invalid data to separate tables, and transforming columns. Practical guidance: keep views per table to around ten or fewer, still GROUP BY at query time since merges are asynchronous, avoid FINAL on large tables, and only aggregate for storage when it reduces rows by roughly 10x.
Related posts
- Chaining Materialized Views in ClickHouse
ClickHouse mediumAdept
ClickHouse materialized views can be chained so that one view reads from a table populated by another, rather than having several views all read from the same Kafka engine table. Using the Wikimedia recent-changes stream, the walkthrough ingests raw events from Kafka into a MergeTree table and builds per-minute aggregation states (uniqState, sumState) in an AggregatingMergeTree. It then adds a 10-minute rollup view that reads those states with -MergeState combinators. The new view only picks up rows ingested after it is created, so older data has to be backfilled with an INSERT ... SELECT, and queries finish with -Merge combinators.
- ClickHouse Release 23.12
ClickHouse mediumAdept
ClickHouse 23.12 adds experimental Refreshable Materialized Views, which periodically re-run an arbitrary query and atomically replace the results in a target table. That makes complex multi-join aggregations practical where incremental insert-trigger views fall short, and they can also serve as cron-style import/export jobs chained with DEPENDS. In the IMDB example, a refreshable view cuts an actor-summary query from 1.2 s to 3 ms. The release also speeds up SELECT ... FINAL by splitting data into non-intersecting sorting-key ranges, which are processed in parallel without merge logic, so only intersecting ranges pay the merge cost and merges are skipped across partitions when the partition key is a prefix of the sorting key.
- Harnessing the Power of Materialized Views and ClickHouse for High-Performance Analytics at Inigo
ClickHouse mediumAdept
Inigo stores analytics on billions of GraphQL API requests in ClickHouse after rejecting Snowflake as too slow and costly for customer-facing data and lacking a local emulator, and PostgreSQL because its indexes and costs blew up past roughly 100K–1M rows. Raw tables keyed on observed_at feed AggregatingMergeTree materialized views bucketed at 1-minute, 6-minute, 40-minute and 3-hour intervals, so most queries scan thousands of aggregated rows instead of millions of raw ones. Counts are stored as a calls column and read back with sum(calls), percentiles use quantilesState in the view and quantileMerge at query time, and high-cardinality fields like trace_id stay in raw tables only. The result is month-long retention in views versus a week of raw data, lower database load, and fast local integration tests, at the cost of setup complexity and no transactions.
- ClickHouse Newsletter June 2022: Materialized, but still real-time
ClickHouse mediumAdept
Point-in-time queries over incomplete time series can combine a materialized view of 15-minute argMax aggregates with raw data for the latest partial interval, unioned and re-aggregated. This keeps answers accurate to the requested timestamp while cutting query time by 98% on a 200M-row dataset, with full CREATE TABLE, materialized view and query SQL shown. The ClickHouse v22.5 release turns on memory overcommit by default, adds parallel hash joins (up to 80% faster large joins), GROUPING SETS and FIPS compliance via BoringSSL.
- Building a product analytics solution with ClickHouse
ClickHouse mediumAdept
ClickHouse's in-house product analytics platform, Galaxy, stores over 20 billion events (14 TB, roughly 14x compression) in a single denormalized MergeTree table keyed on created_at. A single sparse table avoids join overhead, since ClickHouse's sparse serialization makes empty columns cheap. Columns used for cohorts and filters should be top-level rather than buried in a JSON payload. Incremental materialized views speed up the few dozen recurring queries, and a custom SDK handles event capture.