What you will be able to do
- Explain how micro-partition metadata lets Snowflake skip data, and which predicates prevent pruning
- Predict whether a repeated query will be served from the result cache, and set auto-suspend to keep the warehouse cache
- Enable search optimization and query acceleration, check QAS eligibility, and cap their cost
- Choose between a materialized view, search optimization and a clustering key by weighing their compute and storage cost
1.Micro-partitions and pruning
How much data a query scans depends on how the table is stored. Snowflake automatically splits every table into micro-partitions of 50 to 500 MB of uncompressed data, stored by column. For each micro-partition it records metadata such as the range of values in each column and the number of distinct values. During a query it compares the filter with those ranges and skips (prunes) micro-partitions that can't contain matching rows. Inside the micro-partitions it does read, it scans only the columns the query references.
In an ideal case, a filter that selects 10% of a column's value range scans about 10% of the micro-partitions. How close a table gets depends on how its rows are physically ordered, and Snowflake partitions data in the order it is inserted or loaded. An events table loaded day by day prunes well on event date, because each micro-partition covers only a few days. The same table filtered on customer ID can scan almost everything, because every micro-partition contains a wide range of customer IDs.
Clustering depth measures how much micro-partition value ranges overlap for the given columns. A smaller depth means better clustering. Query performance is still the final test: if a large table's queries slow down as DML changes it, it has probably lost its clustering. Measuring depth with system functions and defining keys are covered in Clustering keys and Automatic Clustering.
Checkpoint 1 of 5· Check yourself
A large orders table is loaded once a day, so its rows arrive in order-date order. Queries that filter on one order date scan only a few micro-partitions. Queries that filter on one customer_id scan almost all of them. What explains the difference?
Pruning compares the filter with each micro-partition's value range for a column. Because the table is partitioned in load order, date ranges are narrow, but every micro-partition holds a wide spread of customer IDs, so few can be skipped. Snowflake records ranges for every column and stores data by column, and warehouse size has no effect on how many micro-partitions a query can skip.
“Tables are transparently partitioned using the ordering of the data as it is inserted/loaded.”Source: docs.snowflake.com
Sources1
2.Result cache, warehouse cache and metadata
The result cache stores the result of every query for 24 hours. A repeated query can be answered from it without running at all. Each reuse resets the 24-hour period, up to a maximum of 31 days from the first run. Reuse is on by default and can be turned off at account, user or session level with USE_CACHED_RESULT. A query can only reuse a result when all of these conditions hold, and even then reuse isn't guaranteed:
- The text matches the earlier query exactly. A different case or a table alias prevents full reuse.
- It contains no non-reusable functions such as UUID_STRING, RANDOM or RANDSTR, no external functions, and no hybrid tables.
- Neither the table data nor its micro-partitions have changed, for example through reclustering.
- The role has the privileges the cached query needs. For a SHOW query, the role must be the same role that produced the result.
Only the second run. The third has lowercase keywords, so it doesn't match the cached query exactly and runs again.
The warehouse cache is different. A running warehouse keeps the table data it has read, and later queries on the same warehouse can read that data instead of reading the table again. The cache is dropped when the warehouse suspends, so auto-suspend trades idle credits against cache reuse. Snowflake's guidelines are: suspend immediately for tasks, about 5 minutes for ad-hoc data science work, and at least 10 minutes for BI warehouses. Match the setting to how often queries arrive. If a query arrives every 30 minutes, a 10-minute auto-suspend means you pay for idle time and still lose the cache before the next query.
ALTER WAREHOUSE my_wh SET AUTO_SUSPEND = 600;Micro-partition metadata saves work as well. Some operations, such as deleting every row in a table, change only metadata and scan no data.
Checkpoint 2 of 5· Check yourself
You run DELETE FROM staging_events; with no WHERE clause on a large table that nobody has queried today. It finishes almost instantly and the query profile shows no table data scanned. Which mechanism explains this?
Snowflake records metadata for every micro-partition, and some DML, such as deleting all rows, changes only that metadata, so it needs no scan. The result cache stores query results, not DML effects, and the warehouse cache holds data a running warehouse has already read. It can't explain a scan that never happened on a table nobody has queried.
“For example, some operations, such as deleting all rows from a table, are metadata-only operations.”Source: docs.snowflake.com
3.Search optimization service
Search optimization fixes the case where a table is ordered by date but queries look up one customer ID. The service builds and maintains a search access path that records which micro-partitions might contain each column value, so a selective lookup can skip the rest. It helps equality and IN lookups that return a few rows, substring and regular-expression searches (LIKE, ILIKE, RLIKE), SEARCH and SEARCH_IP text searches, and lookups on elements of VARIANT, OBJECT and ARRAY columns. Queries don't change; some just run faster.
ALTER TABLE t1 ADD SEARCH OPTIMIZATION ON EQUALITY(c1, c2, c3);Enable it on specific columns with the ON clause rather than on the whole table, so you don't pay to build access paths for columns nobody filters on. You need OWNERSHIP on the table and ADD SEARCH OPTIMIZATION on the schema. Write predicates that the service can use. A cast on the column, such as CAST(c2 AS NUMBER) = 2, prevents it, while a cast on the constant does not.
Search optimization costs both storage and serverless compute. The access path is usually about a quarter of the table's size and can reach the full table size. Building it on a multi-terabyte table can cause a short spike in credits, and queries aren't faster until the build finishes. Check search_optimization_progress in SHOW TABLES before measuring the improvement. Maintenance costs rise with churn, and batching DML reduces them. SYSTEM$ESTIMATE_SEARCH_OPTIMIZATION_COSTS estimates the cost beforehand, but actual costs can differ by up to 50%.
Checkpoint 3 of 5· Check yourself
An events table is loaded day by day. Queries on event date run fast, but a support tool's WHERE customer_id = 48213 lookup returns 3 rows and still scans almost every micro-partition. Which change targets this, and how should you check that it worked?
Rows go into micro-partitions in load order, so every micro-partition holds a wide range of customer IDs and pruning can't skip them. A search access path on customer_id records which micro-partitions might hold each value, and putting it on only that column avoids paying for access paths nobody uses. Queries get faster only after the background build finishes, so wait for that before you measure. Warehouse size and cluster count don't change how many micro-partitions a query can skip.
“Tables are transparently partitioned using the ordering of the data as it is inserted/loaded.”Source: docs.snowflake.com
4.Query acceleration service and the scale factor
The query acceleration service (QAS) targets outlier queries: the occasional query that uses far more resources than the rest of the warehouse's workload. When QAS is enabled, Snowflake can move the scanning and filtering of large scans to shared serverless compute. Upsizing affects every query on the warehouse, but QAS is used only for queries that benefit. That makes it cost-effective for mixed workloads where a few large ad-hoc scans run alongside many small queries. Queries can still be ineligible: too few partitions to scan, filters that aren't selective enough, a GROUP BY with too many distinct values, or nondeterministic functions such as RANDOM.
Check eligibility before turning QAS on. SYSTEM$ESTIMATE_QUERY_ACCELERATION('<query_id>') takes a query from the last 14 days. It returns eligible, ineligible, accelerated or invalid, and for eligible queries it estimates run times at several scale factors. To find candidates across the account, the ACCOUNT_USAGE.QUERY_ACCELERATION_ELIGIBLE view lists unaccelerated queries with the execution time QAS could take over. If an ineligible query returns NO_LARGE_ENOUGH_SCAN, QAS can't help it.
The scale factor limits QAS spending. It is a multiple of the warehouse's own credit rate: with a scale factor of 5, serverless compute can use at most five times the warehouse's rate. Those credits are billed separately from the warehouse. Explicitly setting ENABLE_QUERY_ACCELERATION = TRUE in CREATE WAREHOUSE gives a scale factor of 8. New multi-cluster and Gen2 warehouses have QAS on by default with a factor of 2. A factor of 0 removes the limit.
ALTER WAREHOUSE my_wh SET
ENABLE_QUERY_ACCELERATION = true
QUERY_ACCELERATION_MAX_SCALE_FACTOR = 0;Checkpoint 4 of 5· Check yourself
Most queries on a Medium reporting warehouse finish in a few seconds. A few times a day, an analyst runs an ad-hoc query that scans a year of a large fact table with a selective filter, and it takes many minutes. You want those scans to run faster without raising the cost of every other query, and you want a limit on the extra spend. What should you do?
QAS handles outlier queries. Only the queries that benefit send their scanning and filtering to serverless compute, and the scale factor caps that spend at a multiple of the warehouse's credit rate. Upsizing raises the rate for every query, including the short ones that won't get faster. A scale factor of 0 removes the limit rather than setting spend to zero. Extra clusters don't make a single query run faster.
“it can improve overall warehouse performance by reducing the impact of outlier queries, which are queries that use more resources than the typical query”Source: docs.snowflake.com
5.Materialized views, clustering or search optimization: weighing the cost
A materialized view stores the precomputed result of its query, and Snowflake keeps it up to date in the background with serverless compute. It suits queries that read a small subset of data through expensive operations such as aggregation, like a daily SUM(totalprice) that dozens of dashboards request. It only helps queries that need the rows and columns it contains. If a base-table column is changed or dropped, the view is suspended and has to be recreated.
Materialized view maintenance cost grows with the number of views on a table and with how much each one changes. A view clustered differently from its base table can rewrite many more micro-partitions than the insert that triggered the update. No tool estimates materialized view cost. Suspending a view only postpones its maintenance cost. Resource monitors can't cap the MATERIALIZED_VIEW_MAINTENANCE warehouse.
| Optimization | Speeds up | Cost profile |
|---|---|---|
| Clustering the table | Equality and range searches; only one key per table | Compute for reclustering; reorganizes existing data rather than adding a copy |
| Search optimization service | Equality, substring, text, VARIANT and geospatial searches | Storage for the access path plus maintenance compute; Enterprise Edition |
| Materialized view | Equality and range searches, sorts and costly aggregations on the subset it holds | Storage for the results plus maintenance compute; Enterprise Edition |
Choose based on the query pattern, then check the cost. Range filters on the column that orders the data call for a clustering key. Point lookups on a column that has nothing to do with load order call for search optimization. An aggregation repeated over a stable subset calls for a materialized view. All three cost credits before you can measure the improvement, and frequent updates to a table can make any of them too expensive to maintain. Automatic Clustering also triggers extra maintenance for search optimization and materialized views on the same table. Start with one or two tables and compare query cost before and after.
Storage cost is part of the same decision. Search optimization and materialized views add storage billed at the standard rate. Reclustering can increase Fail-safe storage, which matters on tables with frequent changes; retention settings for historical data are covered in Time Travel (retention period and AT | BEFORE). Dropping a column doesn't immediately reclaim its storage, because the micro-partitions holding that column's data aren't rewritten when the column is dropped.
Checkpoint 5 of 5· Check yourself
After running ALTER TABLE ... DROP COLUMN to remove a wide, unused VARIANT column from a multi-terabyte table, the team sees the table's billed storage stay the same. What explains this?
When a column is dropped, Snowflake doesn't rewrite the micro-partitions that contain its data, so that data stays in storage and the storage cost doesn't drop right away. Snowflake compresses each column separately, and a DROP doesn't trigger reclustering.
“the micro-partitions that contain the data for the dropped column are not re-written when the drop statement is executed.”Source: docs.snowflake.com
Sources
(12)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Each micro-partition contains between 50 MB and 500 MB of uncompressed data”
↩︎ Micro-partitions and pruning“only the columns referenced by a query are scanned.”
↩︎ Micro-partitions and pruning“should ideally only scan 10% of the micro-partitions”
↩︎ Micro-partitions and pruning“Tables are transparently partitioned using the ordering of the data as it is inserted/loaded.”
↩︎ Micro-partitions and pruning“Ultimately, query performance is the best indicator of how well-clustered a table is”
↩︎ Micro-partitions and pruning“some operations, such as deleting all rows from a table, are metadata-only operations.”
↩︎ Result cache, warehouse cache and metadata“The data in the dropped column remains in storage.”
↩︎ Materialized views, clustering or search optimization: weighing the cost“Snowflake does not prune micro-partitions based on a predicate with a subquery, even if the subquery results in a constant.”
↩︎ Prediction“For example, some operations, such as deleting all rows from a table, are metadata-only operations.”
↩︎ Checkpoint“Tables are transparently partitioned using the ordering of the data as it is inserted/loaded.”
↩︎ Checkpoint“the micro-partitions that contain the data for the dropped column are not re-written when the drop statement is executed.”
↩︎ Checkpoint - 2.
“For persisted query results of all sizes, the cache expires after 24 hours.”
↩︎ Result cache, warehouse cache and metadata“Snowflake resets the 24-hour retention period for the result, up to a maximum of 31 days”
↩︎ Result cache, warehouse cache and metadata“UUID_STRING, RANDOM, and RANDSTR are good examples of non-reusable functions.”
↩︎ Result cache, warehouse cache and metadata“can be overridden at the account, user, and session level using the USE_CACHED_RESULT session parameter.”
↩︎ Result cache, warehouse cache and metadata“Any difference in syntax, including lowercase versus uppercase, or the use of table aliases, will inhibit 100% cache reuse.”
↩︎ Exam trap 2 - 3.
“the cache is dropped when the warehouse is suspended.”
↩︎ Result cache, warehouse cache and metadata“Snowflake recommends setting auto-suspend to at least 10 minutes to maintain the cache for users.”
↩︎ Result cache, warehouse cache and metadata - 4.
“The search access path keeps track of which values of the table’s columns might be found in each of its micro-partitions”
↩︎ Search optimization service“The following can’t use the search optimization service because the cast is on the table’s column”
↩︎ Search optimization service“Queries are not accelerated until the search access path has been fully built.”
↩︎ Exam trap 1 - 5.
“In general, enabling search optimization only for specific columns is the best practice.”
↩︎ Search optimization service - 6.
“Typically, however, the size is approximately 1/4 of the original table’s size.”
↩︎ Search optimization service“Resource consumption is higher when there is high churn”
↩︎ Search optimization service - 7.
“unlike upsizing a warehouse, it is only used for queries that benefit from increased compute power”
↩︎ Query acceleration service and the scale factor“sets a limit on how much serverless compute can be used by a warehouse”
↩︎ Query acceleration service and the scale factor“consume credits independent of the credits consumed by the warehouse, and are billed separately”
↩︎ Query acceleration service and the scale factor - 8.
“There aren’t enough partitions in the scan.”
↩︎ Query acceleration service and the scale factor“The scale factor is 8 when you explicitly set ENABLE_QUERY_ACCELERATION = TRUE in CREATE WAREHOUSE.”
↩︎ Query acceleration service and the scale factor“it can improve overall warehouse performance by reducing the impact of outlier queries, which are queries that use more resources than the typical query”
↩︎ Checkpoint - 9.https://docs.snowflake.com/en/sql-reference/functions/system_estimate_query_accelerationOfficial docs
“Query ID must be for a query executed within the last 14 days; otherwise, the status is invalid.”
↩︎ Query acceleration service and the scale factor - 10.
“A materialized view can provide a performance boost for queries that access a small subset of data using expensive operations like aggregation.”
↩︎ Materialized views, clustering or search optimization: weighing the cost“If a table is constantly updated, the cost of maintaining a storage optimization might be prohibitive.”
↩︎ Materialized views, clustering or search optimization: weighing the cost“Automatic Clustering activities on the base table can trigger maintenance costs for materialized views and the Search Optimization Service”
↩︎ Materialized views, clustering or search optimization: weighing the cost“The Search Optimization Service and materialized views also require the Enterprise Edition or higher, which increases the price of a credit.”
↩︎ Materialized views, clustering or search optimization: weighing the cost“reclustering can incur additional storage costs if it increases the size of Fail-safe storage.”
↩︎ Materialized views, clustering or search optimization: weighing the cost - 11.
“Materialized views improve performance only for the subset of rows and columns included in the materialized view.”
↩︎ Materialized views, clustering or search optimization: weighing the cost“A table can be clustered only on a single key, which can contain one or more columns or expressions.”
↩︎ Materialized views, clustering or search optimization: weighing the cost - 12.
“There are no tools to estimate the costs of maintaining materialized views.”
↩︎ Materialized views, clustering or search optimization: weighing the cost“you cannot use them to control credit usage for the Snowflake-provided warehouses, including the MATERIALIZED_VIEW_MAINTENANCE warehouse.”
↩︎ Materialized views, clustering or search optimization: weighing the cost“Each materialized view stores query results, which adds to the monthly storage usage for your account.”
↩︎ Materialized views, clustering or search optimization: weighing the cost“suspending maintenance typically only defers costs, rather than reducing them.”
↩︎ Exam trap 3