PrepCity

Materialized views

Open the lab

By the last page, this is the design you will have built. Drag it, or Show all, to see every part.

Lesson · 8 pages · about 7 min

Materialized views

A dashboard query that adds up every order on each load is slow. A materialized view stores the answer, so reads are fast, but it goes stale with every new order until it is refreshed. See full versus incremental refresh, and why PostgreSQL’s CONCURRENTLY keeps readers from waiting.

What it is
A materialized view is a saved query result: the database runs the query once and stores the rows, like a table you can read quickly.
What it is for
Dashboards ask the same heavy question again and again. Storing the answer turns a scan of every order into a read of a few rows.
Use it when
When many reads ask the same expensive question and the answer may be a little out of date, by minutes or hours.
Not when
When every read must see the very latest write, or the base data is small enough to query directly.

Words you’ll learn

  • Dashboard
  • Database
  • Row
  • Materialized view
  • Commit
  • PostgreSQL
  • Snapshot
  • Cron
  • Orchestrator
  • Stale read

Page by page