Snowflake
Architecture
- Snowflake Architecture
- Multi-Cluster Architecture
- Shared Disk and MPP Processing
- Virtual Warehouses
- Zero-Copy Cloning
Data Storage & Management
Data Loading & Integration
QueryProcessing & Optimization
- Query Caching
- Query Profile & Optimization
- Materialized Views
- Adaptive Query Execution
- Query Acceleration Service
SQL & Analytics Features
❄️ Snowflake Materialized Views: Precompute and Store Query Results for Fast Retrieval
As data grows exponentially, analytics queries can become slower and more expensive. Snowflake offers a powerful solution: Materialized Views (MVs) — precomputed and stored query results that automatically refresh as underlying data changes.
Think of a materialized view as a snapshot of a complex SQL query that is kept up-to-date. Instead of recalculating results every time a user queries the data, Snowflake reads directly from the stored results, making queries blazing fast.
🧩 What Are Snowflake Materialized Views?
A Materialized View in Snowflake is a database object that stores the physical results of a query for quick retrieval. Unlike regular views, which recompute data every time they’re queried, materialized views cache the query output in storage.
Whenever the base table changes, Snowflake automatically refreshes the view to keep it synchronized.
🔍 Regular View vs Materialized View
Feature | Regular View | Materialized View |
---|---|---|
Storage | No physical storage | Physically stores query results |
Query Speed | Slower (recomputed each time) | Faster (precomputed results) |
Maintenance | No refresh needed | Automatically refreshed |
Ideal Use | Frequently changing logic | Stable data with repeated queries |
⚙️ How Materialized Views Work
Here’s the internal flow:
- Create Materialized View → Define SQL logic.
- Snowflake Executes Query Once → Stores result.
- Automatic Refresh → Keeps view synced with base table.
- Query Execution → Uses precomputed data for faster performance.
🧭 ** – How Snowflake Materialized Views Work**
🧠 Why Use Materialized Views?
Materialized views are used to:
- Speed up repeated or complex queries
- Reduce compute cost by avoiding reprocessing
- Simplify BI dashboards and ETL pipelines
- Optimize aggregations, joins, and filters
🧠 Example 1: Basic Materialized View
Let’s start simple — precomputing sales totals by region.
-- Step 1: Create base tableCREATE OR REPLACE TABLE sales ( region STRING, amount NUMBER, sale_date DATE);
-- Step 2: Create Materialized ViewCREATE OR REPLACE MATERIALIZED VIEW mv_sales_summary ASSELECT region, SUM(amount) AS total_salesFROM salesGROUP BY region;
-- Step 3: Query the materialized viewSELECT * FROM mv_sales_summary;
🧾 Explanation
- The first query creates a base table (
sales
). - The materialized view (
mv_sales_summary
) stores aggregated totals. - When queried, results are fetched instantly from the precomputed data.
💡 Optimization Tip:
Materialized views work best when base data doesn’t change frequently — e.g., hourly or daily batch updates.
🧠 Example 2: Materialized View with Filters
This example demonstrates filtering active customers.
-- Step 1: Base customer tableCREATE OR REPLACE TABLE customers ( customer_id NUMBER, name STRING, status STRING, region STRING);
-- Step 2: Create filtered materialized viewCREATE OR REPLACE MATERIALIZED VIEW mv_active_customers ASSELECT customer_id, name, regionFROM customersWHERE status = 'ACTIVE';
-- Step 3: Query materialized viewSELECT * FROM mv_active_customers;
🧾 Explanation
- The view precomputes and stores only active customers.
- Any update to
status
in the base table automatically triggers a refresh. - When queried, it avoids full table scans.
💡 Optimization Tip:
Combine filters with clustering keys for even faster retrieval.
ALTER MATERIALIZED VIEW mv_active_customers CLUSTER BY (region);
🧠 Example 3: Materialized View with Joins
Here’s an advanced example combining sales and product details.
-- Step 1: Create base tablesCREATE OR REPLACE TABLE sales ( sale_id NUMBER, product_id NUMBER, amount NUMBER);
CREATE OR REPLACE TABLE products ( product_id NUMBER, category STRING);
-- Step 2: Create materialized view for pre-joined dataCREATE OR REPLACE MATERIALIZED VIEW mv_sales_by_category ASSELECT p.category, SUM(s.amount) AS total_salesFROM sales sJOIN products p ON s.product_id = p.product_idGROUP BY p.category;
-- Step 3: Query the precomputed dataSELECT * FROM mv_sales_by_category;
🧾 Explanation
- Joins between
sales
andproducts
are precomputed and stored. - Future queries on sales by category become instant.
- Snowflake automatically refreshes when either base table changes.
📊 Benefits of Snowflake Materialized Views
Benefit | Description |
---|---|
Speed | Faster query results by reading precomputed data |
Efficiency | Reduce compute by skipping reprocessing |
Automation | Automatically updates when base data changes |
Integration | Works well with BI tools like Tableau, Power BI |
Scalability | Handles large datasets efficiently |
🧠 How Snowflake Refreshes Materialized Views
-
Automatic Refresh: Happens when underlying data in base tables changes.
-
Manual Refresh: Can be triggered using:
ALTER MATERIALIZED VIEW mv_sales_summary REFRESH; -
Deferred Refresh: You can also suspend refreshes during heavy ETL loads:
ALTER MATERIALIZED VIEW mv_sales_summary SUSPEND REBUILD;
🧭 ** – Refresh Cycle of Materialized Views**
⚙️ Snowflake Materialized View Storage & Cost
Snowflake stores materialized views in micro-partitioned storage (same as tables).
You incur:
- Storage Cost: For persisted results.
- Compute Cost: For refresh operations.
However, overall cost is much lower than repeatedly running heavy queries.
🧠 How to Check Materialized View Usage
You can monitor materialized view performance using:
SELECT database_name, schema_name, name, bytes, refresh_type, last_refresh_timeFROM SNOWFLAKE.ACCOUNT_USAGE.MATERIALIZED_VIEWS;
💡 Best Practices for Materialized Views
Practice | Description |
---|---|
Use on Stable Data | Ideal for slowly changing tables |
Avoid Too Many MVs | Each adds maintenance overhead |
Use Selective Filters | Reduces refresh and scan cost |
Monitor Refresh Frequency | Avoid frequent rebuilds |
Combine with Clustering Keys | Improves data access speed |
🧠 Interview Preparation Tips
Remember this mnemonic:
“P.A.R.T.” → Precompute, Automate, Refresh, Tune
Letter | Meaning |
---|---|
P | Precompute heavy queries |
A | Automate refresh process |
R | Refresh data efficiently |
T | Tune for cost & speed |
🧠 Interview Questions & Answers
Question | Answer |
---|---|
What is a Materialized View? | A precomputed, stored query result. |
How is it different from a regular view? | Regular views recompute results; MVs store them. |
Does Snowflake refresh automatically? | Yes, using incremental refresh logic. |
When to use Materialized Views? | For repetitive, compute-heavy queries. |
Can MVs include joins? | Yes, but only deterministic queries. |
How to check refresh status? | Use ACCOUNT_USAGE.MATERIALIZED_VIEWS. |
📊 Common Issues and Fixes
Issue | Cause | Fix |
---|---|---|
Stale Data | Refresh suspended | Use REFRESH command |
High Storage Cost | Unused MVs | Drop or optimize filters |
Slow Refresh | Large base tables | Cluster or partition data |
Ineligible Query | Non-deterministic function | Remove RAND(), CURRENT_TIMESTAMP() |
🧠 Optimization Strategies
-
Cluster Base Tables
ALTER TABLE sales CLUSTER BY (region);→ Improves micro-partition pruning for materialized views.
-
Add Selective Filters
CREATE MATERIALIZED VIEW mv_recent_sales ASSELECT * FROM sales WHERE sale_date > DATEADD('month', -1, CURRENT_DATE());→ Reduces refresh size.
-
Monitor Refresh Costs
SELECT * FROM SNOWFLAKE.ACCOUNT_USAGE.MATERIALIZED_VIEW_REFRESH_HISTORY;→ Helps identify expensive refresh patterns.
🧠 Example Use Case in Real Business Scenario
Scenario:
A retail analytics team frequently runs this query:
SELECT region, category, SUM(sales_amount)FROM transactionsGROUP BY region, category;
Problem:
Each query scans billions of rows → very expensive and slow.
Solution:
Create a materialized view:
CREATE MATERIALIZED VIEW mv_sales_summary ASSELECT region, category, SUM(sales_amount) AS total_salesFROM transactionsGROUP BY region, category;
✅ Result:
- Query performance improved by 90%
- Compute costs reduced by 70%
- BI dashboards refresh instantly
🧭 ** – Materialized View Query Lifecycle**
🧠 How to Remember This Concept Easily
Use the keyword “STORED VIEW” — because a Materialized View = Stored Result of a Query.
Memory Hook:
“Materialized = Made Material (Stored Physically)”
That’s the easiest way to recall what makes it different from a regular view.
🧩 Why Learning Materialized Views Is Important
Reason | Explanation |
---|---|
Performance Optimization | Reduce repetitive query time dramatically |
Cost Savings | Save Snowflake credits by avoiding reprocessing |
Scalability | Supports millions of queries efficiently |
Data Freshness | Automatic incremental refresh |
BI Integration | Improves dashboards and reporting tools |
🧠 Common Mistakes to Avoid
Mistake | Impact | Fix |
---|---|---|
Using non-deterministic functions | View fails | Remove random/time-based logic |
Too frequent refresh | High compute | Schedule refresh intervals |
Ignoring base table clustering | Slow refresh | Add proper clustering keys |
Using SELECT * | Storage waste | Select required columns only |
🧾 Comparison: Snowflake Materialized Views vs Cached Results
Feature | Materialized View | Result Cache |
---|---|---|
Persistence | Stored permanently | Temporary |
Refresh | Auto on data change | Auto on same query |
Usage | Query replacement | Exact query repetition |
Cost | Some storage + compute | Free (within 24h) |
🧠 Performance Monitoring Commands
-- View all materialized views in your accountSHOW MATERIALIZED VIEWS;
-- Check refresh statisticsSELECT * FROM SNOWFLAKE.ACCOUNT_USAGE.MATERIALIZED_VIEW_REFRESH_HISTORY;
🧭 ** – Optimization Lifecycle**
🎯 Conclusion
Snowflake Materialized Views are a cornerstone of data performance optimization. They transform heavy analytical queries into precomputed, ready-to-serve datasets — ideal for dashboards, reports, and real-time analytics.
With automatic refresh, incremental updates, and query acceleration, materialized views ensure that data is always fresh and fast.
💡 Final Thought:
“Materialized Views don’t just store data — they store time saved.”
✅ Quick Recap
Topic | Key Takeaway |
---|---|
What are Materialized Views? | Precomputed, stored query results |
Benefits | Speed, cost-efficiency, automation |
Refresh | Automatic or manual |
Use Cases | Aggregations, joins, dashboards |
Mnemonics | P.A.R.T → Precompute, Automate, Refresh, Tune |