Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
CREATE MATERIALIZED VIEW mv_orders AS SELECT COUNT(*) AS shipped FROM orders WHERE status = 'shipped';
SELECT shipped FROM mv_orders;| shipped |
|---|
| 4 |
A new shipped order does not appear until the refresh:
sql
INSERT INTO orders VALUES (107, 3, '2026-03-20', 'shipped');
SELECT (SELECT shipped FROM mv_orders) AS before_refresh;
REFRESH MATERIALIZED VIEW mv_orders;
SELECT shipped AS after_refresh FROM mv_orders;| after_refresh |
|---|
| 5 |
The first read after the insert still shows the old total; after REFRESH the view recomputes. A plain refresh locks the view against reads while it runs. REFRESH MATERIALIZED VIEW CONCURRENTLY keeps it readable but needs a unique index on the view and takes longer.
Use materialized views for expensive aggregations read far more often than the data changes, such as dashboards refreshed every few minutes. For data that must always be current, use a regular view, a summary table maintained by triggers, or an index that makes the base query fast.
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- Materialized views store results
- Data is stale until REFRESH
- CONCURRENTLY avoids blocking readers but needs a unique index
- SQLite has no materialized views
Common mistakes
- Expecting a materialized view to update automatically
- Refreshing large views on every write
Follow-ups
- How would you schedule refreshes?
- How would you keep a summary table current with triggers instead?
Tips
- Show stale then refreshed data