Skip to content
tezvyn:
Menu

Materialized Views: Pre-computing Slow Queries

Source: Wikipedia: Materialized viewHardHow cards are made

A materialized view trades data freshness for query speed by storing the result of a slow query as a physical table. It's ideal for dashboards that run heavy aggregations, making them load instantly. The footgun is stale data: users see old results.

Why it exists

Complex queries with joins and aggregations on large tables are slow and resource-intensive. Running the same expensive query for multiple users or frequent dashboard refreshes creates a huge, repetitive load on the database, slowing down the entire system.

The mental model

Think of a materialized view as a query's "greatest hits" album. Instead of searching through an artist's entire massive catalog (the source tables) every time you want to hear a popular song (the query result), you play it from a pre-made compilation (the materialized view). You get the result instantly, but the compilation might not have the brand-new single on it yet. It's a direct tradeoff: speed for freshness.

How it works

You define a materialized view with a standard SQL query, like CREATE MATERIALIZED VIEW daily_sales AS SELECT region, SUM(amount) FROM sales GROUP BY region. The database executes this query and stores the output as a physical table. Unlike a regular view, which is just a stored query definition, a materialized view holds actual data. This data must be refreshed periodically. Refresh strategies vary by database but typically include manual triggers, updates on a schedule, or automatic updates when the underlying source data changes.

When to use it

Use materialized views for read-heavy workloads on data that can tolerate some latency. Three common scenarios are: first, business intelligence dashboards that summarize massive amounts of data; second, caching results from remote or federated databases to avoid network latency on every query; third, simplifying access patterns for application developers who can query a simple, fast view instead of writing complex joins.

When not to use it

Avoid materialized views for systems requiring real-time data, as the view will always be slightly stale. They are also a poor fit for tables with very high write volumes, because the cost and complexity of frequently refreshing the view can outweigh the read performance benefits. For simple queries that are already fast, a materialized view just adds unnecessary complexity and storage overhead.

One canonical example

An e-commerce platform wants a dashboard showing total sales per product category. A query joining sales, products, and categories tables with billions of rows might take minutes to run. By creating a materialized view sales_by_category_daily that refreshes every hour, the dashboard can query this small, pre-aggregated table and get a response in milliseconds. The business accepts that the sales total might be up to an hour out of date.

Interview question

Which statement accurately describes the primary trade-off when using a materialized view?

  • a.It reduces storage requirements by pre-aggregating data.
  • b.It sacrifices data freshness to achieve faster query execution.Correct
  • c.It simplifies write operations by pre-calculating complex joins.
  • d.It ensures real-time data consistency across distributed systems.
Why?

The card explicitly states that a materialized view involves a "direct tradeoff: speed for freshness." It pre-computes and stores query results for faster access, but this means the data can be stale. Option A is incorrect because materialized views increase storage by storing a physical copy of the data. Option C is incorrect as they primarily improve read performance, not write performance. Option D is incorrect because materialized views introduce data staleness, which is the opposite of real-time consistency.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on databases — each one lists the topics its interview covers.

See open roles