← Lessons

quiz vs the machine

Gold1350

Databases

Views vs Materialized Views

A saved query versus a saved query result, and when each wins.

4 min read · core · beat Gold to climb

Two Kinds of Saved Query

A view is a stored query that runs fresh every time you read it. It holds no data of its own, just the definition. A materialized view stores the actual rows the query produced, so reading it is as fast as reading a table.

The Core Tradeoff

  • A plain view is always up to date because it recomputes on each read, but a heavy query repeats that cost every time.
  • A materialized view is fast to read because the answer is precomputed, but the stored rows go stale until you refresh them.

Refreshing

You must rebuild a materialized view to pick up new data, either on a schedule or on demand. Some engines support a concurrent refresh so reads are not blocked during the rebuild.

Choosing

Use a plain view for simple reuse and always current data. Reach for a materialized view when an expensive aggregate is read far more often than the source data changes.

Key idea

A view recomputes its query on every read for always fresh data, while a materialized view stores results for fast reads but must be refreshed to avoid stale data.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a materialized view store that a plain view does not?

2. What is the main downside of a materialized view?