Chaining Materialized Views in ClickHouse
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.
Related posts
- Using Materialized Views in ClickHouse
ClickHouse mediumAdept
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.
- 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.
- 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.
- Building a Medallion architecture with ClickHouse
ClickHouse mediumAdept
The Medallion architecture's bronze, silver and gold tiers can be built entirely from native ClickHouse features instead of Spark and Delta Lake. Bronze uses insert-optimized MergeTree tables with the JSON type, materialized columns and TTL, fed by S3Queue, ClickPipes or Kafka. Silver is populated by incremental materialized views with dead-letter routing and ReplacingMergeTree for deduplication and CDC, which requires FINAL at query time. Gold uses refreshable materialized views for joins and denormalization, plus AggregatingMergeTree for precomputed aggregates, at the cost of data replication and extra pipeline management.
- Build a Real-time Materialized View from Postgres Changes using Confluent’s ksqlDB
Neon mediumAdept
Postgres logical replication can feed a Debezium-based CDC connector on Confluent Cloud that streams changes from Neon tables into Kafka topics, where ksqlDB aggregates them into a continuously updated materialized view. The walkthrough builds a player leaderboard: create a publication and pgoutput replication slot, configure the Postgres CDC Source connector with JSON_SR schemas, import topics as ksqlDB streams, and define a SUM aggregation table with EMIT CHANGES. Compared with Postgres REFRESH MATERIALIZED VIEW, this approach avoids repeated refresh costs, retains history in Kafka, and pushes updates to subscribers via the ksqlDB REST API.