In BigQuery, partitioning by integer means dividing a table into segments based on a specific integer column’s value ranges, using the PARTITION BY RANGE_BUCKET clause. This lets you query only the relevant ranges (e.g., customer IDs 1–1000) instead of scanning the entire table, reducing cost and improving performance.
What Is Integer Partitioning in BigQuery?
BigQuery offers three partitioning types: ingestion time (by _PARTITIONTIME), date/timestamp columns, and integer columns. Integer partitioning is designed for columns that contain whole numbers but not dates—like user IDs, order counts, or sensor readings. You define a starting value, an ending value, and an interval; BigQuery then creates partitions for each range. For example, PARTITION BY RANGE_BUCKET(customer_id, GENERATE_ARRAY(0, 100000, 1000)) creates 100 partitions, each covering 1,000 customer IDs.
How to Create an Integer-Partitioned Table
You can create an integer-partitioned table using DDL or by setting the partitioning option when loading data. The syntax requires the PARTITION BY clause with RANGE_BUCKET and a GENERATE_ARRAY for the boundaries.
Example DDL:
CREATE TABLE mydataset.customers (
customer_id INT64,
name STRING,
signup_date DATE
)
PARTITION BY RANGE_BUCKET(customer_id, GENERATE_ARRAY(0, 100000, 1000));
This creates partitions for ranges [0,1000), [1000,2000), …, [99000,100000). Any row with a customer_id outside these bounds (e.g., negative or ≥100000) goes into a special __UNPARTITIONED__ partition.
Choosing the Right Integer Column and Range
Pick a column that is frequently used in WHERE clauses and has a reasonably even distribution. Avoid columns with very high cardinality (millions of unique values) unless you use a large interval, because too many partitions can cause metadata overhead. The range should cover the expected data domain; you can alter the partitioning later, but it requires a rewrite.
- Start value: Should be ≤ the minimum value in your data to avoid unpartitioned rows.
- End value: Should be > the maximum value.
- Interval: Trade-off between granularity (more partitions = faster queries on small ranges) and management (too many partitions = slower metadata operations).
Querying an Integer-Partitioned Table
To benefit from partitioning, always filter on the integer column in your WHERE clause. BigQuery’s query planner can then prune partitions, scanning only the relevant ones. For example:
SELECT * FROM mydataset.customers
WHERE customer_id BETWEEN 5000 AND 6000;
This would only scan the partition covering [5000,6000). Without such a filter, the query scans all partitions—same cost as a non-partitioned table.
Common Pitfalls and Misconceptions
One common mistake is assuming integer partitioning works like clustering. Partitioning physically separates storage, while clustering sorts data within partitions. They are complementary: you can partition by integer and cluster by another column. Another misconception is that you can partition by an integer column that is also a timestamp—use date/timestamp partitioning instead. Also, note that you cannot use PARTITION BY with a column that has NULL values; NULLs go to the unpartitioned partition, which can cause unexpected full scans if you filter on that column without handling NULLs.
Integer Partitioning vs. Date Partitioning
Date partitioning (by DATE or TIMESTAMP) is more common and supports automatic partition expiration and time-based pruning. Integer partitioning is useful when your data has no natural date dimension but has a numeric key that queries filter on. For example, an IoT dataset with device IDs (integers) and no timestamp might benefit from integer partitioning. However, if you have a timestamp, prefer date partitioning because it integrates with time-based features like PARTITIONS filter and expiration.
Cost and Performance Implications
Partitioning reduces the amount of data scanned, which directly lowers query cost (BigQuery charges per byte processed). With integer partitioning, if your queries always filter on the integer column, you can achieve significant savings. However, if your queries rarely filter on that column, partitioning adds no benefit and may even slow down metadata operations. Also, each partition has a minimum storage cost, so too many tiny partitions can increase storage overhead slightly.
Alternatives: Clustering and Materialized Views
If you don’t need strict partition pruning, consider clustering on the integer column. Clustering sorts data and can also reduce bytes scanned, but it doesn’t create separate storage segments. Clustering is more flexible because you can cluster on multiple columns, and it works well with filter predicates. Another alternative is to use a materialized view that pre-aggregates by integer ranges, but that adds complexity. For most use cases, partitioning by integer is best when you have a clear numeric filter that is used in most queries.
Frequently Asked Questions
Can I change the range boundaries after creation?
No, you cannot alter the PARTITION BY clause of an existing table. You must create a new table with the desired boundaries and copy the data over, or use the CREATE OR REPLACE TABLE statement to redefine it (which drops the old table).
What happens to rows with NULL in the integer column?
Rows with NULL in the partitioning column go into the __UNPARTITIONED__ partition. Queries that filter on the column with a condition like WHERE customer_id IS NULL will scan that partition only, but if you filter on a range, NULLs are excluded.
Is integer partitioning supported for external tables?
No, partitioning by integer is only supported for native BigQuery tables. For external tables (e.g., Parquet in GCS), you can use ingestion-time partitioning or partition by a date/timestamp column, but not by an integer column.
How many partitions can I have?
BigQuery allows up to 4,000 partitions per table. If your range and interval produce more than that, you’ll get an error. Plan your interval so the number of partitions stays under this limit.
Does integer partitioning work with wildcard tables?
No, integer partitioning is not available for wildcard tables. Wildcard tables use table name patterns and only support ingestion-time partitioning.
Related
- View Sent Emails in QuickBooks: Full Guide
- Trade Balance: How It’s Calculated and Read
- How Much Does Coding Cost? Real Prices for 2025
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Goal Seek in Excel Browser: Limits and WorkaroundsNo: Excel for the web does not currently include the built-in Goal Seek command. To use Goal Seek…
- How to Find Rank in Excel with RANK.EQTo rank a value in Excel, use RANK.EQ. With scores in C2:C6 and the first score in C2,…
- Split in SQL: Practical Methods for Common DatabasesTo split a delimited string in SQL, use a function supported by your database: for example, PostgreSQL’s string_to_array,…
- Google Sheets Apps Script If Statements: How They WorkIn Google Sheets Apps Script, an if statement runs code only when a condition is true; use else…
- How to Unhide All Rows in an Excel Spreadsheet QuicklyTo unhide every row in an Excel worksheet, select the entire sheet using the Select All button at…
- Google Sheets Filter by Color: Steps and LimitationsYes. In Google Sheets in a web browser, create a filter, open the filter menu on the column…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.