Snowflake · Intermediate
What are clustering keys and clustering depth in Snowflake?
By Anurag Pandey, Lead Data Engineer ·
The short answer
A clustering key tells Snowflake which columns to keep rows grouped by, so queries filtering on them prune more micro-partitions. Clustering depth measures how much micro-partitions overlap on those columns; lower is better.
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.
Say it like this
About 50 seconds out loud. Use it as a model, then put it in your own words.
Snowflake stores rows in micro-partitions in roughly the order they were loaded. If my queries filter on a column that doesn't follow load order, like customer region, values get spread across many micro-partitions and pruning gets worse. A clustering key fixes that by telling Snowflake to keep rows with similar values together. Automatic Clustering then reclusters the table in the background, and that uses credits. To measure it, I look at clustering depth: the average number of micro-partitions that overlap for the same values. A low depth means good clustering. I can check it with SYSTEM$CLUSTERING_INFORMATION. I'd only add a clustering key on a large table where queries filter on the same columns and the Query Profile shows poor pruning.
What interviewers look for
- You connect clustering to pruning, not to indexes.
- You know Automatic Clustering costs credits, so it's a trade-off.
- You say when it's worth it: large tables with consistent filter columns.
Common mistakes
- Calling a clustering key an index. Snowflake has no traditional indexes on standard tables.
- Adding clustering keys to small tables, where they rarely help.
- Choosing a very high-cardinality column, like a unique ID, as the only key.
Follow-up questions to expect
- 1How would you choose columns for a clustering key?
- 2How do you measure whether clustering is paying for itself?
- 3When would Search Optimization be a better fit than clustering?
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.