Web apps & software

What is a materialized view?

A materialized view is a database object that stores the result of a query on disk. Reading it is as fast as reading a table, but its contents are only as fresh as the last time it was refreshed.

Normal view versus materialized view

An ordinary view is a saved query that runs again every time someone selects from it. If that query joins six tables and sums a year of orders, every dashboard load repeats the heavy work. A materialized view does the work once, keeps the answer, and serves it until the next refresh.

Refresh choices

PostgreSQL and Oracle can refresh on demand or on a schedule, and some engines refresh incrementally. A nightly refresh suits a finance report; a dispatcher board that needs live numbers would need a different tool, such as a cache or a table updated by the application.

Pitfalls

  • Stale numbers confuse people, so label the report with its refresh time.
  • A full refresh of a huge view can lock reads or consume the database for minutes.
  • Adding columns to the underlying tables does not change the view until it is rebuilt.

Questions, answered

When the same expensive summary query is read many times and a delay of minutes or hours is acceptable.

It is similar in purpose, but it lives inside the database and is defined in SQL.

By the Stegnor team · Updated