Learn SQL
-- subqueries.materialized_views

Materialized Views & Updatable Views in SQL

Precompute for speed; guard writes with CHECK OPTION

--concept

Ordinary views run fresh per use — always current. Materialized views store results physically — table-fast reads, freshness costs REFRESH (on-demand, scheduled, incremental). Simple single-table views are updatable (writes pass through), optionally guarded by WITH CHECK OPTION (rejects rows that would vanish). Complex views need INSTEAD OF triggers.

--how it works
1

View is a saved query

Runs fresh per use — always current, cost paid per read.

2

Matview stores results

Physically persisted — table-fast reads, REFRESH cost for freshness.

3

Staleness decides

Dashboards/rollups over slow-changing data: yes. Balances/fresh reads: no.

4

Updatable + temp siblings

Simple views accept writes (+ CHECK OPTION guard). TEMP tables serve cross-statement heavies with indexes.

--visualization
dept_stats STORED — reads skip base scanNew hires invisible until REFRESH — staleness contract
Stage 1 / 1
Matview: table-fast reads, REFRESH cost. Dashboards yes, balances no.
--example
query.sql
1CREATE MATERIALIZED VIEW dept_stats AS SELECT department, COUNT(*) AS n FROM employees GROUP BY department;
2REFRESH MATERIALIZED VIEW dept_stats;
Result
departmentn
Engineering3
Sales2
Marketing1

The rollup is precomputed once and read like a table. New hires do not appear until REFRESH — that staleness is the price of speed. Temp tables serve the same need across statements with indexes.

--variants

Precomputed rollup

Expensive agg paid once, read many times.

query.sql
1CREATE MATERIALIZED VIEW dept_stats AS SELECT department, COUNT(*), AVG(salary) FROM employees GROUP BY department;
2REFRESH MATERIALIZED VIEW dept_stats;
Result
departmentcount
Engineering3
Sales2
Marketing1
1CREATE stores the grouped result physically.
2Reads hit the small rollup, not the base scan.
3New hires invisible until REFRESH — staleness contract.

Guarded updatable view

Writes that would vanish are rejected.

query.sql
1CREATE VIEW eng AS SELECT * FROM employees WHERE department = 'Engineering' WITH CHECK OPTION;
Result
guardrule
WITH CHECK OPTIONinserts must satisfy predicate
1Simple single-table view — writes pass through.
2Inserting a Sales row via eng fails the check.
3Complex views need INSTEAD OF triggers instead.
--key facts
View vs matviewfresh-query vs stored-result
Stale tolerancedashboards yes, balances no
CHECK OPTIONinserts must satisfy the view predicate
--use cases
Dashboard rollups, nightly marts over slow-changing facts
Stable APIs over evolving schemas (views as contracts)
--under the hood
PG REFRESH rewrites the backing table (CONCURRENTLY avoids lock with unique index); incremental where supported.
CTE MATERIALIZED forces compute-once shared scans; NOT MATERIALIZED inlines for pushdown.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.