Clustered Table
A clustered table stores its rows sorted by the values of one or more chosen columns, so that a query filtering on those columns reads fewer storage blocks. It is a physical layout decision, invisible in query results and visible only in how much data a query has to touch.
Two constraints from Google’s Introduction to clustered tables determine how it is used. First, “you can only specify up to four clustering columns.” Second, and more consequential in practice, “the order of clustered columns affects query performance.”
Order matters because clustering sorts by the first column, then by the second within each value of the first, and so on โ the way a phone book sorted by surname and then first name is efficient if you know the surname and nearly useless if you only know the first name. Google’s own example makes the rule concrete: with a table clustered by Order_Date, Country, Status, “a query that filters on Order_Date and Country is optimized for clustering, whereas a query that filters on only Country and Status is not optimized.” Put the column that queries filter on most often first.
Size decides whether clustering can help at all: “Unpartitioned tables larger than 64 MB are likely to benefit from clustering. Similarly, table partitions larger than 64 MB are also likely to benefit from clustering.” Below that threshold there are too few blocks for block elimination to remove anything worth removing.
Clustering is best understood next to partitioning rather than instead of it. Partitioning divides a table into separate units that can be skipped whole, which is partition pruning. Clustering sorts rows inside those units. They work on different granularities and compose well: partition by day and cluster by customer, and a query for one customer on one day reads a small part of one partition. Neither is column pruning, which reduces how many columns are read rather than how many rows.
The real trade-off is not speed but predictability. “When you query a clustered table, you don’t receive an accurate query cost estimate before query execution because the number of storage blocks to be scanned is not known before query execution.” The pre-execution estimate becomes an upper bound, and actual bytes billed are usually lower. That is a good deal for cost and an awkward one if you rely on a hard pre-execution limit, which may then reject a query that would have read far less than its estimate.
One related behavior follows from the same mechanism. On a non-clustered table, “applying a LIMIT clause to a query doesn’t affect the amount of data that is read,” so LIMIT is not a cost control. On a clustered table “a LIMIT clause can reduce the number of bytes scanned, because scanning stops when enough blocks are scanned to get the result.”
Clustering is not free to maintain, so it should be justified by the queries that actually run rather than adopted as a default. The honest test is whether the filters your workload really uses line up with the clustering columns, in the order you declared them.
Further reading
See the worked examples in BigQuery Architecture and Cost.
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
