Materialized views store the result of a query physically. This helps to get data faster when you need the same results often.
Materialized views concept in PostgreSQL
CREATE MATERIALIZED VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition; -- To refresh the data later: REFRESH MATERIALIZED VIEW view_name;
The materialized view stores data physically unlike a normal view which runs the query every time.
You must refresh the materialized view to update its data when the original tables change.
CREATE MATERIALIZED VIEW sales_summary AS SELECT product_id, SUM(quantity) AS total_quantity FROM sales GROUP BY product_id;
REFRESH MATERIALIZED VIEW sales_summary;SELECT * FROM sales_summary WHERE total_quantity > 100;
This example creates a sales table, inserts some data, creates a materialized view to sum quantities by product, and then selects from the view.
CREATE TABLE sales ( id SERIAL PRIMARY KEY, product_id INT, quantity INT ); INSERT INTO sales (product_id, quantity) VALUES (1, 10), (2, 5), (1, 15), (3, 7); CREATE MATERIALIZED VIEW sales_summary AS SELECT product_id, SUM(quantity) AS total_quantity FROM sales GROUP BY product_id; SELECT * FROM sales_summary ORDER BY product_id;
Materialized views need manual refresh; they do not update automatically.
Refreshing can be slow if the underlying data is large.
Use materialized views when query speed is more important than always having the latest data.
Materialized views store query results physically for faster access.
You must refresh them to update data after changes.
They are useful for speeding up repeated complex queries.
Practice
materialized view in PostgreSQL?Solution
Step 1: Understand what a materialized view does
A materialized view stores the result of a query physically on disk, unlike a normal view which only stores the query definition.Step 2: Compare options with this definition
To store the result of a query physically for faster access matches this exactly. Options A, B, and C describe other concepts like temporary tables or normal views.Final Answer:
To store the result of a query physically for faster access -> Option AQuick Check:
Materialized view = stored query result [OK]
- Confusing materialized views with normal views
- Thinking materialized views auto-update without refresh
- Assuming materialized views are temporary
sales_summary from a table sales?Solution
Step 1: Recall the correct syntax for materialized views
The correct syntax is: CREATE MATERIALIZED VIEW view_name AS SELECT ...Step 2: Match options with correct syntax
CREATE MATERIALIZED VIEW sales_summary AS SELECT * FROM sales; matches exactly. Options A, B, and D mix keywords incorrectly or use wrong object types.Final Answer:
CREATE MATERIALIZED VIEW sales_summary AS SELECT * FROM sales; -> Option CQuick Check:
CREATE MATERIALIZED VIEW = correct syntax [OK]
- Swapping keywords 'VIEW' and 'MATERIALIZED'
- Using CREATE MATERIALIZED TABLE instead of VIEW
- Adding TABLE keyword incorrectly
mv_products created as CREATE MATERIALIZED VIEW mv_products AS SELECT id, price FROM products;, what will be the result of this query immediately after inserting a new product into products but before refreshing mv_products?
SELECT * FROM mv_products WHERE id = 101;
Solution
Step 1: Understand materialized view data update behavior
Materialized views do not update automatically when base tables change. They require manual refresh.Step 2: Analyze query result after insert but before refresh
The new product with id 101 is not yet in the materialized view, so the query returns no rows for id 101.Final Answer:
No rows will be returned for id 101 -> Option DQuick Check:
Materialized view data = stale until refresh [OK]
- Assuming materialized views auto-update on insert
- Expecting syntax error from a valid SELECT
- Confusing materialized views with normal views
mv_orders created as CREATE MATERIALIZED VIEW mv_orders AS SELECT * FROM orders;. You try to refresh it using REFRESH MATERIALIZED VIEW mv_orders; but get an error: ERROR: permission denied for relation orders. What is the most likely cause?Solution
Step 1: Understand permission requirements for refreshing
Refreshing a materialized view requires SELECT permission on the underlying base tables.Step 2: Analyze the error message
The error says permission denied on relation orders, indicating the user lacks SELECT rights on that table.Final Answer:
The user running the refresh lacks SELECT permission on the base table orders -> Option AQuick Check:
Permission denied = missing SELECT rights [OK]
- Assuming syntax error instead of permission issue
- Ignoring user privileges on base tables
- Thinking materialized views cannot be refreshed
monthly_sales with the aggregation. However, the sales data updates daily. Which approach best keeps monthly_sales up to date with minimal performance impact?Solution
Step 1: Consider update frequency and performance
Since sales data updates daily, refreshing the materialized view nightly balances freshness and performance.Step 2: Evaluate other options
Dropping and recreating is costly and manual. Normal views are slow for complex queries. Auto-refresh on every query is not supported and would hurt performance.Final Answer:
Schedule a nightly REFRESH MATERIALIZED VIEW to update the data once per day -> Option BQuick Check:
Nightly refresh = best balance for daily updates [OK]
- Trying to auto-refresh materialized views on every query
- Dropping and recreating instead of refreshing
- Using normal views for heavy aggregation queries
Ready to prove it?
Pass the test and this pattern moves unknown
