Request a demo

BigQuery · DDL statement

BigQuery CLUSTER BY

CLUSTER BY sorts a BigQuery table's storage blocks by up to four columns, so queries that filter on those columns skip blocks and bill fewer bytes. Use it on large tables filtered by columns such as customer_id or status, usually on top of date partitioning.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

CLUSTER BY is a clause of CREATE TABLE (and of CREATE MATERIALIZED VIEW) that tells BigQuery to keep rows with similar values of the clustering columns together in the same storage blocks. Each block records the range of values it holds, so a query with a filter such as customer_id = 'c_381' reads only the blocks that can contain that value. This is block pruning, and on-demand you are billed only for the blocks that were read. On a partitioned table, data is clustered inside each partition, which is why partitioning by date and clustering by the columns you filter on is the standard layout for large tables.

You can name up to four columns. They must be top-level, non-repeated columns of type STRING, INT64, NUMERIC, BIGNUMERIC, DATE, DATETIME, TIMESTAMP, BOOL, GEOGRAPHY or RANGE, without a collation; for STRING values only the first 1,024 characters are used. Order matters: BigQuery sorts by the first column, then the second within it, and so on, so a filter on the first column prunes the most and a filter that skips it prunes much less. Put the column you filter on most often first. Wrapping a clustering column in a function (LOWER(customer_id) = ...) or comparing it with another column disables block pruning.

The savings only exist at run time. BigQuery cannot know before execution which blocks a filter will skip, so the dry-run estimate for a clustered table is an upper bound, and the bytes you are billed (visible in INFORMATION_SCHEMA.JOBS) are often lower. For the same reason, LIMIT can reduce bytes scanned on a clustered table, which it never does on an unclustered one. If you need an exact estimate before running a query, that is what partitioning gives you.

As data arrives, BigQuery reclusters the table in the background, at no charge and without using your slots. Changing the clustering columns of an existing table is not done in SQL: there is no ALTER TABLE option for it, so use bq update --clustering_fields or the tables API. Only data written afterwards follows the new spec; to recluster existing rows, rewrite them, either with UPDATE t SET col = col WHERE TRUE or by recreating the table with CREATE TABLE AS SELECT. Tables or partitions under about 64 MB gain little from clustering.

Syntax

CREATE [OR REPLACE] TABLE table_name
  (column_definitions)
[PARTITION BY partition_expression]
CLUSTER BY column_1[, column_2[, column_3[, column_4]]]
[OPTIONS (table_options)]
[AS query_statement]

CREATE MATERIALIZED VIEW view_name
[PARTITION BY partition_expression]
CLUSTER BY column_1[, ...]
AS query_expression

Arguments and return value

ArgumentDescription
column_1 ... column_4Up to four top-level, non-repeated columns, in sort order: the first column has the strongest effect on pruning. Types: STRING, INT64, NUMERIC, BIGNUMERIC, DATE, DATETIME, TIMESTAMP, BOOL, GEOGRAPHY, RANGE.
partition_expressionOptional. With partitioning, clustering applies inside each partition. See PARTITION BY.

Returns: A table (or materialized view) whose storage blocks are sorted by the clustering columns. Queries that filter on those columns read, and on-demand bill, only the matching blocks.

Examples

Partition by day, cluster by the columns you filter on

Most queries on this table ask for one customer over a date range, sometimes narrowed to one country, so customer_id goes first.

CREATE TABLE `project.dataset.page_views`
(
  view_ts TIMESTAMP,
  customer_id STRING,
  country STRING,
  url STRING,
  load_time_ms INT64
)
PARTITION BY DATE(view_ts)
CLUSTER BY customer_id, country
OPTIONS (require_partition_filter = TRUE);

Filters that use the clustering, and filters that don't

All three read a single partition. Only the first gets full block pruning. The second skips the first clustering column, and the third wraps it in a function, which turns block pruning off.

-- Full block pruning: filters on the first clustering column
SELECT COUNT(*) FROM `project.dataset.page_views`
WHERE DATE(view_ts) = '2026-11-22' AND customer_id = 'c_381';

-- Weak pruning: skips customer_id and filters only on country
SELECT COUNT(*) FROM `project.dataset.page_views`
WHERE DATE(view_ts) = '2026-11-22' AND country = 'DE';

-- No block pruning: a function on the clustering column
SELECT COUNT(*) FROM `project.dataset.page_views`
WHERE DATE(view_ts) = '2026-11-22' AND LOWER(customer_id) = 'c_381';

Recluster existing rows after changing the clustering columns

After bq update --clustering_fields=customer_id,status dataset.orders, only new data follows the new order. This UPDATE rewrites every row so the whole table is clustered: on-demand it bills at least the full table size, and it moves long-term storage back to active pricing.

UPDATE `project.dataset.orders`
SET customer_id = customer_id
WHERE TRUE;

Common mistakes

  • Ordering the columns by habit instead of by filters. With CLUSTER BY country, customer_id, a query on customer_id alone prunes far less than with the reverse order. Put the most frequently filtered column first.
  • Defeating pruning in the WHERE clause. CAST(customer_id AS STRING) = '10000', LOWER(customer_id) = ... and customer_id = referrer_id cannot prune blocks. Compare the bare column with constants.
  • Trusting the dry run or maximum_bytes_billed to reflect clustering. Both work from an upper-bound estimate, so a query capped with maximum bytes billed can fail with Query exceeded limit for bytes billed even though it would have read far less.
  • Expecting ALTER TABLE ... CLUSTER BY to exist, as in Snowflake. BigQuery has no DDL to change clustering; use bq update --clustering_fields or the API, then rewrite old rows if they need to be clustered too.
  • Trying to drop or rename a clustering column. ALTER TABLE DROP COLUMN and RENAME COLUMN refuse clustering columns; change the clustering spec first, or recreate the table.

Cost and performance

  • On-demand you pay only for the blocks read, but the saving is known only after the job runs: compare total_bytes_billed in INFORMATION_SCHEMA.JOBS with the dry-run estimate. On a clustered table LIMIT can also stop the scan early and lower the bytes billed, which it never does on an unclustered table.
  • Automatic reclustering is free and does not consume your reservation's slots, unlike Snowflake's automatic clustering, which consumes credits. A manual rewrite with UPDATE ... SET col = col WHERE TRUE is ordinary DML: it bills at least the whole table on demand and resets the 90-day long-term storage timer.
  • UPDATE, DELETE and MERGE statements that filter on clustering columns read and rewrite fewer blocks, which matters most for frequent small DML on large tables.

FAQ

How many columns can I cluster a BigQuery table by?

Up to four. Their order is the sort order, so the first column gives the strongest pruning. If you need more columns, combine clustering with partitioning.

Should I partition or cluster a BigQuery table?

Usually both: partition by the date you filter on, which gives exact cost estimates and cheap partition deletes, and cluster by up to four other filter columns. Clustering alone suits tables whose partitions would be smaller than about 10 GB or that are filtered on high-cardinality columns.

How do I add clustering to an existing table?

Run bq update --clustering_fields=col1,col2 dataset.table (or patch the table through the API). New data is clustered from then on; to cluster existing rows, rewrite them with UPDATE ... SET col = col WHERE TRUE or recreate the table with CREATE TABLE ... CLUSTER BY ... AS SELECT.

Why doesn't the dry run show the savings from clustering?

Block pruning is decided while the query runs, so the estimate before execution is an upper bound. The real figure is total_bytes_billed of the finished job.

Errors you may hit

More DDL statements

All BigQuery DDL statements · All statements and clauses · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.