Materialized Views & Concurrent Refresh Strategies
Cache expensive query results and refresh them without long locks.
In plain words
A materialized view is a saved answer to a slow question. It's fast to read, but you have to tell the database when to work out the answer again.
Think of it like…
A printed monthly report on a manager's desk: instant to read, but it shows numbers as of the day it was printed. To update it you print a new one, ideally without taking the old copy away while the printer runs.
Where you’ve seen it
A sales dashboard reads sales_daily_mv, which sums 200 million order lines by day and region. A cron job runs REFRESH MATERIALIZED VIEW CONCURRENTLY sales_daily_mv every hour, so the dashboard is at most an hour old and loads in milliseconds.
Words you’ll hear
- Materialized view
- A stored query result that can be indexed and must be refreshed.
- REFRESH
- Recomputes the whole result; blocks reads of the view while it runs.
- CONCURRENTLY
- Refreshes without blocking readers; needs a unique index; slower.
- Staleness
- How old the stored result can be, set by the refresh schedule.
Visual diagram
Theory & example queries
Creating and refreshing
CREATE MATERIALIZED VIEW sales_daily_mv AS SELECT … runs the query once and stores the rows. You can add indexes to it like a table. Its data stays fixed until a REFRESH, which in PostgreSQL re-runs the whole query from scratch, however little changed.
A plain REFRESH locks the view against reads for the whole recompute, so a dashboard would hang. REFRESH … CONCURRENTLY builds the new result separately, compares it with the old one and applies the differences, so readers keep seeing the old data until it's done. It needs a unique index on the view and takes longer.
CREATE MATERIALIZED VIEW sales_daily_mv ASSELECT DAY, region, SUM(revenue) AS revenueFROM order_linesGROUP BY DAY, region;CREATE UNIQUE INDEX ON sales_daily_mv (DAY, region);REFRESH MATERIALIZED VIEW CONCURRENTLY sales_daily_mv;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative command responses3 sample rows
| statement | status |
|---|---|
| CREATE MATERIALIZED VIEW | CREATE MATERIALIZED VIEW |
| CREATE UNIQUE INDEX | CREATE INDEX |
| REFRESH MATERIALIZED VIEW CONCURRENTLY | REFRESH MATERIALIZED VIEW |
Note: PostgreSQL returns these command tags after a successful run. The refresh duration and materialized row count depend on the data and are not shown.
Key takeaway: Add a unique index and refresh CONCURRENTLY for views people read during the day.
Choosing it, and its limits
Use a materialized view when the query is expensive, read often, and slightly stale data is acceptable. Schedule refreshes with cron, pg_cron or your orchestrator, at a frequency that matches how fresh users need it, and show "last updated" in the dashboard.
Because PostgreSQL always recomputes everything, a view over a huge table with frequent refreshes can become a cost of its own. Then a summary table updated incrementally (only new days) is better. Other systems go further: Oracle and SQL Server (indexed views) can maintain some views automatically, and warehouses like Snowflake and BigQuery offer automatically maintained materialized views.
INSERT INTO mv_refresh_log (view_name, refreshed_at)VALUES ('sales_daily_mv', now());PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative command response
Note: This assumes one refresh-log row was inserted successfully; PostgreSQL command tags vary by client and dialect.
Store refresh times in a small table,
, after each refresh, and show it on the dashboard.
Key takeaway: Great for expensive, read-heavy, staleness-tolerant queries; switch to incremental tables when full refreshes get too costly.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Confirm the trade-off
Check that the query is slow, read frequently, and that users accept data that's minutes or hours old.
slow + popular + staleness OK
When to use it & trade-offs
What it is
A materialized view stores the result of a query as real data, so reading it is as fast as reading a table, and you can index it. The catch: it doesn't update itself. In PostgreSQL you run REFRESH MATERIALIZED VIEW name to recompute it, or REFRESH MATERIALIZED VIEW CONCURRENTLY name to recompute it without blocking readers (which requires a unique index).
Why it matters: Dashboards that aggregate millions of rows can't run the full query on every page load. A materialized view trades freshness for speed. The interview questions are about that trade-off and about refreshing without taking the dashboard down.
When to reach for it
- Dashboard and report aggregates
- Precomputed lookups that join several large tables
- Read-heavy analytics in PostgreSQL without a separate warehouse
When to avoid or be careful
- Plain REFRESH on views read during business hours
- Materialized views for data that must be real-time
- Refreshing a huge view every minute when only today's data changes
View vs materialized view vs incremental summary table
Strengths: Never stale; nothing to refresh.
Watch out: Runs the full query on every read.
Strengths: Fast indexed reads; simple to set up.
Watch out: Full recompute on each refresh; data as old as the last refresh.
Strengths: Updates only what changed; scales well.
Watch out: You write and maintain the update logic.
Interview signals & numbers to know
When this topic comes up
- “The dashboard query takes 30 seconds”
- “View vs materialized view”
- “How do you refresh it without downtime?”
- Precomputed aggregates
- Staleness requirements
Benchmarks & rules of thumb
- 1
- unique index needed for CONCURRENTLY
- 100%
- of the query re-run on each PostgreSQL refresh
- 0
- automatic refreshes in core PostgreSQL
Common mistakes & follow-ups
Common interview mistakes
- Running plain REFRESH during the day and freezing the dashboard until it finishes.
- Trying REFRESH … CONCURRENTLY without a unique index and getting an error.
- Forgetting to schedule refreshes, so the view silently shows weeks-old data.
Likely interviewer follow-ups
- How does REFRESH MATERIALIZED VIEW CONCURRENTLY avoid blocking readers?
- When would you choose a summary table over a materialized view?
- How would you tell users how fresh the data is?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Materialized Views & Concurrent Refresh Strategies with hands-on SQL practice and interview Q&A.