Skip to main content

Data Clustering in Snowflake

By octubre 20, 2023marzo 5th, 2024No Comments

Data clustering in Snowflake, a process of organizing related rows within micropartitions, is pivotal for efficient query performance. While Snowflake automatically manages micropartitions, sub-optimal clustering can occur. To address this, Snowflake offers user-defined clustering keys and its Automatic Clustering service, optimizing data distribution for optimal partition pruning. When implemented thoughtfully, Snowflake’s data clustering methodology ensures swift query responses and resource optimization, enhancing the user experience with large datasets.

What is Data Clustering in Snowflake?

In Snowflake, data clustering refers to the process of organizing and grouping related rows within micropartitions. Snowflake handles micropartitions automatically, and in general this will produce good clustering. However, there are some situations that may produce sub-optimal clustering over time.

In a very large table, queries may be inefficient if they are always predicated on a limited set of columns that are not being used to cluster micropartitions. Furthermore, since micropartitions are created in the order of arrival of the data, the data in these micropartitions may not be optimally stored, and the quality of clustering may degrade over time.

To solve this problem, Snowflake supports the definition of user-defined clustering keys, which allow a Snowflake service called Automatic Clustering. This service will work behind the scenes to redistribute data according to the clustering key to ensure optimal partition pruning. Note that reclustering is only performed if the service determines that the table will benefit from reclustering.

The automatic clustering service runs on Snowflake-managed compute, meaning that it does not need a virtual warehouse assigned to the task. On top of the compute cost, reclustering will also incur storage costs, since data will be physically redistributed and new micropartitions created. (The original micropartitions will be retained through time travel and fail safe storage.)

How to define a clustering key

Clustering can be enabled by specifying a clustering key during table creation or later on using the following commands:

-- Define a clustering key at table creation
CREATE TABLE <name> ... CLUSTER BY ( <expr1> [ , <expr2> ... ] )

-- Alter a table to add a clustering key
ALTER TABLE <name> CLUSTER BY ( <expr1> [ , <expr2> ... ] )

A clustering key for a table can use multiple columns, and it may be based on expressions.

Clustering Best Practices

Snowflake offers a set of best practices to optimize data clustering:

  1. Use on large tables. Clustering is recommended for tables with sizes exceeding 1TB.
  2. Diagnose poor pruning. Cluster only if the number of micro-partitions scanned closely matches the total number leading to poor query response times.
  3. Inform users. Users should be aware of the cluster key and incorporate it into WHERE clauses for queries.
  4. Beware of frequent updates. Avoid placing cluster keys on tables where a significant portion of partitions is frequently updated to prevent high re-clustering costs.
  5. Consider Cardinality. Ideal cluster keys possess intermediate cardinality – neither too high nor too low.

Selecting appropriate columns for clustering keys is pivotal, and Snowflake offers the following best practices:

  1. Frequently used columns. Clustering keys should include columns frequently used in WHERE and JOIN clauses.
  2. Cardinality. Column cardinality should be balanced for effective partition pruning without overwhelming Snowflake with excessive overhead.
  3. Multi-column clustering. For multi-column clustering keys, arrange columns from lowest to highest cardinality.
  4. Limit columns. Limit clustering keys to 3 or 4 columns to maintain manageable maintenance costs.
  5. Use expressions. In cases of too high cardinality, expressions can help reduce cardinality.

Understanding Clustering Depth and Clustering Width

Clustering depth and clustering width are crucial metrics for evaluating the efficiency of data clustering.

  • Clustering Depth: the number of overlaps at a specific point in the partition. A table with no micro-partitions has a clustering depth of 0. Smaller average depth implies better clustering.
  • Clustering Width: the number of partitions that overlap.

Monitoring these metrics helps gauge the health of clustering in a table, especially during dynamic data modifications. Snowflake provides the following system functions to monitor the clustering health of a table:

-- Compute the average clustering depth of the table 
-- according to the specified columns, 
-- or according to the defined clustering key. 

-- Returns general clustering information for a table, 
-- including average depth, clustering keys, 
-- suggestions to improve clustering, number of micropartitions, 
-- average overlaps, and clustering errors


In conclusion, Snowflake’s data clustering methodology, when implemented thoughtfully and in accordance with best practices, ensures swift query responses and optimal utilization of resources, providing a seamless experience for users interacting with large datasets.


Leave a Reply