Skip to content
SQL

BigQuery SQL Best Practices for Analysts: How to Reduce Query Cost

On-demand BigQuery queries are billed by the bytes they read. These practices, each checked against Google's documentation, show how to read fewer bytes and how to see the estimate before you run a query.

Eric ProvencioFounder · Analytics Engineering
10 min read
On this page · 21 sections
  1. How BigQuery bills a query
  2. BigQuery practices and what each one saves
  3. Select only the columns you need
  4. What LIMIT does and does not do
  5. Partition large tables and filter on the partitioning column
  6. Write filters that prune
  7. Require a partition filter
  8. Cluster tables on the columns you filter by
  9. Estimate the cost before you run a query
  10. Cap the bytes billed for a query
  11. Use approximate aggregation when an estimate is enough
  12. Save results you query repeatedly
  13. Join and filter advice from Google's optimization pages
  14. Applying these practices to dbt models on BigQuery
  15. Common mistakes
  16. Frequently Asked Questions
  17. How can query costs be minimized in Google BigQuery?
  18. Does LIMIT reduce the cost of a BigQuery query?
  19. How do partitioning and clustering make BigQuery queries more efficient?
  20. What are the best practices for structuring JOIN operations in BigQuery?
  21. Primary sources and further reading

BigQuery SQL best practices come down to making each query read less data. Under on-demand pricing, Google bills a query by the bytes it processes, so to reduce BigQuery query cost you select only the columns you need, filter on the partitioning column of partitioned tables, cluster large tables on the columns you filter by, and check the bytes estimate before you run anything. Each practice below cites the Google documentation page behind it.

All SQL here is BigQuery (GoogleSQL) syntax taken from Google's documentation on November 22, 2026. None of it was run. For related guides, see the BigQuery hub.

How BigQuery bills a query

BigQuery has two pricing models for queries.

  • On-demand pricing. Queries are billed by the bytes read, per TiB (tebibyte) of logical, meaning uncompressed, data. Fewer bytes read means a lower charge.
  • Capacity pricing. You pay for the compute resources, called slots, that process queries, set up through reservations. Google states that capacity costs follow the slot capacity provisioned over time, not the data a specific query scans, and that the exact cost of one query cannot be estimated before it runs.

This article quotes no rates: they vary by location and change, so check Google's pricing page. Queries answered from cached results are not charged.

BigQuery practices and what each one saves

PracticeWhat it savesGoogle documentation page
Select only the columns you needBytes in the columns you leave outOptimize query computation
Filter on the partitioning columnBytes in the pruned partitionsQuery partitioned tables
Require a partition filterUnfiltered scans of the whole tableCreate partitioned tables
Filter on clustered columnsBytes in the pruned storage blocksIntroduction to clustered tables
Dry run the query firstNothing directly. It shows the bytes firstRun a query
Set maximum bytes billedOversized queries fail without a chargeEstimate and control costs
Use approximate aggregate functionsTime and resources, for an estimateBest practices for functions
Use materialized views or staged tablesRepeated reads of large tablesIntroduction to materialized views
Aggregate before joiningRows the join has to processOptimize query computation

Select only the columns you need

BigQuery stores table data in columnar format: each column is stored separately, so a query can read one column without reading every field of every row. On-demand queries are charged for the data in the columns you select, so SELECT * reads, and bills, every column.

Name the columns you want. SELECT * EXCEPT leaves out the columns you list, which Google says can greatly reduce the data read.

-- BigQuery (GoogleSQL). Adapted from Google's documentation; not run.
SELECT transaction_id, transaction_amount
FROM mydataset.transactions
WHERE transaction_date >= '2026-01-01';

SELECT * EXCEPT (customer_id)
FROM mydataset.transactions
WHERE transaction_date >= '2026-01-01';

To look at a table, use the console's Preview tab or the bq head command, which show rows at no charge. For external tables stored as Parquet or ORC, the bytes charged are likewise limited to the columns read.

What LIMIT does and does not do

LIMIT caps the rows a query returns. On a table that is not clustered, it does not change the data read: Google states that you are billed for all the bytes the query reads, even though only some rows come back. On a clustered table, LIMIT can reduce the bytes scanned, because scanning stops once enough blocks have been read. Use it to limit output, not cost.

Partition large tables and filter on the partitioning column

A partitioned table is divided into segments, called partitions, by one partitioning column: a date, timestamp, or datetime column, the ingestion time, or an integer range. When a query filters on that column, BigQuery scans the matching partitions and skips the rest. This is partition pruning, and pruned partitions are not counted in the bytes scanned.

This table is partitioned by day, clustered by customer, and rejects queries that have no partition filter:

-- BigQuery (GoogleSQL). Built from Google's documented CREATE TABLE examples; not run.
CREATE TABLE mydataset.transactions
(
  transaction_id INT64,
  customer_id STRING,
  transaction_date DATE,
  transaction_amount NUMERIC
)
PARTITION BY transaction_date
CLUSTER BY customer_id
OPTIONS (
  require_partition_filter = TRUE
);

Write filters that prune

Pruning needs a filter BigQuery can evaluate without reading table data:

-- BigQuery (GoogleSQL). WHERE clauses adapted from Google's documentation; not run.

-- Prunes: compared with constants
WHERE transaction_date >= '2026-01-01' AND transaction_date < '2026-02-01'

-- Still prunes: a second condition joined by AND
WHERE customer_id = 'C1001' AND transaction_date >= '2026-01-01'

-- Does not prune: a subquery value is not constant
WHERE transaction_date = (SELECT MAX(transaction_date) FROM mydataset.staging_transactions)

-- Does not prune: with OR, any partition can still qualify
WHERE transaction_date >= '2026-01-01' OR customer_id = 'C1001'

Keep the partitioning column alone on one side of the comparison. A few functions, including DATE_TRUNC, DATE_ADD, and DATE_SUB, still allow pruning when their other arguments are constant. Other functions on the column cause a full table scan.

Require a partition filter

With require_partition_filter = TRUE, a query without a qualifying filter fails with an error. At least one condition must reference only the partitioning column, so the OR filter above is rejected. The rule also covers views and materialized views on the table.

To choose a partitioning column, see partitioning strategies in Snowflake and BigQuery.

Cluster tables on the columns you filter by

A clustered table sorts its data into storage blocks by the values of up to four clustered columns. A query that filters or aggregates on those columns scans only the relevant blocks, which Google calls block pruning.

Google calls clustering generally a good first option and says tables or partitions larger than 64 MB are likely to benefit, especially when queries filter on columns with many distinct values. Filter on the first clustered column, alone or with the ones after it. Wrapping a clustered column in a function, or comparing it with another column, prevents block pruning.

The trade-off is predictability: the blocks to scan are not known until the query runs, so the estimate is an upper bound. Google suggests partitioning as well when you need a strict estimate beforehand, or when partitions would average at least 10 GB.

Estimate the cost before you run a query

BigQuery reports the bytes a query will process before you run it. In the console's query editor, the query validator shows the estimate for any valid query. A dry run returns an estimate from the command line or the API, and Google states that dry runs are not charged.

# bq command-line tool. Adapted from Google's documentation; not run.
bq query \
--use_legacy_sql=false \
--dry_run \
'SELECT transaction_id, transaction_amount
 FROM mydataset.transactions
 WHERE transaction_date >= "2026-01-01"'

The estimate is an upper bound, and it shows whether a partition filter pruned.

Cap the bytes billed for a query

The maximum bytes billed setting puts a ceiling on one on-demand query. If the estimate is over the limit, the query fails without a charge. Set it in the console's query settings, or replace --dry_run above with a flag such as --maximum_bytes_billed=1000000000. A query on a clustered table can fail this check even when the bytes billed would have been under the limit. Custom quotas set a daily cap for a project or a user.

Use approximate aggregation when an estimate is enough

APPROX_COUNT_DISTINCT(customer_id) estimates COUNT(DISTINCT customer_id), and APPROX_QUANTILES returns approximate quantile boundaries. Google's reference states the trade-off: these functions typically use fewer resources and perform better on very large inputs, but they return a statistical estimate whose precision you cannot set. Google describes the gain as speed and resource use and does not claim fewer bytes billed. Keep exact counts for figures that must reconcile, such as finance reports.

Save results you query repeatedly

  • Materialized views. A materialized view is a precomputed view whose stored results BigQuery refreshes in the background when the base tables change. BigQuery can also reroute a query on the base table to the view, which Google calls smart tuning. You still pay to query, refresh, and store it, and the SQL allowed in one is restricted.
  • Staged tables. Google recommends breaking a large multi-stage query into stages that each write to a destination table, then querying the smaller table.
  • Not common table expressions. Google warns that a WITH clause is for readability, not performance. A common table expression (CTE) referenced several times may be evaluated each time.

Join and filter advice from Google's optimization pages

  • Aggregate before you join. Reduce the rows with GROUP BY first, so the join processes less data.
  • Put the largest table first, then the smallest, then the rest by decreasing size. A large table followed by a small one gives a broadcast join, which sends the small table to every slot processing the large one.
  • Join on INT64 keys where you can. Google says they compare faster and cost less than STRING keys.
  • Replace self-joins with a window function or the PIVOT operator, and avoid joins that output more rows than they take in.
  • Filter on simple types. Operations on BOOL, INT64, FLOAT64, and DATE columns are typically faster than on STRING or BYTES columns.

For advice that applies to any database, see query optimization basics, and practice in the SQL hub. For table design, see data warehouse schema design.

Applying these practices to dbt models on BigQuery

In dbt, models create the tables, so partitioning and clustering are set in each model's configuration. The dbt documentation for BigQuery names the settings: partition_by, a dictionary with field, data_type, and granularity; cluster_by, one column name or a list; and require_partition_filter, which also affects other models and tests that select from the model.

-- dbt model configuration for BigQuery, as named in the dbt documentation; not run.
{{ config(
    materialized = 'table',
    partition_by = {
      "field": "transaction_date",
      "data_type": "date",
      "granularity": "day"
    },
    cluster_by = "customer_id",
    require_partition_filter = true
) }}

dbt's page repeats the rule that pruning needs literal values, not a subquery. For incremental models, which transform only new or changed rows on each run, dbt lists three strategies on BigQuery: merge (the default), insert_overwrite, and microbatch. It documents that merge scans the destination table as well as the source tables, and that insert_overwrite replaces whole partitions and requires partition_by. The dbt platform's BigQuery connection also has an optional Maximum bytes billed field, and dbt queries over it fail.

Common mistakes

  • Using SELECT * with LIMIT to look at a table. On a table that is not clustered it reads the whole table.
  • Filtering a partitioned table with a subquery value or with OR. Neither prunes.
  • Wrapping a partitioning or clustered column in a function. Most functions stop pruning.
  • Expecting byte savings to lower a capacity bill directly. Capacity pricing charges for slots over time.

Frequently Asked Questions

How can query costs be minimized in Google BigQuery?

Under on-demand pricing, read fewer bytes. Select only the columns you need, filter on the partitioning column, and cluster large tables on the columns you filter by. Check the estimate with a dry run, and set maximum bytes billed so an oversized query fails before it is charged.

Does LIMIT reduce the cost of a BigQuery query?

Not on a table that is not clustered. Google's documentation says you are billed for all the bytes the query reads, even though only some rows are returned. On a clustered table, LIMIT can reduce the bytes scanned.

How do partitioning and clustering make BigQuery queries more efficient?

Partitioning divides a table into segments by one column, and a filter on that column lets BigQuery skip whole partitions. Clustering sorts data into blocks by up to four columns, and a filter on those columns lets BigQuery skip blocks. A table can use both.

What are the best practices for structuring JOIN operations in BigQuery?

Google's guidance is to aggregate before the join, place the largest table first, join on INT64 keys instead of STRING keys where possible, and replace self-joins with window functions.

Primary sources and further reading

These pages were read on November 22, 2026. No statement in this article was run. Prices and product behavior change.