Save the answer of a heavy query as a table, and refresh it when needed.
A materialized view is the saved result of a query, stored like a table. Reading it is fast because the heavy work was already done. The cost is that it can get stale, so you must refresh it.
Some queries are heavy: they join big tables, group, sum and sort millions of rows. A dashboard that runs one on every page load will drown the database. But the answer changes slowly, and many people ask for the same answer. So we compute it once and keep it. That kept answer is a materialized view.
Think of it as a cache that lives inside the database, defined by a query. You get the benefits of precomputing without writing your own summary table and sync code.
| Strategy | How it works | Good and bad |
|---|---|---|
| Full refresh | Delete the stored result and run the whole query again (REFRESH MATERIALIZED VIEW). | Simple and always correct as of that moment. Costs the full query each time. In PostgreSQL, the plain form locks reads while it runs, and CONCURRENTLY avoids that but needs a unique index. |
| Incremental refresh | Apply only the rows that changed since the last refresh (a delta). | Much cheaper. Needs support from the engine, or a change log, and more complex logic. |
| On commit | The view is updated inside each write transaction. | Never stale. Every write is slower and holds locks longer. |
| On demand | A person or a deploy runs the refresh. | Simple. Easy to forget. |
| Scheduled | A job (cron, scheduler) refreshes every N minutes or each night. | Predictable cost. Staleness up to the interval. |
| Event-driven | A change stream triggers refresh or updates the stored rows. | Near real time. More moving parts. |
CREATE MATERIALIZED VIEW daily_sales AS
SELECT date_trunc('day', created_at) AS day,
product_id,
SUM(amount) AS revenue,
COUNT(*) AS orders
FROM orders
GROUP BY 1, 2;
-- a unique index lets you refresh without blocking readers
CREATE UNIQUE INDEX ON daily_sales (day, product_id);
REFRESH MATERIALIZED VIEW CONCURRENTLY daily_sales;REFRESH MATERIALIZED VIEW rebuilds everything from scratch at a set interval. Simple. The result is stale between refreshes.
A simplified model with a simulated clock and abstract work units (no random numbers). Real refresh cost depends on the engine: PostgreSQL rebuilds fully (REFRESH MATERIALIZED VIEW), while Oracle, SQL Server (indexed views), Snowflake and others can maintain some views incrementally. Check what your database supports.
| Term | Meaning in easy words |
|---|---|
| View | A saved query that runs each time you read it |
| Materialized view | A saved query whose result is stored as rows |
| Refresh | Updating the stored result from the base tables |
| Staleness | How old the stored data is compared with the real data |
| Full refresh | Rebuilding the whole result from scratch |
| Incremental refresh | Applying only the changes since the last refresh |
| Delta / change log | The list of rows that changed |
| Concurrent refresh | A refresh that does not block readers |
| Base table | The original table the view is built from |
| Query rewrite | The optimizer silently using a materialized view to answer a query (Oracle, Snowflake and others) |
| Rollup / summary table | A hand-made table of precomputed totals |
| Choice | We gain | We lose |
|---|---|---|
| Refresh often | Fresher data | More refresh work and load |
| Refresh rarely | Cheap | Older data |
| Full refresh | Simple, always correct | Cost grows with table size |
| Incremental refresh | Cost grows with change size, not table size | Complex, limited query shapes |
| Refresh on commit | Never stale | Slower writes, more locking |
| Plain view instead | Always fresh, no storage | Slow reads |