Views vs. Materialized Views

Difficulty: Advanced

Question

What is the difference between a view and a materialized view, and when would you choose one over the other?

Answer

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.

How each one works

  • View: 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.
  • Materialized view: 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.

Comparison

AspectViewMaterialized view
Stores dataNo, only the queryYes, the result set
FreshnessAlways currentStale until refreshed
Read costSame as running the queryLike reading a table
Can be indexedNo (except SQL Server indexed views)Yes
Write overheadNoneRefresh cost
Typical useSecurity, abstraction, simplifying joinsDashboards, reports, heavy aggregations

Refreshing materialized views

Refresh behaviour differs a lot between databases:

  • PostgreSQL: 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.
  • Oracle: supports fast (incremental) refresh using materialized view logs, and refresh on commit.
  • SQL Server: has no MATERIALIZED VIEW keyword; instead an indexed view is maintained automatically on every write, with many restrictions.
  • MySQL: has no native materialized views. Teams emulate them with summary tables updated by jobs or triggers.
  • Warehouses like BigQuery and Snowflake maintain materialized views automatically in the background.

Why plain views still matter

  • Security: expose SELECT id, name, city FROM users without the phone or pan_number columns and grant access only to the view.
  • Abstraction: keep old column names working after a refactor.
  • Simplicity: hide a five-table join behind one name for reporting users.

A plain view does not make anything faster. It is the same query with a shorter name.

Gotchas

  • Querying a view of a view of a view can produce very large plans that are hard to tune.
  • A full refresh on a large materialized view can take minutes and cause I/O spikes, so schedule it off-peak.
  • Applications reading a materialized view must tolerate staleness. A "today's revenue" tile refreshed every 15 minutes is fine; a wallet balance is not.
Materialized view lifecycle. The expensive query runs at refresh time, not at read time.
  1. Base tables change: Orders keep arriving all day.
  2. Scheduled refresh runs: The defining query executes once.
  3. Result stored on disk: Rows saved, indexes rebuilt.
  4. Dashboard reads: Fast table-like scan of stored rows.

Code examples

View vs materialized view in PostgreSQL

-- 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.

Key points

  • A view stores only the query; a materialized view stores the result
  • Plain views do not improve performance; they help with security and abstraction
  • PostgreSQL refreshes are full recomputations; CONCURRENTLY needs a unique index
  • MySQL has no native materialized views; SQL Server uses indexed views instead
  • Materialized views suit dashboards and reports where some staleness is acceptable

Common follow-up questions

  • How would you refresh a materialized view without blocking reads?
  • How would you emulate a materialized view in MySQL?
  • What is an updatable view?
  • When would you use a summary table maintained by triggers instead of a materialized view?

Interview tips

  • Lead with the one-line difference: query vs stored result, then talk about freshness
  • Mention vendor differences; it shows you have worked with more than one database
  • Give a staleness-tolerant use case (analytics) and an intolerant one (balances)

Concepts covered

View, Materialized View, REFRESH MATERIALIZED VIEW, Staleness, Query Caching

More Database Design questions