0
0
Snowflakecloud~10 mins

Views and materialized views in Snowflake - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Views and materialized views
Create View
View stores SQL query
Query View
Execute stored SQL on base tables
Return fresh data
Create Materialized View
Materialized View stores data snapshot
Query Materialized View
Return stored snapshot quickly
Background refresh updates snapshot
Views store SQL queries and run them fresh on demand; materialized views store data snapshots for faster reads and refresh in background.
Execution Sample
Snowflake
CREATE VIEW v_sales AS SELECT * FROM sales WHERE region = 'East';
CREATE MATERIALIZED VIEW mv_sales AS SELECT * FROM sales WHERE region = 'East';
SELECT * FROM v_sales;
SELECT * FROM mv_sales;
Creates a view and a materialized view filtering sales by region, then queries both to show data retrieval differences.
Process Table
StepActionObjectResultNotes
1Create viewv_salesStores SQL query: SELECT * FROM sales WHERE region = 'East'No data stored yet
2Create materialized viewmv_salesStores data snapshot matching queryData physically stored for fast access
3Query viewv_salesExecutes stored SQL on sales tableReturns fresh data each time
4Query materialized viewmv_salesReturns stored snapshot dataFaster response, may be slightly stale
5Update base table salessalesData changes in sales tableView will reflect changes next query
6Query view againv_salesExecutes SQL on updated salesReturns updated fresh data
7Query materialized view againmv_salesReturns old snapshot dataData not updated yet
8Background refreshmv_salesUpdates snapshot with latest sales dataMaterialized view now current
9Query materialized view after refreshmv_salesReturns updated snapshot dataData now matches base table
10End--Execution stops after refresh and queries
💡 Execution stops after materialized view refresh and queries show updated data
Status Tracker
VariableStartAfter Step 2After Step 5After Step 8Final
v_sales (query)Not createdStores SQL queryNo changeNo changeStores SQL query
mv_sales (data snapshot)Not createdStores initial snapshotStale snapshot after sales updateUpdated snapshot after refreshUpdated snapshot
sales (base table data)Initial dataNo changeUpdated dataNo changeUpdated data
Key Moments - 2 Insights
Why does querying the view always return fresh data but the materialized view might not?
Because the view runs the stored SQL query on the base tables every time (see execution_table rows 3 and 6), while the materialized view returns stored snapshot data that only updates during background refresh (rows 4, 7, and 8).
What happens to the materialized view data when the base table changes?
The materialized view data becomes stale until a background refresh updates it (see execution_table rows 5, 7, and 8). The view, however, always reflects the latest base table data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What data does querying the materialized view return?
AAn error because data is not stored
BFresh data from the base table
CStored snapshot data
DEmpty result set
💡 Hint
Refer to execution_table row 4 under 'Result' column
At which step does the materialized view snapshot get updated to match the base table?
AStep 5
BStep 8
CStep 6
DStep 9
💡 Hint
Check execution_table row 8 describing background refresh
If the base table 'sales' is updated, what will the view return on the next query?
AFresh updated data
BOld data from before update
CError due to data mismatch
DEmpty result set
💡 Hint
See execution_table row 6 where view query runs after sales update
Concept Snapshot
Views store SQL queries and run them fresh each time you query.
Materialized views store a snapshot of data for faster reads.
Materialized views need background refresh to update data.
Views always show current data from base tables.
Use views for always fresh data, materialized views for speed.
Full Transcript
This lesson shows how views and materialized views work in Snowflake. A view saves a SQL query and runs it fresh every time you ask for data, so it always shows the latest information. A materialized view saves a snapshot of the data matching the query, so it returns results faster but may be slightly out of date until refreshed. When the base table changes, the view reflects those changes immediately on the next query. The materialized view only updates its stored data during a background refresh process. This means querying a view always runs the SQL on the base tables, while querying a materialized view returns stored data until refreshed. Understanding this helps you choose the right option for your needs: fresh data or faster reads.