Materialized views vs projections in ClickHouse: when each is the right pick

Two ways to make a slow query fast in ClickHouse. They look similar and they behave differently. Here is the honest tradeoff.

ClickHouse ships two mechanisms for turning a slow aggregation query into a fast one: materialized views and projections. They look similar - both pre-compute something, both make a query cheaper - and teams pick badly between them because the failure modes look alike on the happy path.

This post walks through the mechanical difference, the write cost, the consistency semantics, the query-rewrite behaviour, and the four query shapes where each of them is the honest pick.

The mechanical difference

A materialized view (MV) in ClickHouse is an insert trigger on a source table that writes a transformed row into a destination table. When a row lands in raw_orders, the MV computes sum(amount) per (tenant_id, toStartOfMonth(created_at)) and inserts the aggregated row into a target SummingMergeTree table. Docs at ClickHouse materialized views.

A projection is an alternate storage layout attached to a table. When you define a projection on orders sorted by plan_id, ClickHouse stores the same rows twice - once in the primary sort order and once in the projection’s sort order. At query time, the optimiser picks whichever storage layout serves the query cheaper. Docs at ClickHouse projections.

The difference: an MV is a new object that you query explicitly. A projection is a secondary index that is picked transparently.

Write cost

Both cost extra writes. That is the tax you pay.

An MV writes one row per source row per MV, plus a background merge of the aggregate state (for AggregatingMergeTree and SummingMergeTree). If you have five MVs on raw_orders, every insert into raw_orders writes six rows in total.

A projection writes one row per source row per projection. If you have three projections on orders, every insert writes four physical rows.

The linear write amplification is the same. What differs is what happens after: an MV writes to a separate table with independent storage, retention, and TTL. A projection stays in the same table part, and if the source part is dropped, the projection goes with it.

Consistency semantics

This is where the picks diverge.

MVs are eventually consistent. The insert trigger fires synchronously, but the underlying aggregate state (for SummingMergeTree or AggregatingMergeTree) is merged in the background. Read the MV before the merge finishes and you see partial state - correct in the aggregate sense (all rows are there) but potentially not fully collapsed (the same key might appear in multiple parts).

Projections are strongly consistent with the source. A projection lives inside the same part as the source. If the source part is queryable, the projection is queryable, and they agree by construction.

The MV story is not a bug; it is the shape of the mechanism. Queries with FINAL or GROUP BY on the read side handle the eventual-consistency correctly. But if your query is a naive SELECT sum(x) FROM mv_totals WHERE key = ?, and the merge is behind, you get the wrong sum.

Query rewrite

A projection is transparent - you query the source table, and ClickHouse’s optimiser rewrites the query to hit the projection when the projection is cheaper. This is genuinely automatic.

An MV is not transparent. You query the MV explicitly. If a business user writes SELECT sum(amount) FROM orders WHERE month = ? against the source, the MV is not touched. Getting the MV picked requires either (a) educating users to query the MV directly, or (b) putting a semantic layer or view on top that routes the query to the MV.

The upshot: projections give you speedup with no application changes. MVs give you speedup with application (or semantic-layer) changes, but they scale to shapes projections cannot handle.

Query shape #1: same table, different sort order

You have orders sorted by (created_at, customer_id). A common query filters by plan_id. The query scans a lot of parts because the primary sort key does not help.

Projection is the honest pick. Define a projection sorted by plan_id, and every filter-by-plan query rewrites transparently.

ALTER TABLE orders
ADD PROJECTION by_plan
(
    SELECT * ORDER BY plan_id, created_at
);
ALTER TABLE orders MATERIALIZE PROJECTION by_plan;

Query shape #2: pre-aggregated rollup on a large fact

You have a fact table with a billion rows and every dashboard queries sum(revenue) GROUP BY month. The query is expensive because it scans the fact table.

MV is the honest pick. A SummingMergeTree MV pre-aggregates by month, and the dashboards query the MV (10,000 rows) instead of the fact (a billion).

CREATE MATERIALIZED VIEW mv_monthly_revenue
ENGINE = SummingMergeTree
ORDER BY (tenant_id, month)
AS SELECT
    tenant_id,
    toStartOfMonth(created_at) AS month,
    sum(amount)                AS revenue
FROM raw_orders
GROUP BY tenant_id, month;

A projection cannot do this because a projection stores the same rows in a different order - it does not pre-aggregate them. This is the shape MVs are for.

Query shape #3: subset filter that runs constantly

You have events with billions of rows, and every product query filters event_type = 'purchase' first. That single filter scans a lot of data.

Projection with a WHERE clause is the honest pick. ClickHouse projections support WHERE in the definition, so you get a “materialised subset” without duplicating aggregate math.

Query shape #4: cross-table derived aggregate

You want the daily rollup of orders JOIN customers ON tenant, sum(amount) per country. This spans two source tables.

MV is the only option. Projections live inside one table. Cross-table pre-aggregation is what MVs are for. The MV consumes inserts from orders and joins to a dictionary or refreshable view of customers.

Failure modes worth naming

MV goes stale silently. If the MV target table hits a merge error, or its target’s disk fills, or the MV was created with a bad SELECT, the pipeline stops. The source table keeps taking inserts. Nobody notices until a dashboard reads a stale rollup. The fix is to monitor system.mutations and system.merges and alert on MV target-table freshness.

Projection storage doubles the part size. Each projection roughly duplicates the source data on disk (with different compression, since the sort order is different). Three projections on a 1 TB table is a 4 TB table. Storage is cheap but not free.

Projection MATERIALIZE is not free. ALTER TABLE ... MATERIALIZE PROJECTION on a large table rewrites every part. Run it in a maintenance window on production. New inserts to the table will build the projection incrementally; the ALTER is for backfilling existing rows.

MV WHERE conditions can trip you up. An MV with a WHERE amount > 0 clause silently drops rows that fail the predicate. Later, when a business user asks “why does the MV disagree with the source by 3 percent,” the answer is upstream of the query.

The rule of thumb

  • Sort-order or filter speedup on one table: projection.
  • Pre-aggregated rollup on one large table: MV.
  • Cross-table derived aggregate: MV.
  • You want strong consistency and no application changes: projection.
  • You want a rollup that outlives the source’s retention: MV (with the MV target’s own TTL).

Most production ClickHouse BI stacks end up with both. Projections for the hot sort-order variants, MVs for the heavy rollups. The choice is not either-or; the choice is which one is the right pick for the specific slow query.

The semantic-layer angle

If you build a semantic layer on top of ClickHouse (see how to build a semantic layer in ClickHouse), the layer should own the choice of which MV or projection to route to. The semantic compiler picks the physical path; the user asks for a metric, and the compiler emits SQL against whichever storage layout is cheapest.

This is how Goni handles ClickHouse performance under the hood: the semantic-layer YAML declares metrics, the compiler analyses each metric’s typical query shape, and it proposes the MVs and projections that would collapse the cost. The DBA reviews and applies the proposal in a PR. The alternative (asking analysts to know which MVs exist) is how MVs end up unused past their first quarter.

Why this matters for a data team

ClickHouse’s speed is not automatic on wide fact tables. It is the sort key, plus one or two well-chosen materialised views or projections per hot query shape. Get the picks right, and dashboards render in 100 ms without an aggressive infrastructure spend. Get them wrong, and the cluster spends its CPU scanning columns that were never going to be part of the answer.