Difficulty: Advanced
What is the difference between a view and a materialized view, and when would you choose one over the other?
A view is a saved query: it stores no data, and every time you select from it the database runs the underlying query against the live tables. A materialized view runs the query once and stores the result physically on disk like a table, so reads are fast but the data is only as fresh as the last refresh. Use a view for abstraction and always-current data; use a materialized view when the query is expensive and slightly stale results are acceptable.
CREATE VIEW active_users AS SELECT ... stores only the query text. When you run SELECT * FROM active_users WHERE city = 'Pune', the planner inlines the view definition into your query and optimizes the whole thing, including pushing the city filter down to the base table.CREATE MATERIALIZED VIEW daily_sales AS SELECT ... executes the query and saves the rows. Selects read those saved rows directly. You can add indexes on it just like a table. The data does not change until you refresh it.| Aspect | View | Materialized view |
|---|---|---|
| Stores data | No, only the query | Yes, the result set |
| Freshness | Always current | Stale until refreshed |
| Read cost | Same as running the query | Like reading a table |
| Can be indexed | No (except SQL Server indexed views) | Yes |
| Write overhead | None | Refresh cost |
| Typical use | Security, abstraction, simplifying joins | Dashboards, reports, heavy aggregations |
Refresh behaviour differs a lot between databases:
REFRESH MATERIALIZED VIEW daily_sales recomputes the full result and blocks reads while it runs. REFRESH MATERIALIZED VIEW CONCURRENTLY lets reads continue but requires a unique index on the view and is usually slower. There is no built-in incremental refresh; people schedule refreshes with cron or pg_cron, or use extensions such as pg_ivm.MATERIALIZED VIEW keyword; instead an indexed view is maintained automatically on every write, with many restrictions.SELECT id, name, city FROM users without the phone or pan_number columns and grant access only to the view.A plain view does not make anything faster. It is the same query with a shorter name.
-- Plain view: always current, no storage
CREATE VIEW customer_public AS
SELECT id, name, city FROM customers; -- hides phone, PAN
-- Materialized view: stored result of an expensive aggregation
CREATE MATERIALIZED VIEW daily_sales AS
SELECT order_date, city, SUM(amount_paise) AS revenue_paise, COUNT(*) AS orders
FROM orders
GROUP BY order_date, city;
-- Required for CONCURRENTLY refresh
CREATE UNIQUE INDEX ON daily_sales (order_date, city);
-- Refresh without blocking readers (e.g. every 15 min via pg_cron)
REFRESH MATERIALIZED VIEW CONCURRENTLY daily_sales;
The view only hides columns. The materialized view turns a large GROUP BY into a small indexed table that dashboards can read instantly, at the cost of periodic refreshes.
View, Materialized View, REFRESH MATERIALIZED VIEW, Staleness, Query Caching