We use views to save queries for easy reuse. Materialized views save the query results physically, while regular views run the query every time.
Materialized view vs regular view decision in PostgreSQL
CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition; CREATE MATERIALIZED VIEW mat_view_name AS SELECT column1, column2 FROM table_name WHERE condition WITH NO DATA; -- optional
A regular view does not store data; it runs the query each time you use it.
A materialized view stores the query result physically and needs to be refreshed to update.
CREATE VIEW recent_orders AS SELECT order_id, order_date FROM orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days';
CREATE MATERIALIZED VIEW top_customers AS SELECT customer_id, SUM(amount) AS total_spent FROM sales GROUP BY customer_id ORDER BY total_spent DESC WITH NO DATA;
REFRESH MATERIALIZED VIEW top_customers;This example shows creating a regular view and a materialized view on the same data. It inserts some sales, queries both views before and after refreshing the materialized view.
CREATE TABLE sales ( sale_id SERIAL PRIMARY KEY, customer_id INT, amount NUMERIC, sale_date DATE ); INSERT INTO sales (customer_id, amount, sale_date) VALUES (1, 100, '2024-06-01'), (2, 150, '2024-06-02'), (1, 200, '2024-06-03'); CREATE VIEW sales_view AS SELECT customer_id, SUM(amount) AS total FROM sales GROUP BY customer_id; CREATE MATERIALIZED VIEW sales_mat_view AS SELECT customer_id, SUM(amount) AS total FROM sales GROUP BY customer_id WITH NO DATA; -- Query regular view SELECT * FROM sales_view ORDER BY customer_id; -- Query materialized view before refresh SELECT * FROM sales_mat_view ORDER BY customer_id; -- Refresh materialized view REFRESH MATERIALIZED VIEW sales_mat_view; -- Query materialized view after refresh SELECT * FROM sales_mat_view ORDER BY customer_id;
Materialized views use storage space because they save data.
Regular views always show current data but can be slower for complex queries.
You must manually refresh materialized views to update their data.
Regular views run the query every time you use them.
Materialized views save query results and need refreshing to update.
Choose materialized views for speed with less frequent updates, regular views for always fresh data.
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
