What does CLUSTER BY do to a Snowflake table, and who maintains that ordering?
answer
- a declared intent, not an index
- columns or expressions on the table
- a background service does the work
- serverless credits, not your warehouse
- suspend it during a big backfill
basics
~20 sCLUSTER BY declares a clustering key — the columns or expressions Snowflake should co-locate rows by, so micro-partitions cover narrow value ranges and prune well. Snowflake's Automatic Clustering service maintains it in the background, consuming serverless credits.
solid answer
~50 s`CLUSTER BY (cols)` on a Snowflake table declares a **clustering key**: the columns or expressions along which rows should be co-located so each micro-partition covers a narrow range of those values and filters on them prune aggressively. It is not an index and creates no separate structure — it changes how data is physically organized. You set it at create time or with `ALTER TABLE t CLUSTER BY (a, b)`, and remove it with `ALTER TABLE t DROP CLUSTERING KEY`. Once declared, the **Automatic Clustering** service maintains it: a serverless background process that rewrites overlapping micro-partitions as DML and loads degrade the organization, without locking the table or blocking queries. It is billed as serverless credits, separate from your virtual warehouses, roughly in proportion to the bytes it rewrites — so a table churned heavily can cost more to recluster than the clustering saves. `ALTER TABLE t SUSPEND RECLUSTER` pauses it, for example during a large backfill.
code
sql · 6 lines-- declare at create time, or add later on a live table
create table events (event_ts timestamp_ntz, tenant_id number, payload variant)
cluster by (to_date(event_ts), tenant_id);
alter table events cluster by (to_date(event_ts), tenant_id);
alter table events drop clustering key;go deeper
Recall the syntax and the purpose: CLUSTER BY (cols) tells Snowflake how to physically organize a table's rows so filters on those columns read fewer micro-partitions.
Explain that no separate structure is created, that Automatic Clustering maintains the ordering in the background as data churns, and that its serverless credits are billed apart from your warehouses.
Show operational control: suspend reclustering around bulk loads, audit declared keys with SHOW TABLES, and watch AUTOMATIC_CLUSTERING_HISTORY credits against the warehouse time the pruning actually saves.
Treat clustering as a paid service with a break-even point and set the standard for when a team may declare a key — churn profile, query profile, and a measured before/after — rather than letting keys accumulate table by table.
## What the clause actually declares ```sql create table events ( event_ts timestamp_ntz, tenant_id number, payload variant ) cluster by (to_date(event_ts), tenant_id); ``` This does not build an index, and it does not create partitions in the way a range-partitioned relational table has partitions. It records an *intent* about physical layout: Snowflake should try to arrange the table's micro-partitions so that each one covers a narrow range of `to_date(event_ts)` and, within that, of `tenant_id`. The payoff is pruning — a query filtering on those expressions can exclude most micro-partitions using their recorded min/max metadata. A clustering key can be columns or expressions on columns, and the practical guidance is to keep it short (a few columns), to put the column your queries filter on most selectively first, and to prefer lower-cardinality leading columns — often via an expression like `to_date(ts)` rather than a raw timestamp with microsecond precision. A key so granular that nearly every row is unique gives Snowflake an ordering it can never economically maintain. You add and remove it on a live table: ```sql alter table events cluster by (to_date(event_ts), tenant_id); alter table events drop clustering key; ``` `SHOW TABLES` reports the current clustering key per table, which is the quickest audit of what is declared where. ## Who does the work Once a clustering key exists, **Automatic Clustering** — a Snowflake-managed serverless service — takes responsibility for it. It monitors the table and, when micro-partitions have drifted into overlapping key ranges, rewrites the worst offenders so they again cover narrow, disjoint ranges. Important properties: - It runs in the **background**, on Snowflake-managed compute, not on your virtual warehouse. Your queries do not block on it and you do not schedule it. - It is **incremental and best-effort**. It never promises a fully sorted table; it works toward reducing overlap, and it prioritizes the micro-partitions where rewriting buys the most. - Because micro-partitions are immutable, "reclustering" literally means reading overlapping files and writing new ones — so it produces exactly the write amplification you would expect, and superseded files remain under Time Travel and Fail-safe retention. - Manual reclustering — running a statement yourself to re-sort the table — is no longer the model; Automatic Clustering is how a declared key is maintained. ## What it costs Automatic Clustering consumes **serverless credits**, billed separately from the credits your virtual warehouses burn. The consumption tracks the volume of data it has to rewrite. That leads to the single most important operational fact about the feature: *the cost is driven by how much the table churns, and the benefit is driven by how much the table is queried on the key*. An append-mostly fact table loaded in roughly key order, read by hundreds of dashboards a day, is the ideal case — little to rewrite, large savings. A staging table fully reloaded every hour is the worst case — everything is rewritten every hour, and the reads may not even filter on the key. Monitor it rather than assume: ```sql select start_time, table_name, credits_used from snowflake.account_usage.automatic_clustering_history where table_name = 'EVENTS' order by start_time desc; ``` The same information is available as an `INFORMATION_SCHEMA` table function for recent activity. Compare those credits against the warehouse credits the improved pruning saves; if the first exceeds the second, the key is a net loss. ## Controlling it ```sql alter table events suspend recluster; -- large backfill runs here alter table events resume recluster; ``` Suspending is the standard move around a bulk rewrite: there is no point paying to reorganize data that is about to be replaced. Resume afterwards and let the service converge once, rather than chasing every intermediate state. Dropping the clustering key stops the service entirely and leaves the data as it currently lies — the existing organization is not undone, it simply stops being maintained. ## The mental model to carry into an interview A clustering key is a **standing instruction plus a paid background service**, not a structure. You are buying maintained physical order with credits, in exchange for scanning fewer micro-partitions. That framing immediately produces the right follow-up questions: how much does the table churn, how many queries actually filter on the key, and what did partitions-scanned look like before and after.
- How is Snowflake's Automatic Clustering billed compared with a query on a virtual warehouse?Separately. Reclustering runs on Snowflake-managed serverless compute and shows up as serverless credits, roughly proportional to the data it rewrites, while your queries burn the credits of whichever virtual warehouse runs them. `SNOWFLAKE.ACCOUNT_USAGE.AUTOMATIC_CLUSTERING_HISTORY` reports the reclustering side per table so you can compare it against the warehouse time the better pruning saves.
- Why suspend reclustering before a large backfill on a clustered Snowflake table?Because the service would spend credits reorganizing data your backfill is about to supersede, and every intermediate state it produces is wasted work. `ALTER TABLE t SUSPEND RECLUSTER` before the load and `RESUME RECLUSTER` afterwards lets it converge once against the final data instead of chasing a moving target.
- Does a clustering key guarantee the table is fully sorted?No. Automatic Clustering is incremental and best-effort: it reduces the overlap between micro-partitions on the key, prioritizing the files where rewriting pays most. There will normally always be some recently written, unclustered data. The goal is enough separation that filters prune well, not a total sort.
- What happens to the data if you drop a Snowflake table's clustering key?Nothing physically changes — the micro-partitions stay exactly as they lie, so pruning is as good as it was the moment you dropped it. What stops is maintenance: no more reclustering credits, and the organization now decays with every load and update until filters on that column prune no better than on any other.
saying these in an interview costs you the question
- Calls a clustering key an index Snowflake builds
- Thinks you must run reclustering manually on a schedule
- Says reclustering runs on your virtual warehouse's credits
- Expects the table to become fully sorted
- Adds a clustering key to a small or rarely queried table