Request a demo

Glossary · BigQuery

What is clustered table?

A clustered table in BigQuery stores rows sorted into blocks by up to four clustering columns. Queries that filter or aggregate on those columns skip blocks that cannot match, so they scan and bill fewer bytes; reclustering in the background is free.

Also called: table clustering, CLUSTER BY, clustering columns, block pruning

How clustering works

When you create a table with CLUSTER BY, BigQuery sorts its data into storage blocks by the values of the clustering columns, in the order you list them. Each block keeps metadata about the range of values it holds. A query that filters on the clustering columns uses that metadata to skip blocks, which Google calls block pruning, and only the scanned blocks count toward bytes processed.

CREATE TABLE analytics.orders
PARTITION BY DATE(created_at)
CLUSTER BY customer_id, status
AS
SELECT * FROM analytics.orders_staging;

Rules that matter:

  • Up to four clustering columns, top-level and not repeated, of types such as STRING, INT64, DATE, TIMESTAMP, BOOL, NUMERIC or GEOGRAPHY.
  • Order counts. With customer_id, status, a filter on customer_id benefits; a filter on status alone mostly does not.
  • For STRING columns only the first 1,024 characters are used for clustering.
  • Tables or partitions under about 64 MB gain little.

As new data arrives, BigQuery reclusters in the background. Automatic reclustering is free and does not use your query capacity. In a partitioned table, clustering applies within each partition. See CLUSTER BY for the syntax.

What it does to the bill

On demand, fewer scanned blocks means fewer bytes billed. Suppose the columns a query reads total 2 TiB: a full scan costs $12.50 at $6.25 per TiB. If the table is clustered by customer_id and the blocks holding one customer total 20 GiB, a query for that customer bills about $0.12. How much is pruned depends on the data, and you only see it after the run. On editions, pruning saves slot time, though Google notes clustering does not guarantee fewer slots.

Clustering also helps with partitioning limits: high-cardinality columns that would need far more than 10,000 partitions are a natural clustering key.

Find big tables without clustering

INFORMATION_SCHEMA.COLUMNS marks clustering columns with clustering_ordinal_position. Joined with table sizes from TABLE_STORAGE, it lists the largest tables and how they are clustered:

WITH clustering AS (
  SELECT
    table_schema,
    table_name,
    STRING_AGG(column_name, ', ' ORDER BY clustering_ordinal_position) AS cluster_columns
  FROM `region-us`.INFORMATION_SCHEMA.COLUMNS
  WHERE clustering_ordinal_position IS NOT NULL
  GROUP BY table_schema, table_name
)
SELECT
  s.table_schema AS dataset,
  s.table_name,
  ROUND(s.total_logical_bytes / POW(1024, 4), 2) AS logical_tib,
  IFNULL(c.cluster_columns, '(none)') AS cluster_columns
FROM `region-us`.INFORMATION_SCHEMA.TABLE_STORAGE AS s
LEFT JOIN clustering AS c
  ON c.table_schema = s.table_schema AND c.table_name = s.table_name
WHERE NOT s.deleted AND s.table_type = 'BASE TABLE'
ORDER BY s.total_logical_bytes DESC
LIMIT 20;

A large table with (none) that is queried with filters on the same few columns is a candidate. Check which columns its expensive queries filter on in INFORMATION_SCHEMA.JOBS before choosing the order.

Pitfalls

  • Clustering on a column nobody filters on costs nothing but saves nothing.
  • LIMIT does not reduce bytes billed; only filters on the clustering columns do.
  • A maximum bytes billed setting is checked against the upper-bound estimate, so it can block clustered queries that would have been cheap.

Model the savings with the pricing calculator.

FAQ

What is the difference between partitioning and clustering in BigQuery?

Partitioning splits a table into separate segments by one column and gives an exact cost estimate before the query runs. Clustering sorts data into blocks by up to four columns, works with high-cardinality columns such as customer IDs, and is only known to have pruned blocks after the query runs. Most large tables use both: partition by date, cluster by the columns you filter on.

Why does the dry run of a clustered table show the full size?

BigQuery cannot know how many blocks a filter will prune until the query runs, so the dry-run estimate is an upper bound. The bytes actually billed are usually lower, which also means a maximum bytes billed limit can reject a query that would have stayed under it.

Does clustering an existing table recluster its old data?

No. If you add clustering to an existing table, only data written afterwards is clustered. To cluster all of it, recreate the table from a query with CLUSTER BY.

Read next

Browse the full glossary

Cut BigQuery spend without guessing

Masthead attributes compute and storage cost to every project, user, pipeline and table, finds unused and dead-end tables, and shows where editions or on-demand pricing is cheaper. Mnemonic cut its BigQuery compute cost by 20% within 24 hours of onboarding.