Overview

A ClickHouse materialized view is an insert trigger: each block inserted into a source table is transformed by the view’s SELECT and written to a target table. It is the standard way to pre-aggregate, reshape or route data at ingestion time. It does not recompute from scratch.

Core concepts

  • Syntax: CREATE MATERIALIZED VIEW v TO target AS SELECT ... FROM source. Always use TO target so the target is an explicit table with the engine you choose.
  • The query runs only on the new block, not on the whole source table. Joins in the view apply to the new block against the right-hand table at that moment.
  • Not backfilled: rows that existed before the view was created are not processed; backfill manually with INSERT INTO target SELECT ....
  • Typical targets:
    • SummingMergeTree: rows with the same key are summed at merge time (numeric columns).
    • AggregatingMergeTree: stores partial aggregate states (sumState, uniqState…), merged on read with -Merge functions; right for distinct counts and averages.
  • Because merging is asynchronous, always aggregate again at query time (SELECT key, sum(x) FROM target GROUP BY key).
  • Refreshable materialized views (REFRESH EVERY ...) recompute periodically, closer to scheduled queries.

Worked example

CREATE TABLE mv_src (ts DateTime, country LowCardinality(String), amount Float64) ENGINE = MergeTree ORDER BY ts;
CREATE TABLE mv_daily (day Date, country LowCardinality(String), orders UInt64, revenue Float64)
ENGINE = SummingMergeTree ORDER BY (day, country);
CREATE MATERIALIZED VIEW mv_daily_v TO mv_daily AS
SELECT toDate(ts) AS day, country, count() AS orders, sum(amount) AS revenue FROM mv_src GROUP BY day, country;
 
INSERT INTO mv_src VALUES ('2025-03-01 10:00:00','VN',10),('2025-03-01 11:00:00','VN',5),('2025-03-01 12:00:00','US',7);
INSERT INTO mv_src VALUES ('2025-03-01 13:00:00','VN',20);

Two inserts created two parts in mv_daily, so a plain SELECT returned the VN key twice (VN 2 15 and VN 1 20). Querying with GROUP BY returned the right answer (VN 3 35, US 1 7); after OPTIMIZE TABLE mv_daily FINAL a plain SELECT returned the merged row as well.

Backfill behaviour, verified: a view created after two rows already existed in the source showed 0 rows in its target; a later insert of 5 produced a target row 1 | 5, ignoring the earlier values.

Pitfalls and production notes

  • Treat the target as an append-only aggregate; never rely on a merge having happened.
  • The view fires per insert block; very small inserts create many small target parts (ch-ingestion).
  • An error in the view makes the source insert fail (unless you configure otherwise); test view logic with SELECT first.
  • Changing a view’s query requires dropping and recreating it plus a backfill plan.
  • For one-time or ad-hoc rollups, a normal query or a projection may be simpler (ch-projections).

Practice questions

  1. Why must you still use GROUP BY when querying a SummingMergeTree target?
  2. How would you backfill a new materialized view over 10 TB of history safely?
  3. Which engine and functions would you use for unique visitors per day?

References

  • ClickHouse documentation, Materialized View; AggregatingMergeTree; SummingMergeTree

ch-projections · ch-replacing-collapsing-engines · ch-ingestion