Choose clustering keys and evaluate maintenance
A clustering key guides Snowflake’s background organization of table data for frequently used access patterns.
Your learning goal
Choose clustering keys from workload evidence, inspect background maintenance and weigh selective query gains against service and retained-storage costs.
Understand the idea
A concrete example
A large orders table repeatedly queried by date and region may benefit from organization matching those filters. A tiny table or a mostly full-scan workload may gain little. First record scan ratios and query frequency.
| Concept | What it means |
|---|---|
| Key expression | Column or expression used to organize related values. |
| Cardinality | Number of distinct values in the key. |
| Automatic Clustering | Snowflake-managed reclustering when useful. |
| Maintenance cost | Service consumption and storage consequences of rewritten data. |
Remember: Choose keys for measured repeated access patterns and evaluate the ongoing maintenance cost alongside query savings.
Explain it step by step · full theory
Learning goals and prerequisites
Choose clustering keys from workload evidence, inspect background maintenance and weigh selective query gains against service and retained-storage costs.
Use the M15_LAB setup from lesson 1. Know SELECT, joins, aggregates and window functions from Modules 03–04. Follow the supplied examples with an authorized warehouse and an isolated learning role. Keep the same data and account context when comparing runs.
Start with repeated expensive filters
A large orders table repeatedly queried by date and region may benefit from organization matching those filters. A tiny table or a mostly full-scan workload may gain little. First record scan ratios and query frequency.
A unique random ORDER_ID has high cardinality and can be a poor general-purpose clustering choice. Very low cardinality can also limit discrimination. A timestamp expression such as TO_DATE reduces granularity while preserving useful ordering.
Multiple columns encode priorities
A multi-column key needs a deliberate order. Consider distinct counts, common predicates and whether the leading expression leaves room for later expressions to contribute. Compare candidate information and actual queries rather than applying a universal key recipe.
Limit the key to useful expressions. Adding many columns increases maintenance complexity and can dilute the organization needed by the main workload.
Background work has a lifecycle
Automatic Clustering reorganizes eligible clustered data as needed. It consumes separately metered resources and can create historical storage through rewritten micro-partitions. Check the current Automatic Clustering/Optima documentation for the account’s service behavior.
A newly declared key does not mean reclustering is finished. Observe service history and repeated query scans over time. For a disposable experiment, suspend reclustering or drop the object when finished. Use authorized ownership/control privileges.
Classic and Optima billing need different assumptions
Clustering Classic meters service compute; Optima Clustering uses ingested uncompressed data volume and the applicable overlap factor. Inspect the table version and current service consumption rules before estimating a bill. Key changes can incur a one-time cost.
Suspending Optima Clustering pauses charges for new ingestion, but resuming can include catch-up cost. Repeated suspension is therefore not a free maintenance strategy. For a finished disposable experiment, drop its key or table after reviewing the scope. History can retain rewritten storage.
Choose a first key candidate
| Workload | Possible candidate | What to measure |
|---|---|---|
| Repeated date ranges | ORDER_DATE | Scanned partitions for the same range |
| Timestamp ranges at day granularity | TO_DATE(ORDER_TS) | Pruning and maintenance versus raw timestamps |
| Date and customer predicates | ORDER_DATE, CUSTOMER_ID | Whether the second expression improves the workload |
| One rare random order ID | Investigate Search Optimization | Lookup selectivity and maintenance economics |
Common mistakes and next action
The useful organization, workload and service maintenance all affect the outcome.
Choose keys for measured repeated access patterns and evaluate the ongoing maintenance cost alongside query savings.
Words used in this lesson
- Key expression
- Column or expression used to organize related values.
- Cardinality
- Number of distinct values in the key.
- Automatic Clustering
- Snowflake-managed reclustering when useful.
- Maintenance cost
- Service consumption and storage consequences of rewritten data.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Choose clustering keys and evaluate maintenance
Identify access pattern
Repeated selective date reports scan too many partitions. Confirm the symptom first.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Identify access pattern
Repeated selective date reports scan too many partitions. Confirm the symptom first.
Query frequency → Date filters → Scanned partitions
- Choose expression
Use date granularity and useful secondary dimensions based on the real workload.
Candidate keys → Cardinality → Key order
- Maintain and observe
Background reorganization costs resources. Compare scan improvements with service usage and churn.
Recluster → New organization → Compute + history cost
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Manual examples for L2_OBJECTS_LAB.M15_LAB. Select an authorized warehouse and a role with database/schema access and permission to create the lab objects. Setup runs once with fresh names. Optional paid services require their stated privileges and edition. SQL and performance timings have not been executed or measured for this lesson.
Optional isolated key experiment
-- Requires ORDERS_LARGE from the previous lesson. Paid background work may start.
CREATE TABLE ORDERS_CLUSTER_TEST CLONE ORDERS_LARGE;
ALTER TABLE ORDERS_CLUSTER_TEST CLUSTER BY (ORDER_DATE,CUSTOMER_ID);
SELECT SYSTEM$CLUSTERING_INFORMATION('L2_OBJECTS_LAB.M15_LAB.ORDERS_CLUSTER_TEST');
-- Stop maintenance after your controlled experiment:
ALTER TABLE ORDERS_CLUSTER_TEST SUSPEND RECLUSTER;What to expect and why
Inspect actual maintenance and scans. A clone may begin with reclustering suspended, and a key change can affect maintenance behavior. Do not assume this immediate sequence gives the service time to improve organization.
Optional expression-based key on timestamp data
-- Optional paid experiment; use an authorized lab role.
-- CREATE TABLE ORDERS_TS_TEST AS
-- SELECT *,ORDER_DATE::TIMESTAMP_NTZ AS ORDER_TS FROM ORDERS_LARGE;
-- ALTER TABLE ORDERS_TS_TEST CLUSTER BY (TO_DATE(ORDER_TS));
-- SELECT SYSTEM$CLUSTERING_INFORMATION(
-- 'L2_OBJECTS_LAB.M15_LAB.ORDERS_TS_TEST');
-- ALTER TABLE ORDERS_TS_TEST DROP CLUSTERING KEY;
-- DROP TABLE ORDERS_TS_TEST;What to expect and why
TO_DATE groups timestamps at day granularity instead of maintaining near-unique timestamps. ORDERS_LARGE is required. Wait and observe an authorized experiment before its cleanup; this tiny example does not establish a performance benefit.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Would you cluster a six-row table for a once-a-month full scan? Explain the cost-benefit decision.
Compare your reasoning
Choose keys for measured repeated access patterns and evaluate the ongoing maintenance cost alongside query savings.
Key takeaway
Choose keys for measured repeated access patterns and evaluate the ongoing maintenance cost alongside query savings.
Connect this lesson to the learning path
Official sources and curriculum
Module 15 · See module scope
By Rohit Ray · Independent educational lesson · Explore this visual lab