clickhouse projection vs materialized view
Compares ClickHouse projections and materialized views for precomputation. Use it when choosing between them, when an MV is too expensive, or when a query pattern needs a different sort order. Not for other databases.
TL;DR
Projections are extra sort orders stored with the table itself: transparent to queries and cheap to maintain. Materialized views are separate tables filled by insert triggers: more flexible (they can aggregate, filter, or join) but they cost insert throughput and storage. Use projections when the same table just needs another ORDER BY pattern; use materialized views when you need a different grain, an aggregation, or data from another table.
The query
clickhouse projection vs materialized viewUse this when
- you are choosing between a projection and a materialized view in ClickHouse
- a materialized view is slowing down inserts and you want a cheaper option
- queries need a different sort order than the table's primary key
Not for
- plain MergeTree tuning, which is a separate topic
- precomputation in other databases or warehouses
Steps
- List what the query needs: same rows in a new order means projection; fewer rows, aggregates, or joins mean materialized view.
Expected output: You have a clear answer for which feature matches the requirement.
- For projections, define the projection with the ORDER BY your slow queries use, then let the optimizer pick it up automatically.
Expected output: EXPLAIN shows the query using the projection without any query rewrite.
- For materialized views, decide between a TO-table (explicit target, flexible) and the implicit inner table, and keep the MV query simple.
Expected output: The MV populates on insert and queries against it return fresh results.
- Measure insert impact before and after adding either. MVs can noticeably slow high-rate ingestion.
Expected output: You have insert throughput numbers with and without the MV.
- Avoid stacking both on the same access pattern. One precomputation layer is enough; two is maintenance debt.
Expected output: Each slow query pattern has exactly one precompute strategy.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstrWBvsuOXIQv2-_9VZRC-A
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.