---
title: "Chaining Materialized Views in ClickHouse"
description: "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."
url: "https://swe.quest/articles/chaining-materialized-views-in-clickhouse-79c630686e93ebe9"
canonical: "https://clickhouse.com/blog/chaining-materialized-views"
source: "ClickHouse"
published: "2024-04-16"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["analytics","stream-processing","sql"]
---

# Chaining Materialized Views in ClickHouse

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2024-04-16, 8 min read, How-to. Original: [https://clickhouse.com/blog/chaining-materialized-views](https://clickhouse.com/blog/chaining-materialized-views)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [Stream processing](https://swe.quest/tags/stream-processing.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- SQL aggregation ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- ClickHouse MergeTree tables ([easy posts on Analytics & OLAP](https://swe.quest/tags/analytics.md?difficulty=easy))
- Kafka basics ([easy posts on Queues & messaging](https://swe.quest/tags/messaging.md?difficulty=easy))

## Keywords

ClickHouse, materialized views, AggregatingMergeTree, Kafka table engine, kcat, Wikimedia recent changes

## Related posts

- [Using Materialized Views in ClickHouse](https://swe.quest/articles/using-materialized-views-in-clickhouse-f547ca7b5675b5e9.md): ClickHouse · 2023-01-19 · Medium · 13 min read · Analytics & OLAP, SQL, Performance engineering, Data pipelines
  ClickHouse materialized views act as insert-time triggers that write the result of a SELECT into a target table, letting expensive aggregations be precomputed.
- [ClickHouse Release 23.12](https://swe.quest/articles/clickhouse-release-23-12-36e0c0d21cc33770.md): ClickHouse · 2024-01-11 · Medium · 16 min read · Analytics & OLAP, Query engines, SQL, Performance engineering
  ClickHouse 23.12 adds experimental Refreshable Materialized Views, which periodically re-run an arbitrary query and atomically replace the results in a target table.
- [Building a product analytics solution with ClickHouse](https://swe.quest/articles/building-a-product-analytics-solution-with-clickhouse-786424348993d92f.md): ClickHouse · 2024-12-05 · Medium · 24 min read · Analytics & OLAP, SQL, Databases, Product engineering
  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.
- [Building a Medallion architecture with ClickHouse](https://swe.quest/articles/building-a-medallion-architecture-with-clickhouse-9488d8022c239606.md): ClickHouse · 2024-12-12 · Medium · 11 min read · Analytics & OLAP, Data pipelines, System design
  The Medallion architecture's bronze, silver and gold tiers can be built entirely from native ClickHouse features instead of Spark and Delta Lake.
- [Build a Real-time Materialized View from Postgres Changes using Confluent’s ksqlDB](https://swe.quest/articles/build-a-real-time-materialized-view-from-postgres-changes-using-confluent-s-cfa507d6c7070e59.md): Neon · 2024-02-28 · Medium · 10 min read · Event-driven architecture, PostgreSQL, Stream processing, Queues & messaging
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
