Snowflake

Snowflake Materialized Views: Performance Optimization Guide

2026-03-21
更新: 2026-03-27
NicheeLab Editorial Team

Materialized Views (MVs) are a Snowflake performance optimization feature that physically stores the result of a SELECT statement and automatically refreshes it when the base table changes. By precomputing the results of frequently executed aggregation or filtering queries, MVs dramatically reduce query response times. They are available on Enterprise Edition or higher.

How Materialized Views Work

Unlike a regular view, a Materialized View physically stores the SELECT result as micro-partitions. When DML operations occur on the base table, Snowflake's background service (Serverless) automatically refreshes the MV's contents.

AspectRegular ViewMaterialized View
Data storageNone (SELECT runs at query time)Yes (results stored as micro-partitions)
Query speedScans the base tableReads precomputed results directly
MaintenanceNoneAutomatic (Serverless Credits)
StorageNoneConsumes storage for the result set
EditionAll editionsEnterprise or higher

CREATE MATERIALIZED VIEW Syntax

-- Create an MV that aggregates sales by day and region
CREATE OR REPLACE MATERIALIZED VIEW analytics.daily_sales_mv
AS
SELECT
  order_date,
  region,
  COUNT(*)        AS order_count,
  SUM(amount)     AS total_amount,
  AVG(amount)     AS avg_amount
FROM analytics.orders
WHERE order_date >= '2025-01-01'
GROUP BY order_date, region;

-- Inspect the MV
SHOW MATERIALIZED VIEWS IN SCHEMA analytics;

-- Query the MV (access it like a regular table)
SELECT *
FROM analytics.daily_sales_mv
WHERE region = 'APAC'
  AND order_date >= '2026-03-01'
ORDER BY order_date;

Automatic Query Rewrite

When a query against the base table matches an MV's definition, Snowflake's optimizer automatically rewrites the query to reference the MV. Users do not need to reference the MV explicitly.

-- The optimizer automatically routes this query to daily_sales_mv
SELECT region, SUM(amount) AS total
FROM analytics.orders
WHERE order_date >= '2026-03-01'
GROUP BY region;

-- You can see MATERIALIZED_VIEW_MATCH in the Query Profile steps

Materialized View Restrictions

Restriction categoryDetail
Table referencesSELECT from a single table only (no JOINs)
AggregationGROUP BY and aggregate functions (SUM/COUNT/MIN/MAX/AVG) are allowed
Window functionsOVER clause is not allowed
SubqueriesNot allowed
HAVING / ORDER BY / LIMITNot allowed
UDFNot allowed
Non-deterministic functionsCURRENT_TIMESTAMP() and similar are not allowed
Base table changesDropping a column or changing a type on the base table invalidates the MV

Automatic Maintenance and Cost Management

MV refreshes run automatically on Serverless compute and do not consume your user Warehouse resources. Refresh frequency tracks the rate of DML operations on the base table.

-- Inspect refresh history (last 7 days)
SELECT *
FROM TABLE(INFORMATION_SCHEMA.MATERIALIZED_VIEW_REFRESH_HISTORY(
  DATE_RANGE_START => DATEADD(DAY, -7, CURRENT_DATE()),
  DATE_RANGE_END => CURRENT_DATE()
))
ORDER BY start_time DESC;

-- Long-term cost analysis via the Account Usage view
SELECT
  table_name,
  materialized_view_name,
  SUM(credits_used) AS total_credits
FROM SNOWFLAKE.ACCOUNT_USAGE.MATERIALIZED_VIEW_REFRESH_HISTORY
WHERE start_time >= DATEADD(MONTH, -1, CURRENT_TIMESTAMP())
GROUP BY table_name, materialized_view_name
ORDER BY total_credits DESC;

Clustering on Materialized Views

You can also define Clustering Keys on the Materialized View itself. When the MV's result set is large, adding Clustering Keys further improves partition pruning accuracy for queries against the MV.

-- Add Clustering Keys to the MV
ALTER MATERIALIZED VIEW analytics.daily_sales_mv
  CLUSTER BY (order_date);

Comparison with Dynamic Tables

AspectMaterialized ViewDynamic Table
JOINNot allowedAllowed
SubqueriesNot allowedAllowed
Window functionsNot allowedAllowed
Refresh controlAutomatic (refreshed immediately when base table changes)Specify acceptable latency via TARGET_LAG
Automatic query rewriteYes (optimizer automatically references the MV)No (you query the Dynamic Table explicitly)
Pipeline DAGSelf-contained within a single viewDAG automatically built from references between Dynamic Tables
When to useSpeeding up aggregations or filters on a single tableComplex multi-table transformation pipelines

Use Cases

  • Dashboard acceleration: Precompute results of aggregation queries that scan billions of rows each time, cutting BI tool response from tens of seconds down to a few seconds.
  • Filtering optimization: Store only the rows of a large table that match a specific predicate (WHERE clause) in the MV, reducing the amount of data scanned.
  • Pre-flattening semi-structured data: Convert frequently accessed keys in VARIANT columns into relational columns in the MV to boost performance.

Best Practices

  • Keep the number of MVs in check: Creating many MVs on the same base table forces all of them to refresh on every DML operation, driving up costs.
  • Be cautious with high-churn tables: On tables that update in real time, maintenance costs can balloon.
  • SECURE MATERIALIZED VIEW: Use the SECURE option when you want to hide the MV's definition (SQL) in data sharing scenarios.
  • Drop MVs you no longer need: Promptly remove unused MVs with DROP MATERIALIZED VIEW to stop their maintenance costs.

Check Your Understanding

Performance Optimization

問題 1

On Enterprise Edition, a daily aggregation query against a single large table takes tens of seconds every time it runs on a BI dashboard. You want to precompute the aggregation results to speed it up. Which feature is most appropriate?

  1. Create a Dynamic Table with TARGET_LAG = '1 minute'
  2. Create a Materialized View and rely on the optimizer's automatic query rewrite
  3. INSERT INTO a Temporary Table with aggregated results every hour
  4. Extend the Result Cache TTL to cache the aggregation results permanently

正解: B

Materialized Views are the best fit for speeding up aggregation queries against a single table. MVs are automatically refreshed when the base table changes, and the optimizer automatically rewrites queries against the base table to use the MV. Dynamic Tables are appropriate when you hit MV restrictions such as multi-table JOINs, but for single-table aggregations MVs win thanks to automatic rewrite. Result Cache caches query results but is not permanent and is invalidated when the base table changes.

Frequently Asked Questions

When are Materialized View maintenance costs incurred?

Materialized View maintenance costs are incurred when DML operations (INSERT/UPDATE/DELETE/MERGE) are performed on the base table, because Snowflake's background service automatically refreshes the view contents. The charges appear as Serverless Credits and scale with the base table update frequency and the complexity of the MV's SELECT statement. Use the MATERIALIZED_VIEW_REFRESH_HISTORY table function to inspect refresh history and credit consumption. For very frequently updated tables, MV maintenance costs can become excessive, so validate cost-effectiveness up front.

Are there SQL syntax restrictions for Materialized Views?

Yes — the SELECT statement in a Materialized View has several restrictions. JOINs are not allowed; only SELECTs from a single table are supported. Subqueries, window functions (OVER clause), HAVING, ORDER BY, and LIMIT are not allowed. GROUP BY and aggregate functions (SUM/COUNT/MIN/MAX/AVG) are supported. UDFs (user-defined functions) are also not allowed. If you hit any of these limits, consider using Dynamic Tables instead.

How do I choose between Materialized Views and Dynamic Tables?

Materialized Views precompute and cache aggregation or filtering results from a single table, and queries are transparently rewritten to reference the MV. Dynamic Tables let you declaratively define complex transformations including multi-table JOINs and subqueries, with TARGET_LAG controlling refresh latency. Use MVs to speed up simple aggregations or filters, and Dynamic Tables for complex transformation pipelines. Dynamic Tables require Enterprise Edition or higher.

Check what you learned with practice questions

Practice with certification-focused question sets

無料で問題を解いてみる
Author

NicheeLab Editorial Team

NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.


Related articles
Snowflake

Snowflake Certifications: All 11 Exams Explained (2026)

Every SnowPro certification — Associate, Core, Specialty, Ad...

Snowflake

Snowflake Exam Difficulty Ranking: All 11 Certs Compared (2026)

All 11 SnowPro exams ranked by difficulty with study-time es...

Snowflake

Snowflake Study Guide: Fastest Pass Route by Exam (2026)

How to pass SnowPro certifications efficiently — official ma...

Snowflake

SnowPro Core (COF-C03): Complete Exam Guide (2026)

Pass the SnowPro Core exam — six domains, scope, sample ques...

Snowflake

SnowPro Associate Platform (SOL-C01): Complete Guide (2026)

The entry-level SnowPro Associate exam — scope, weighting, s...

Browse all Snowflake articles (103)
© 2026 NicheeLab All rights reserved.