You have a large table with millions of rows that rarely changes. You want to speed up complex queries that aggregate data from this table. Which option best explains when to use a materialized view instead of a regular view?
Think about whether the data is stored or computed each time you query.
A materialized view stores the query result physically, which speeds up queries but requires manual or scheduled refresh to update data. A regular view computes data on demand and always shows the latest data but can be slower.
Given a table sales with columns id, amount, and sale_date, you create a regular view and a materialized view that calculate total sales per day. If a new sale is inserted after creating both views, what will be the output of querying each view?
CREATE VIEW daily_sales_view AS SELECT sale_date, SUM(amount) AS total FROM sales GROUP BY sale_date; CREATE MATERIALIZED VIEW daily_sales_matview AS SELECT sale_date, SUM(amount) AS total FROM sales GROUP BY sale_date; -- After inserting a new sale: INSERT INTO sales VALUES (101, 500, '2024-06-15'); -- Query both views: SELECT * FROM daily_sales_view WHERE sale_date = '2024-06-15'; SELECT * FROM daily_sales_matview WHERE sale_date = '2024-06-15';
Remember how materialized views store data versus regular views.
Regular views compute results on demand, so they reflect the latest data immediately. Materialized views store results physically and require manual refresh to update.
You have a complex query joining multiple large tables and aggregating data. The query runs slowly when executed frequently. Which approach will best improve performance using materialized views?
Think about balancing query speed and data freshness.
Materialized views store query results physically, speeding up queries. Scheduling refreshes during low-traffic times keeps data reasonably fresh without slowing down users.
You created a materialized view from a table and query it multiple times. You notice the data does not reflect recent changes in the base table. What is the most likely cause?
Consider how materialized views update their data.
Materialized views do not update automatically when base tables change. They require manual or scheduled refresh to show current data.
You are designing a dashboard that shows real-time analytics updated every second. The underlying data changes frequently. Which is the best choice and why?
Think about data freshness and update frequency.
Regular views compute data on demand, so they always reflect the latest changes, which is essential for real-time analytics. Materialized views require refresh and may lag behind.