0
0
PostgreSQLquery~20 mins

Materialized view vs regular view decision in PostgreSQL - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Materialized View Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to use a materialized view instead of a regular view?

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?

AUse a materialized view when you want faster query results by storing the query output physically, but you must refresh it manually or on schedule.
BUse a materialized view when you want the query to always show the latest data without any delay.
CUse a materialized view when you want to avoid storing any data and always compute results on the fly.
DUse a materialized view only if the underlying tables are updated every second.
Attempts:
2 left
💡 Hint

Think about whether the data is stored or computed each time you query.

query_result
intermediate
2:00remaining
Output difference between materialized view and regular view

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?

PostgreSQL
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';
ANeither view shows the new sale until the materialized view is refreshed.
BBoth views show the new sale total immediately after insertion.
CThe regular view shows the new sale total including the inserted row; the materialized view does not show the new sale until refreshed.
DThe materialized view shows the new sale total immediately; the regular view does not.
Attempts:
2 left
💡 Hint

Remember how materialized views store data versus regular views.

optimization
advanced
2:00remaining
Optimizing query performance with materialized views

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?

AAvoid using views and run the complex query directly every time.
BCreate a materialized view with the complex query and schedule regular refreshes during low-traffic hours.
CCreate a regular view with the complex query and index the underlying tables heavily.
DCreate a materialized view but never refresh it after creation.
Attempts:
2 left
💡 Hint

Think about balancing query speed and data freshness.

🔧 Debug
advanced
2:00remaining
Why does querying a materialized view show outdated data?

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?

AThe query on the materialized view is missing a WHERE clause.
BThe base table is locked and cannot update the materialized view automatically.
CMaterialized views always show outdated data by design.
DThe materialized view has not been refreshed since the base table was updated.
Attempts:
2 left
💡 Hint

Consider how materialized views update their data.

🧠 Conceptual
expert
2:00remaining
Choosing between materialized view and regular view for real-time analytics

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?

AUse a regular view because it always shows the latest data without needing refresh.
BUse a materialized view because it stores data and speeds up queries even if data is slightly outdated.
CUse a materialized view and refresh it every second to keep data real-time.
DUse neither; export data to a flat file for faster access.
Attempts:
2 left
💡 Hint

Think about data freshness and update frequency.