---
title: "Using Materialized Views in ClickHouse"
description: "ClickHouse materialized views act as insert-time triggers that write the result of a SELECT into a target table, letting expensive aggregations be precomputed."
url: "https://swe.quest/articles/using-materialized-views-in-clickhouse-f547ca7b5675b5e9"
canonical: "https://clickhouse.com/blog/using-materialized-views-in-clickhouse"
source: "ClickHouse"
published: "2023-01-19"
difficulty: "medium"
kind: "How-to"
reading_minutes: 13
tags: ["analytics","sql","performance","data-pipelines"]
---

# Using Materialized Views in ClickHouse

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2023-01-19, 13 min read, How-to. Original: [https://clickhouse.com/blog/using-materialized-views-in-clickhouse](https://clickhouse.com/blog/using-materialized-views-in-clickhouse)

## Summary

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.

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)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Data pipelines](https://swe.quest/tags/data-pipelines.md) (Data & databases)

## Prerequisites

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

## Keywords

ClickHouse, Materialized Views, SummingMergeTree, AggregatingMergeTree, Null engine, TTL

## Related posts

- [Chaining Materialized Views in ClickHouse](https://swe.quest/articles/chaining-materialized-views-in-clickhouse-79c630686e93ebe9.md): ClickHouse · 2024-04-16 · Medium · 8 min read · Analytics & OLAP, Stream processing, SQL
  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.
- [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.
- [Harnessing the Power of Materialized Views and ClickHouse for High-Performance Analytics at Inigo](https://swe.quest/articles/harnessing-the-power-of-materialized-views-and-clickhouse-for-high-performance-ebcfa2d951da7c43.md): ClickHouse · 2023-06-23 · Medium · 8 min read · Analytics & OLAP, Databases, SQL, Performance engineering
  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.
- [ClickHouse Newsletter June 2022: Materialized, but still real-time](https://swe.quest/articles/clickhouse-newsletter-june-2022-materialized-but-still-real-time-9ad5be45d6c0df99.md): ClickHouse · 2022-06-09 · Medium · 6 min read · Analytics & OLAP, SQL, Performance engineering
  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.
- [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.

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)
