Materialized view vs regular view decision in PostgreSQL - Performance Comparison
When choosing between a materialized view and a regular view, it's important to understand how their execution time changes as data grows.
We want to know how query speed behaves with each option as the database gets bigger.
Analyze the time complexity of querying a regular view versus a materialized view.
-- Regular view definition
CREATE VIEW sales_summary AS
SELECT product_id, SUM(quantity) AS total_quantity
FROM sales
GROUP BY product_id;
-- Materialized view definition
CREATE MATERIALIZED VIEW sales_summary_mat AS
SELECT product_id, SUM(quantity) AS total_quantity
FROM sales
GROUP BY product_id;
-- Querying the views
SELECT * FROM sales_summary;
SELECT * FROM sales_summary_mat;
The regular view runs the aggregation each time it is queried, while the materialized view stores the result and returns it directly.
Look at what happens when you query each view.
- Primary operation: Scanning and grouping the entire sales table.
- How many times: For the regular view, this happens every query; for the materialized view, it happens only when refreshed.
As the sales table grows, the time to compute the aggregation grows too.
| Input Size (n rows) | Regular View Query Time | Materialized View Query Time |
|---|---|---|
| 10 | Small, quick scan | Almost instant (precomputed) |
| 1000 | Longer scan and group | Still almost instant |
| 1000000 | Much longer scan and group | Still almost instant |
Pattern observation: Regular view query time grows roughly with data size; materialized view query time stays nearly constant.
Time Complexity: O(n) for regular view queries, O(1) for materialized view queries
This means regular views take longer as data grows, while materialized views return results quickly because they store precomputed data.
[X] Wrong: "Materialized views always speed up queries without any extra cost."
[OK] Correct: Materialized views need to be refreshed to update data, which can take time and resources, so they are not always the best choice for frequently changing data.
Understanding when to use materialized views versus regular views shows you can balance query speed and data freshness, a key skill in real database work.
"What if the materialized view is refreshed automatically after every insert? How would that affect the time complexity of queries and updates?"
Practice
Solution
Step 1: Understand what a regular view does
A regular view is a saved SQL query that runs fresh every time you use it.Step 2: Compare with other options
Storing data physically and requiring manual refresh describes a materialized view; saying it cannot be used in SELECT statements is false because views can be queried directly; automatically updating data without running queries is incorrect for regular views.Final Answer:
It runs the underlying query every time it is accessed. -> Option CQuick Check:
Regular view runs query each time [OK]
- Confusing materialized views with regular views
- Thinking views store data physically
- Believing views auto-refresh data
Solution
Step 1: Identify syntax for materialized view
The correct syntax uses CREATE MATERIALIZED VIEW followed by the view name and query.Step 2: Eliminate incorrect options
CREATE VIEW my_view AS SELECT * FROM sales; creates a regular view, CREATE TABLE my_mat_view AS SELECT * FROM sales; creates a table, CREATE TEMP VIEW my_mat_view AS SELECT * FROM sales; creates a temporary view, not materialized.Final Answer:
CREATE MATERIALIZED VIEW my_mat_view AS SELECT * FROM sales; -> Option AQuick Check:
Materialized view syntax includes 'MATERIALIZED' keyword [OK]
- Using CREATE VIEW instead of CREATE MATERIALIZED VIEW
- Confusing tables with materialized views
- Using TEMP keyword incorrectly
CREATE MATERIALIZED VIEW sales_summary AS SELECT region, SUM(amount) AS total FROM sales GROUP BY region;What will be the result of:
SELECT * FROM sales_summary;immediately after creation?
Solution
Step 1: Understand materialized view creation behavior
When created, a materialized view stores the query result immediately.Step 2: Determine output of SELECT from materialized view
It returns the aggregated totals per region as per the query at creation time.Final Answer:
The aggregated total sales per region based on current data. -> Option AQuick Check:
Materialized view stores query result on creation [OK]
- Thinking materialized views are empty until refreshed
- Expecting detailed rows instead of aggregated results
- Assuming errors occur without manual refresh
Solution
Step 1: Identify how to update materialized view data
Materialized views do not auto-update; REFRESH MATERIALIZED VIEW command updates stored data.Step 2: Evaluate other options
Dropping and recreating the materialized view every time is inefficient, using SELECT * FROM materialized_view does not refresh it, restarting the PostgreSQL server is unrelated.Final Answer:
Run REFRESH MATERIALIZED VIEW to update the data. -> Option DQuick Check:
Use REFRESH MATERIALIZED VIEW to update data [OK]
- Expecting SELECT to refresh data automatically
- Dropping and recreating instead of refreshing
- Restarting server to fix view data
Solution
Step 1: Analyze requirements for speed and data freshness
Reports need fast access and can accept data up to 1 hour old.Step 2: Match options to requirements
Materialized views store results and can be refreshed hourly, balancing speed and freshness. Regular views run slow on large data. Triggers add complexity. Temporary views do not persist.Final Answer:
Use a materialized view refreshed every hour for faster queries. -> Option BQuick Check:
Materialized view + hourly refresh = best for speed and acceptable freshness [OK]
- Choosing regular views despite slow queries
- Using triggers unnecessarily complex
- Confusing temporary views with materialized views
Ready to prove it?
Pass the test and this pattern moves unknown
