What you will be able to do
- Describe what a micro-partition is, how big it is, and what metadata Snowflake keeps about it
- Explain how pruning uses micro-partition metadata, and name a predicate that cannot prune
- Interpret clustering depth and use it to judge a table's clustering health
- Decide when a clustering key is worth its cost and choose suitable key columns
Key concept
Micro-partition — The physical storage unit behind every Snowflake table. Snowflake creates micro-partitions automatically as data is loaded and stores each one by column, along with metadata such as the range of values in each column. That metadata is what makes pruning and clustering possible.
1.What a micro-partition is
Traditional data warehouses use static partitioning. A partition is a unit you define and manage yourself with special DDL, and it comes with familiar problems such as maintenance overhead and data skew, where some partitions end up much larger than others. Snowflake takes a different approach called micro-partitioning. You don't declare partitions and you don't maintain them. Every table, whatever its type, is split automatically as data arrives, and the split follows the order in which rows are inserted or loaded.
Each micro-partition holds 50 MB to 500 MB of uncompressed data. The stored size is smaller, because Snowflake always compresses data. Within a micro-partition, groups of rows are stored by column. Each column is compressed on its own, and Snowflake picks the best compression algorithm for each column in each micro-partition. Because columns are stored separately, a query reads only the columns it references.
For each micro-partition, Snowflake records metadata that includes the range of values in each column and the number of distinct values. Micro-partitions can overlap in their value ranges. Together with their small, uniform size, this overlap helps prevent skew.
DML statements such as DELETE, UPDATE and MERGE also use this metadata. Some operations never touch the data files. Deleting every row in a table, for example, is a metadata-only operation. Dropping a column works in a similar way: Snowflake doesn't rewrite the affected micro-partitions when the statement runs.
Checkpoint 1 of 5· Check yourself
Which statement about Snowflake micro-partitions is accurate?
Micro-partitions are created automatically, hold 50–500 MB before compression, and store data by column. Value ranges can overlap, and Snowflake chooses compression separately for each column.
“Each micro-partition contains between 50 MB and 500 MB of uncompressed data”Source: docs.snowflake.com
Sources1
2.Pruning: skipping micro-partitions you don't need
Because Snowflake knows the value range of every column in every micro-partition, it can skip any micro-partition that can't contain matching rows. This is called pruning, and it happens in two steps. First, Snowflake prunes the micro-partitions the query doesn't need. Then it prunes by column within the micro-partitions that remain. Pruning works on semi-structured columns too.
In the ideal case, a filter that selects 10% of a column's value range scans only about 10% of the micro-partitions. The documentation gives an example: a table holds one year of data with date and hour columns, and the data is spread evenly. A query for one particular hour would ideally scan 1/8760th of the micro-partitions, and only the hour column within them. Pruning is more efficient the closer the scanned fraction gets to the fraction of data actually selected. For time-series data, this can mean sub-second responses for slices of an hour or less.
Not every predicate can be used for pruning. A predicate that contains a subquery does not prune, even when the subquery returns a constant.
Checkpoint 2 of 5· Check yourself
A query filters with WHERE sale_date = (SELECT MAX(sale_date) FROM promo_dates). The subquery always returns a single date. How does Snowflake prune on this predicate?
Snowflake does not prune on a predicate that contains a subquery, even when the subquery returns a constant.
“Snowflake does not prune micro-partitions based on a predicate with a subquery, even if the subquery results in a constant.”Source: docs.snowflake.com
Sources1
3.Data clustering and clustering depth
Pruning only works well if similar values sit together. Table data is usually sorted along natural dimensions such as date or region, which is known as clustering. When data is loaded, Snowflake records clustering metadata for each new micro-partition. That metadata covers the total number of micro-partitions, how many of them overlap in value range for a given set of columns, and how deep those overlaps go.
Clustering depth is the average depth of overlapping micro-partitions for the columns you specify. It is 1 or greater for a populated table, and an empty table has a depth of 0. A smaller depth means better clustering. If no micro-partitions overlap at all, the table is in a constant state, meaning clustering can't improve it further. Real tables rarely reach that state, and they don't need to in order to perform well.
You can use clustering depth to track a large table's clustering health over time as DML runs against it, and to decide whether the table needs an explicit clustering key. It isn't a precise measure, though. Query performance is the real test: if queries slow down over time, the table has probably lost its clustering.
Checkpoint 3 of 5· Check yourself
Which interpretation of clustering depth is correct?
Depth measures how much micro-partitions overlap, so lower is better. An empty table has a depth of 0, and the docs name query performance as the best indicator.
“The smaller the average depth, the better clustered the table is with regards to the specified columns.”Source: docs.snowflake.com
Sources1
4.Clustering keys: when to define one and what to put in it
Over time, and especially as DML hits very large tables, data can stop clustering well on the dimensions you care about. You could fix this yourself by sorting rows and reinserting them, but that is cumbersome and expensive. Instead, you can define a clustering key: one or more columns or expressions that Snowflake uses to keep similar rows together in the same micro-partitions. A table with a clustering key is called clustered. You can set the key in CREATE TABLE or add it later with ALTER TABLE, and you can change or drop it at any time. Materialized views can be clustered too. Hybrid tables can't, because their data is always ordered by primary key.
After you define a key, Snowflake handles reclustering automatically. Rows aren't necessarily reorganized straight away, because Snowflake only does maintenance when the table will benefit. That maintenance uses compute and consumes credits, so a clustering key isn't something every table should have. A table is a good candidate when it has a large number of micro-partitions (typically multiple terabytes), when queries are selective or sort the data, and when most queries filter or sort on the same few columns. To keep costs down, look for tables that are queried often and changed rarely. Before you add a key, run a representative set of queries to establish a performance baseline.
| Column profile | Example from the docs | Suitability as a clustering key |
|---|---|---|
| Used in selective filters | invoice_date in date-range fact queries | Top priority |
| Used in join predicates | table2.column_A = table1.column_B | Consider if there is room for more keys |
| Very low cardinality | IS_NEW_CUSTOMER (Boolean) | Minimal pruning |
| Very high cardinality | Nanosecond timestamp values | Poor directly; use an order-preserving expression on the column |
Snowflake recommends no more than 3 or 4 columns or expressions per key, because adding more usually raises cost faster than it improves performance. A good key column has enough distinct values for pruning to work, but few enough that Snowflake can group rows into the same micro-partitions.
Checkpoint 4 of 5· Check yourself
Which table is the strongest candidate for a clustering key?
Clustering pays off on very large tables with selective queries and a high ratio of queries to DML. Hybrid tables can't have clustering keys at all.
“clustering is generally most cost-effective for tables that are queried frequently and do not change frequently.”Source: docs.snowflake.com
Checkpoint 5 of 5· Check yourself
You want to cluster on a column of nanosecond timestamps. What does Snowflake recommend?
Very high cardinality makes clustering expensive to maintain. An expression that keeps the column's ordering reduces the number of distinct values while still allowing pruning.
“defining the key as an expression on the column, rather than on the column directly”Source: docs.snowflake.com
Sources2
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.Every large table should get a clustering key, because clustering always makes queries faster.Why is that wrong?
Clustering consumes credits to set up and maintain. It only makes sense for multi-terabyte tables whose queries benefit enough to cover that cost.
Covered in Clustering keys: when to define one and what to put in it
2.A higher clustering depth means a table is better clustered.Why is that wrong?
Depth measures how much micro-partitions overlap, so a smaller depth means better clustering.
Covered in Data clustering and clustering depth
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Tables are transparently partitioned using the ordering of the data as it is inserted/loaded.”
↩︎ What a micro-partition is“Micro-partitions can overlap in their range of values, which, combined with their uniformly small size, helps prevent skew.”
↩︎ What a micro-partition is“some operations, such as deleting all rows from a table, are metadata-only operations.”
↩︎ What a micro-partition is“a query targeting a particular hour would ideally scan 1/8760th of the micro-partitions in the table”
↩︎ Pruning: skipping micro-partitions you don't need“Then, prune by column within the remaining micro-partitions.”
↩︎ Pruning: skipping micro-partitions you don't need“A table with no micro-partitions (i.e. an unpopulated/empty table) has a clustering depth of 0.”
↩︎ Data clustering and clustering depth“Ultimately, query performance is the best indicator of how well-clustered a table is”
↩︎ Data clustering and clustering depth“All data in Snowflake tables is automatically divided into micro-partitions, which are contiguous units of storage.”
↩︎ Key concept“The smaller the average depth, the better clustered the table is with regards to the specified columns.”
↩︎ Exam trap 2“The data in the dropped column remains in storage.”
↩︎ Prediction“Each micro-partition contains between 50 MB and 500 MB of uncompressed data”
↩︎ Checkpoint“Snowflake does not prune micro-partitions based on a predicate with a subquery, even if the subquery results in a constant.”
↩︎ Checkpoint“The smaller the average depth, the better clustered the table is with regards to the specified columns.”
↩︎ Checkpoint - 2.
“All future maintenance on the rows in the table (to ensure optimal clustering) is performed automatically by Snowflake.”
↩︎ Clustering keys: when to define one and what to put in it“For most tables, Snowflake recommends a maximum of 3 or 4 columns (or expressions) per key.”
↩︎ Clustering keys: when to define one and what to put in it“Typically, this means that the table contains multiple terabytes (TB) of data.”
↩︎ Clustering keys: when to define one and what to put in it“Clustering keys are not intended for all tables due to the costs of initially clustering the data and maintaining the clustering.”
↩︎ Exam trap 1“clustering is generally most cost-effective for tables that are queried frequently and do not change frequently.”
↩︎ Checkpoint“defining the key as an expression on the column, rather than on the column directly”
↩︎ Checkpoint