What if you could get instant answers from huge data without waiting or errors?
Why Views and materialized views in Snowflake? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a huge spreadsheet with thousands of rows and complex formulas. Every time you want to see a summary, you have to recalculate everything manually, which takes a long time and can cause mistakes.
Manually recalculating or copying data for summaries is slow and error-prone. It wastes time and can lead to inconsistent results if you forget to update everything correctly.
Views and materialized views automatically create saved queries or pre-calculated results. This means you can quickly access up-to-date summaries without recalculating everything yourself.
SELECT * FROM big_table WHERE condition; -- run full query every time
CREATE MATERIALIZED VIEW summary_view AS SELECT aggregated_data FROM big_table; -- fast access to precomputed results
It lets you get fast, reliable answers from large data sets without waiting or making mistakes.
A sales team uses a materialized view to instantly see monthly sales totals instead of waiting minutes for the full report to run every time.
Manual data summaries are slow and risky.
Views save queries for easy reuse.
Materialized views store results for fast access.
Practice
view and a materialized view in Snowflake?Solution
Step 1: Understand what a view does
A view saves a query for reuse but does not store the actual data; it runs the query each time.Step 2: Understand what a materialized view does
A materialized view stores the results of the query physically, so it can return data faster without rerunning the query.Final Answer:
A view does not store data, while a materialized view stores query results. -> Option AQuick Check:
View = no stored data, Materialized view = stored data [OK]
- Thinking views store data permanently
- Believing materialized views update instantly with every query
- Confusing reuse capability between views and materialized views
Solution
Step 1: Recall Snowflake syntax for materialized views
The correct syntax starts withCREATE MATERIALIZED VIEWfollowed by the view name and query.Step 2: Check each option's order and keywords
Only CREATE MATERIALIZED VIEW my_view AS SELECT * FROM my_table; uses the correct order and keywords:CREATE MATERIALIZED VIEW my_view AS SELECT * FROM my_table;Final Answer:
CREATE MATERIALIZED VIEW my_view AS SELECT * FROM my_table; -> Option BQuick Check:
Correct syntax = CREATE MATERIALIZED VIEW [OK]
- Mixing order of keywords
- Placing MATERIALIZED after VIEW
- Using incorrect keyword sequences
CREATE TABLE sales (id INT, amount FLOAT);
INSERT INTO sales VALUES (1, 100.0), (2, 200.0);
CREATE MATERIALIZED VIEW sales_mv AS SELECT SUM(amount) AS total FROM sales;
INSERT INTO sales VALUES (3, 300.0);
SELECT * FROM sales_mv;
What will the
SELECT query return?Solution
Step 1: Understand materialized view refresh behavior
Materialized views in Snowflake do not automatically update after data changes; they show data as of last refresh.Step 2: Analyze the timing of inserts and query
The materialized view was created after inserting two rows (100.0 + 200.0 = 300.0). The third row (300.0) was inserted after the view creation but before the select.Final Answer:
total = 300.0 -> Option DQuick Check:
Materialized view shows data at last refresh, not latest inserts [OK]
- Assuming materialized views auto-update instantly
- Adding new rows to materialized view results without refresh
- Confusing materialized views with normal views
Solution
Step 1: Identify how to update materialized views
Materialized views do not update automatically; they require manual refresh to show latest data.Step 2: Choose the correct refresh method
Snowflake supports manual refresh withALTER MATERIALIZED VIEW ... REFRESHto update the stored data.Final Answer:
Manually refresh the materialized view using ALTER MATERIALIZED VIEW ... REFRESH. -> Option CQuick Check:
Manual refresh updates materialized view data [OK]
- Dropping and recreating instead of refreshing
- Restarting warehouse has no effect on view data
- Confusing materialized views with normal views for freshness
Solution
Step 1: Understand trade-offs between views and materialized views
Normal views provide fresh data but can be slow on large data; materialized views are fast but need refresh to update.Step 2: Find a balanced solution
Scheduling frequent refreshes of materialized views keeps data reasonably fresh while improving query speed.Final Answer:
Use a materialized view and schedule frequent refreshes to keep data updated. -> Option AQuick Check:
Materialized view + refresh = speed + freshness balance [OK]
- Not refreshing materialized views and expecting fresh data
- Using only normal views and ignoring performance
- Trying to combine views in one query without refresh strategy
