Hello! Welcome back to our course on the internals of database systems.
In our last lesson, we took a deep dive into the low-level world of data structures, exploring how classic B+ trees are re-engineered into cache-conscious B+ trees to maximize performance in main-memory databases. We saw how aligning data structures with CPU architecture is a powerful optimization strategy.
Today, we move up the optimization stack from data structures to the query optimizer itself. We'll explore a technique that can dramatically speed up complex queries by doing the work ahead of time. This lesson focuses on the learning outcome: Explain the purpose of materialized views as a query optimization technique.
We'll investigate what materialized views are, how they differ from regular views, and—most importantly—how a modern query optimizer can use them automatically to supercharge query performance without any changes to your application code.
The Core Idea: Pre-computation as an Optimization
Imagine you have a complex query for an analytics dashboard. It joins several large tables, calculates aggregates like SUMs and AVGs, and groups the results. Running this query might take several seconds or even minutes. If many users are accessing this dashboard, the database will be stuck re-doing the same expensive work over and over.
The core idea behind materialized views is simple: if a query is expensive and its results are needed frequently, why not compute the result once, save it, and then simply read the saved result for subsequent requests? This is the essence of pre-computation.
Regular Views vs. Materialized Views
To understand this, we first need to distinguish between a regular VIEW and a MATERIALIZED VIEW. You may have encountered regular views in your previous database course.
- A regular view is just a stored SQL query, acting as a named alias. When you query a view, the database executes the underlying query against the base tables at that moment. It's a convenience for simplifying complex logic, but it doesn't inherently improve performance.
- A materialized view, on the other hand, physically stores the results of the query on disk, just like a regular table. When you query a materialized view, the database reads the pre-computed, stored data, bypassing the expensive computation on the base tables.
This short video provides an excellent visual explanation of this fundamental difference.
Count Millions of Rows Fast with Materialized Views: Databases for Developers: Performance #6
The video 'Count Millions of Rows Fast with Materialized Views' from The Magic of SQL channel clearly illustrates the problem materialized views solve and how they differ from regular views.
Please watch from the beginning to 2:40. Focus on the distinction between a view storing the SQL statement versus a materialized view storing the query's result.
The Fundamental Trade-Offs
As with any engineering decision, materialized views involve trade-offs. By pre-computing and storing results, you gain a significant performance boost for read queries, but at a cost.
Database Views and Materialized Views: The Smart Way to ...
The blog post 'Database Views and Materialized Views' by Artem Khrenov provides a clear breakdown of these trade-offs.
Read the section titled 'The Key Differences That Matter'. It effectively summarizes the trade-offs in terms of storage, performance, and data freshness.
To summarize the key points from the article:
- Performance: Queries against materialized views are much faster, as the expensive joins and aggregations are already done.
- Data Freshness: The data in a materialized view is only as current as its last refresh. This means the data can be stale. A regular view always returns live data.
- Storage: Materialized views consume disk space to store their results, whereas regular views consume almost none.
- Maintenance: Materialized views require a refresh strategy to keep them updated, adding operational complexity.
This diagram illustrates the general architecture. Raw data is processed via pre-computation into a materialized view, which can then be queried directly.

The Real Magic: Automatic Query Rewriting
The true power of materialized views is unlocked when the database optimizer can use them transparently. This means you can run a query against your base tables, and the optimizer is smart enough to realize, "Hey, I can answer this query (or part of it) using this existing materialized view, which will be much faster!" This process is called query rewriting.
This automatic substitution is what makes materialized views a powerful optimization tool, as you can add or remove them just like indexes, without changing application code.
Let's see a quick overview of how this works.
Count Millions of Rows Fast with Materialized Views: Databases for Developers: Performance #6
The same video we watched earlier also gives a great high-level summary of query rewriting.
Please watch from 2:40 to 3:27. Notice how the database can use the materialized view to answer a query that looks different from the view's definition.
How Query Rewriting Works Internally
So, how does the optimizer decide if a materialized view can be used? This process is integrated into the optimizer's rule-based and cost-based optimization phases, which we touched upon in Module 4.
When a query comes in, the optimizer looks at the available materialized views and checks if a match can be found. This isn't just a simple text match of the SQL. The optimizer uses relational algebra and a series of sophisticated checks to determine if the query's result is computable from the view's result.
This technical blog post from Alibaba Cloud provides an excellent, detailed explanation of the internal mechanics.
Detailed Explanation of Query Rewriting Based on ...
The post 'Detailed Explanation of Query Rewriting Based on Materialized Views' dives into the specific checks an optimizer performs.
Please read from the section 'Background of Materialized Views' through to the end of 'Grouping and Aggregation Checks'. Focus on understanding the purpose of each check, not necessarily memorizing every detail of the implementation. Key sections to focus on: Background of Materialized Views: Reinforces the core concept. Query Rewriting Based on Materialized Views: Explains why automatic rewriting is so important. Query Rewriting Checks: Introduces the main steps. JOIN Check: Can the view's joins satisfy the query's joins? Output Check: Does the view contain the necessary columns and rows? Grouping and Aggregation Checks: How to handle queries with GROUP BY and functions like SUM() or COUNT().
Let's distill the main ideas from that article:
For a query to be rewritten using a materialized view, the optimizer must confirm several things:
- Table Equivalence: The query must use a subset of the tables present in the materialized view.
- Join Compatibility: The join relationships in the query must be derivable from the joins in the view.
- Predicate Containment: The rows selected by the view must be a superset of the rows needed by the query. For example, if the query has
WHERE state = 'CA', it can be answered by a view withWHERE country = 'USA', but not by a view withWHERE state = 'NY'. The optimizer adds the missingstate = 'CA'predicate to the new plan that reads from the view. This is called a "compensation predicate". - Output Computability: All columns and expressions in the query's
SELECTlist must be computable from the columns available in the materialized view. For example, a query asking forAVG(price)can be answered by a view that containsSUM(price)andCOUNT(price).
This process is a core part of modern query optimizers. It's often implemented as a set of transformation rules, similar to the predicate pushdown or join reordering rules we discussed earlier in the course.
To see how this fits into a real-world system, this clip from a talk on the SQL Server query optimizer is very insightful.
The Cascades Framework for Query Optimization at Microsoft (Nico Bruno + Cesar Galindo-Legaria)
In this segment from a CMU talk, engineers from Microsoft discuss how materialized views are handled within the Cascades optimization framework used by SQL Server.
Watch from 23:13 to 26:15. Pay attention to how they explicitly mention that 'materialized views are also done through transformation rules' and how the optimizer loads metadata about available views to use during planning.
Common Design Patterns for Materialized Views
Now that we understand the mechanism, let's look at when to apply it. Materialized views are particularly effective for a few common patterns.
Database Views and Materialized Views: The Smart Way to ...
The blog post by Artem Khrenov also provides excellent, code-driven examples of practical design patterns for materialized views.
Please read the sections 'Design Patterns for Materialized Views', 'Scenario 3: Complex Reporting Dashboard', and 'Making the Choice Between View Types'. These provide concrete SQL examples for common use cases and a practical framework for deciding when to use a materialized view.
The most common and powerful use cases are:
- Aggregation Caching for Dashboards: This is the classic example. Pre-calculating daily/monthly sales, user activity metrics, or other KPIs for a reporting dashboard. The queries go from minutes to milliseconds. The
daily_business_metricsexample in the article is a perfect illustration. - Pre-joining Complex Data: If your application frequently joins a common set of tables (e.g.,
products,categories,brands,reviews), you can create a materialized view that pre-joins them. This simplifies application logic and boosts performance. The "search index view" pattern is an advanced version of this. - Partial Results (Union Rewriting): In some cases, a materialized view might only contain a portion of the data needed, for example, historical data that rarely changes. The optimizer can be smart enough to fetch the historical part from the materialized view and query the base tables for the recent data, then combine the results with a
UNION.

Test your understanding!
You are building an e-commerce site. You have a query that calculates the total revenue per product category for the last 30 days. This query is used on the main sales dashboard and is run hundreds of times per hour. The underlying orders table has billions of rows. Would a materialized view be a good fit here? Why? What is the main trade-off you'd have to explain to the business team?
Show answer
Yes, a materialized view would be an excellent fit. The query is computationally expensive (aggregating over a huge table) and is run frequently. By creating a materialized view that pre-calculates the revenue per category, you can reduce the query time from potentially minutes to sub-second.
The main trade-off to explain is data freshness. You would need to tell the business team that the dashboard will not be real-time to the second. For example, if you refresh the view every hour, the revenue figures they see could be up to an hour out of date. For most analytics dashboards, this level of latency is perfectly acceptable, but it's a crucial part of the design decision.
Conclusion
We've seen today that materialized views are a powerful query optimization technique that leverages pre-computation to avoid expensive, repetitive work. They represent a classic engineering trade-off, sacrificing data freshness and storage space for dramatic gains in query performance.
Key Takeaways:
- Purpose: Materialized views are physical objects that store the pre-computed results of a query to accelerate performance.
- Core Mechanism: The query optimizer uses query rewriting to transparently substitute parts of an incoming query with a materialized view, even if the query doesn't reference it directly.
- Trade-Offs: The primary benefit is speed for read-heavy workloads. The costs are increased storage usage and, most importantly, data staleness, as the view's data is only as current as its last refresh.
- Use Cases: They are ideal for analytics, reporting dashboards, and any scenario involving frequent, expensive queries (with heavy joins or aggregations) where some data latency is acceptable.
Preview of the Next Lesson
We've established the "what" and "why" of materialized views and highlighted their main trade-off: data freshness. This naturally leads to our next topic. To make materialized views truly useful in production, we need a robust strategy for keeping them up-to-date.
In the next lesson, we will address the learning outcome: Describe strategies for materialized view maintenance (e.g., immediate vs. deferred refresh). We'll explore the different ways to refresh these views and the performance implications of each approach.