Skip to content
BENIBIX Learn

Clustering Key

Data🗄️ Storage & WarehousingTable Partitioning

Also known as Clustered Table, Cluster By, Data Clustering

Rows are kept in order by one column. Then a query can skip blocks.

Think of it like

It is like keeping books on a shelf in A to Z order. You still have one shelf, but finding a book is quick.

Example

A table is split by day and clustered by customer. A query for one customer on one day reads a few blocks of one part.

Why it matters

It helps where splitting a table cannot. A column with millions of values, like a customer ID, is a bad split but a good cluster key.

Definition

A clustering key tells a warehouse to store rows with close values near each other. Unlike a partition, it does not cut the table into separate parts. It sorts the data inside the files, often on a column you filter by a lot. Then the engine skips blocks that cannot match. Snowflake and BigQuery both offer it.

Example code

sql
ALTER TABLE events CLUSTER BY (customer_id);
In Snowflake. Rows for the same customer are kept close together.

Common mistake

Clustering a small table. It costs work to keep sorted and gives little back.

Key takeaways

  1. 1
    Sort, not split. Rows are ordered inside files.
  2. 2
    Skip blocks. Out-of-range blocks are never read.
  3. 3
    For busy filters. Pick the column queries filter on most.

Compare