Skip to content
About
Main content

View expansion and materialized-view rewriting

How can views push a query off the search cliff, and why are materialized views so often unused?

View expansion

View expansion is textually trivial and has a hidden hazard: it inflates the relation count. A query joining three views, each defined over five tables, becomes a 15-table join, which is past the . A stack of views does not degrade the plan gradually. It pushes the query off a discontinuity into greedy or genetic search, where the plan becomes both worse and unstable. Views are a plan-space hazard, not a runtime hazard. The symptom is a query that is fine until one more view layer is added and then is inexplicably terrible (Dev's problem in Join order search).

Materialized-view rewriting

A (MV) is a stored result of a query. MV rewriting solves the problem: can this query be answered from this stored result? The conditions are provable but restrictive:

  1. The MV's tables must cover the query's tables, or the missing joins must be provably lossless via enforced .
  2. The MV's predicate must be weaker than or equal to the query's, so the query's predicate can be applied as a residual filter on the MV's output.
  3. The required columns must be present or derivable, including any column needed to apply the . That is why MVs that project only a few columns are often unusable.
  4. The MV's grouping must be at equal or finer granularity, and the aggregate must be decomposable.

Example. The MV stores SUM(amount) per store_id, day. The query asks SUM(amount) per store_id, month. The MV grouping is finer (day is finer than month) and SUM rolls up (the sum of daily sums is the monthly sum), so the MV can answer it. The query asking for revenue per customer_id cannot be answered: the MV does not have that column.

Why aggregate algebra decides reusability

Condition 4 shows an important idea in an unexpected place. SUM and COUNT roll up because they are associative (a sum of sums is a sum). AVG does not roll up, unless the MV stored the sum and the count separately: the average of daily averages is not the monthly average if days have different row counts. COUNT(DISTINCT) does not roll up at all, unless the MV stored a mergeable sketch (such as ) rather than a number, because a customer who shopped on two days would be counted twice.

This is the identical algebraic property that makes partial aggregation work in parallel execution (each machine aggregates its share and a final step combines the results), here used for reuse rather than for parallelism. The general statement: , which is a stronger requirement than whether computing it is parallelizable.

Why matchers are conservative

The general problem, query (is every result of query A also a result of query B?), is undecidable for full SQL and NP-complete even for conjunctive queries. So the "exact test that is too expensive is replaced by a safe approximation" idea appears in its purest form. Every engine implements a restricted, conservative matcher that proves subsumption for a bounded class of patterns, . The failure is a missed rewrite, not a wrong answer, and it is silent. That is the mechanical explanation for the most common complaint about materialized views: they exist, they are fresh, they are correct, and queries that could use them do not, because the matcher's pattern class is narrower than the user's SQL.

In one sentence: expansion inflates the relation count and can push a query off the cliff; MV matching is a conservative approximation of an undecidable containment test, and its usability depends on whether the aggregate's algebra permits roll-up.