0
0
PostgreSQLquery~10 mins

Materialized views concept in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Materialized views concept
Create Materialized View
Store Query Result
Query Materialized View
Use Stored Data
Refresh Materialized View?
YesRecompute and Store
No
End
Materialized views store query results physically. You query the stored data until you refresh it to update.
Execution Sample
PostgreSQL
CREATE MATERIALIZED VIEW sales_summary AS
SELECT region, SUM(amount) AS total_sales
FROM sales
GROUP BY region;

SELECT * FROM sales_summary;
Creates a materialized view summarizing sales by region, then queries the stored summary.
Execution Table
StepActionQuery/CommandResult/State
1Create materialized viewCREATE MATERIALIZED VIEW sales_summary AS SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region;Materialized view 'sales_summary' created and data stored
2Query materialized viewSELECT * FROM sales_summary;Returns stored summary data quickly without re-running original query
3Insert new sales dataINSERT INTO sales VALUES ('East', 100);New data added to 'sales' table, materialized view not updated
4Query materialized view againSELECT * FROM sales_summary;Returns old summary data, does not include new insert
5Refresh materialized viewREFRESH MATERIALIZED VIEW sales_summary;Materialized view data updated to include new sales
6Query refreshed materialized viewSELECT * FROM sales_summary;Returns updated summary including new sales data
💡 Execution stops after querying refreshed materialized view with up-to-date data
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
sales table[empty or initial data]Initial data presentNew row ('East', 100) addedNo changeNo change
sales_summary view dataNoneStored summary of initial dataNo change (stale)Updated summary including new rowUpdated summary
Key Moments - 2 Insights
Why does the materialized view not show new data after inserting into the base table?
Because materialized views store a snapshot of data at creation or last refresh. The insert changes the base table but the view stays stale until refreshed (see execution_table rows 3 and 4).
What happens when you refresh a materialized view?
Refreshing recomputes the query and updates the stored data, so the view reflects the latest base table data (see execution_table row 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the query in step 4 return?
AThe updated summary including new sales data
BThe old summary data without new sales data
CAn error because data changed
DEmpty result set
💡 Hint
Check execution_table row 4 where the materialized view is queried before refresh
At which step is the materialized view data updated to include new sales?
AStep 5
BStep 2
CStep 3
DStep 6
💡 Hint
Look at execution_table row 5 where REFRESH MATERIALIZED VIEW is executed
If you never refresh the materialized view, what happens when you query it after new data inserts?
AIt shows the new data automatically
BIt shows an error
CIt shows the old data without new inserts
DIt deletes the new data
💡 Hint
Refer to variable_tracker showing 'sales_summary view data' after step 3 and 4
Concept Snapshot
Materialized views store query results physically.
Querying them is fast but shows stored data only.
Base tables can change without updating the view.
Use REFRESH MATERIALIZED VIEW to update stored data.
Useful for expensive queries needing fast repeated access.
Full Transcript
Materialized views in PostgreSQL store the results of a query physically. When you create a materialized view, the query runs once and the results are saved. Later, when you query the materialized view, it returns the stored data quickly without running the original query again. If the base tables change, the materialized view does not update automatically. You must run REFRESH MATERIALIZED VIEW to update it with the latest data. This makes materialized views useful for speeding up expensive queries that do not need real-time data. The execution steps show creating the view, querying it, inserting new data into the base table, querying the stale view, refreshing it, and querying the updated view.