Cached query results, on demand
A regular VIEW is just a stored query — every SELECT against it re-runs the underlying query. A MATERIALIZED VIEW is a stored query plus its results — the data is computed once, stored on disk, and queried like a table. Refresh on schedule (or on demand) to get updated data.
When materialised views earn their keep
- Expensive aggregations queried often (dashboards, leaderboards).
- Complex joins where the underlying tables change much less than they're queried.
- Reports that don't need second-by-second freshness — minute-fresh or hour-fresh is enough.
Refreshing
REFRESH MATERIALIZED VIEW rebuilds the data; while it runs, the view is locked. REFRESH MATERIALIZED VIEW CONCURRENTLY rebuilds without locking — but requires a unique index on the view and is slower. Use CONCURRENTLY in production.