Consider a materialized view mv_sales_summary created from a sales table. After new sales data is inserted, what will be the output of the following query immediately after running REFRESH MATERIALIZED VIEW mv_sales_summary;?
SELECT COUNT(*) FROM mv_sales_summary;
CREATE MATERIALIZED VIEW mv_sales_summary AS SELECT product_id, SUM(quantity) AS total_quantity FROM sales GROUP BY product_id; -- Assume 100 rows in sales before insert INSERT INTO sales (product_id, sale_date, quantity) VALUES (101, '2024-06-01', 5); REFRESH MATERIALIZED VIEW mv_sales_summary; SELECT COUNT(*) FROM mv_sales_summary;
Think about what REFRESH MATERIALIZED VIEW does to the stored data.
Refreshing a materialized view updates its stored data to reflect the current state of the underlying tables. After inserting new sales data and refreshing, the count will include the new data.
Which statement best describes how a materialized view stores data compared to a regular view?
Think about when the data is saved and when the query runs.
Materialized views save the query results physically, so they can be queried quickly without rerunning the original query. Regular views just store the query and run it each time you select from them.
Which of the following SQL statements correctly creates a materialized view named mv_customer_orders that summarizes total orders per customer?
Remember the correct order of keywords and the need for AS.
The correct syntax requires CREATE MATERIALIZED VIEW view_name AS SELECT .... Option A follows this exactly.
Why might using a materialized view improve query performance compared to querying the base tables directly?
Think about what happens when you query a materialized view versus the original tables.
Materialized views store the results of expensive queries, so when you query them, the database does not need to recompute the data, improving performance.
Given the command REFRESH MATERIALIZED VIEW CONCURRENTLY mv_report;, which error is most likely if the materialized view does not have a unique index?
Check the requirements for using CONCURRENTLY with materialized views.
PostgreSQL requires a unique index on the materialized view to allow concurrent refreshes. Without it, the command fails with an error.