Materialized views: store the answer, refresh it
Vinnie on rung six of the scaling ladder: a materialized view stores an expensive query's result so reads are cheap, and a refresh brings it up to date. PostgreSQL's REFRESH MATERIALIZED VIEW and why CONCURRENTLY needs a unique index, SQL Server's indexed views, MySQL's summary tables, MongoDB's on-demand views, and staleness as the price.
Diagrams in this video · 1Free in archhy
The diagrams, full size
Copy any of them into archhy, free, and change it for your own system.
After this lesson you can
- Explain how a materialized view differs from a view
- Create and refresh a materialized view in PostgreSQL
- Say how SQL Server, MySQL and MongoDB do the same job
- Weigh staleness against speed
Transcript
Materialized views: store the answer, refresh it
Vinnie here. Rung six. Materialized views. Some queries are expensive every single time. Joins, sums, millions of rows. And a dashboard asks the same one all day long. So why work it out every time? Work it out once, and keep the answer.
A view vs a materialized view
First, a plain view. It stores only the query. Every time you read it, the database runs that query again. Always current. Never any faster. A materialized view stores the result. Reads come straight from the stored rows, like a table, and you can index them. The catch hides in one word. Refresh.
How a materialized view works
Here's the flow. One. Your base tables, like orders, change all day. Two. The expensive query joins them, and adds them up. Three. A materialized view runs that query once, and stores the result. Four. Dashboards read the stored result. No joins and no sums at read time. Fast. Five. To bring it up to date, you refresh it, which reruns the query. In PostgreSQL, you run the refresh yourself, from a scheduled job, or when you need it. Six. Between refreshes, the answer is as old as the last refresh. That's the trade. Staleness, for speed.
A materialized view in PostgreSQL
Here it is in PostgreSQL. Create materialized view, daily sales. Total sales per seller, per day, leaving out today, which isn't finished yet. Then a unique index on seller and date. Fast lookups, and something else you'll need in a second. Refresh materialized view. A plain refresh can block anyone reading the view while it runs. Add concurrently, and readers carry on. But concurrently needs a unique index on plain columns, covering every row. That's why we made one. Only one refresh runs at a time. And when lots of rows change, a plain refresh is often quicker. Concurrently shines when only a few do.
How the big databases do it
Other databases do it differently. SQL Server has indexed views. Create the view with schema binding, add a unique clustered index, and the result is stored. SQL Server updates it on every insert, update and delete. Never stale, but every write pays a little more. Mind the rules, though. Many query shapes aren't allowed in the view, like outer joins. And on Standard edition, you query the view with the no expand hint. MySQL has no materialized views built in. Teams keep a summary table instead, and refresh it from a scheduled event, or from the app. MongoDB has on-demand materialized views. An aggregation writes its result to a collection, with merge or out, and you run it again to refresh. And a managed PostgreSQL, on any cloud, works exactly like this. It's the same engine.
When not to use one
When not to use one? When readers need the data up to the second. An account balance isn't a dashboard. When the query is already cheap. Try an index first. That was rung one. And when a refresh takes longer than the gap between refreshes. Then you're always refreshing, and always behind. My nose can smell that one from orbit.
Remember
Let's recap. A materialized view stores a query's result, so reads are cheap. You refresh it, and between refreshes, it's stale. In PostgreSQL, concurrently keeps readers going, and needs a unique index. SQL Server's indexed views stay current, and the writes pay for it.
The full article, and what's next
Next time, rung seven. Sharding and partitioning. For when one table, or one server, isn't enough. See you there.





