QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Database structure

How does a view differ from a materialized view and how do you test stale data?

A view is a stored query: it keeps no data and returns the current state on every read. A PostgreSQL materialized view stores the query result and reads fast, but goes stale until REFRESH runs. QA compares it with its source query through EXCEPT and checks the refresh schedule and the acceptable delay. MySQL has no built-in materialized views.

SQL · Database structure

A test added an order of 100 and the report still shows the old total. A bug or by design?

The report "orders total per customer" reads not a table but a materialized view. A test added an order and checked the report at once: the total did not change. Compared with the table the report "lies", but the database broke nothing: a materialized view stores the query result and is refreshed only on command. This lesson is about how it differs from a plain view and how to check whether the data is stale.

How it works and why. A plain VIEW is a stored query: it keeps no data and runs its SELECT over the base tables at every read, so it always shows the current state, but costs the same each time. A MATERIALIZED VIEW in PostgreSQL runs the query once and stores the rows physically, like a table. Reads are fast and indexes can be built on it, but the data stays a "snapshot" until the next REFRESH MATERIALIZED VIEW. It is not refreshed after an INSERT into a base table or by time: a person, a job scheduler (for example pg_cron) or the application starts the refresh. This trades freshness for speed: expensive report aggregates are computed once a night or an hour, not on every page open.

Example: the view is fresh, the materialized view is stale.

CREATE TABLE ord (id serial PRIMARY KEY, customer_id int, amount int);
INSERT INTO ord (customer_id, amount) VALUES (1, 10), (1, 20), (2, 5);

CREATE VIEW v_tot AS
  SELECT customer_id, sum(amount) AS total FROM ord GROUP BY customer_id;
CREATE MATERIALIZED VIEW mv_tot AS
  SELECT customer_id, sum(amount) AS total FROM ord GROUP BY customer_id;

INSERT INTO ord (customer_id, amount) VALUES (2, 100);

SELECT * FROM v_tot  ORDER BY 1;  -- (1, 30), (2, 105)
SELECT * FROM mv_tot ORDER BY 1;  -- (1, 30), (2, 5)

After the insert the view returns 105 for customer 2, while the materialized view still returns 5. After REFRESH MATERIALIZED VIEW mv_tot both return (1, 30) and (2, 105).

After the insert: view and materialized view

v_tot (VIEW)
customer_idtotal
130
2105
mv_tot (MATERIALIZED VIEW)
customer_idtotal
130
25 stale
Figure 1. A read right after the INSERT of 100 for customer 2: the view reruns its query and gives 105, the materialized view returns the stored snapshot with 5 until REFRESH.

How QA checks for stale data.

  • Compare the materialized view with the query it is built on, in both directions: an EXCEPT without rows means the data is fresh. In the example above the result is (2, 105) and (2, 5), so there is a difference.
  • After changing data the test starts a refresh (or waits for the scheduled one) and only then checks the report. A separate test checks that the refresh job really runs on schedule and that the acceptable freshness delay is known and written down.
  • A query to pg_matviews shows whether the view is populated (ispopulated). After REFRESH ... WITH NO DATA a read fails with materialized view "mv_tot" has not been populated.

Limits and alternatives. A plain REFRESH blocks reads while it runs, so under load REFRESH MATERIALIZED VIEW CONCURRENTLY is used. It needs at least one UNIQUE index on column names only, without expressions or a WHERE, and a populated view; without such an index PostgreSQL fails with cannot refresh materialized view "mv_tot" concurrently and a hint to create a unique index with no WHERE clause. Only one refresh of a given view runs at a time. MySQL has no built-in materialized views: they are imitated with a table filled by a scheduled job, while a plain view works there as in PostgreSQL. When an always-fresh number is needed, a plain view or an indexed query is used.

Common mistakes.

  • Calling a mismatch between the report and the table a code bug without checking that the report reads a materialized view: what is needed is a refresh.
  • Testing only "after the refresh": nobody learns how long the data stays stale or whether the schedule works.
  • Running CONCURRENTLY without a unique index or on an unpopulated view and getting an error on an environment where everything passed in development.
  • Assuming a MySQL view also "caches" the result: it always runs the query anew.

Check yourself

After an INSERT into the table a SELECT from the view shows the new row, but from the materialized view it does not. Is it a bug?

Answer

No, that is how a materialized view works: it stores a snapshot and changes only after REFRESH. It would be a bug if the data did not appear even after the refresh.

REFRESH MATERIALIZED VIEW CONCURRENTLY returns an error. What do you check first?

Answer

Whether the view has a unique index without expressions or a WHERE, and whether it is populated (ispopulated in pg_matviews). Without them CONCURRENTLY does not work.

In an interview. Junior: A view is a stored query that reads current data each time. A materialized view stores the result and is faster, but goes stale until it is refreshed. Middle/Senior: I check freshness by comparing the view with its query through EXCEPT, I check the refresh schedule and the acceptable delay, and I know about CONCURRENTLY and the unique index. MySQL has no built-in materialized views.

More articles in this chapter