# BigQuery bytes scanned: how partition pruning and column choice set the cost of a query

A BigQuery on-demand query is billed for the bytes it reads, not the rows it returns. Pick a date window, switch partition pruning on and off, choose how many columns you select and watch the bytes, the cost per run and the monthly bill of an hourly dashboard change.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;BigQuery bytes scanned: partition pruning and column selection&#34;,&#34;sub&#34;:&#34;A daily-partitioned events table, 28 days. Choose the date window, whether the WHERE clause can prune partitions and how much of each row you select, and see the bytes billed and what a dashboard refreshing all day costs.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;d_from&#34;,&#34;label&#34;:&#34;Filter from day&#34;,&#34;min&#34;:1,&#34;max&#34;:28,&#34;step&#34;:1,&#34;value&#34;:22,&#34;note&#34;:&#34;First day in the WHERE clause&#34;},{&#34;id&#34;:&#34;d_to&#34;,&#34;label&#34;:&#34;Filter to day&#34;,&#34;min&#34;:1,&#34;max&#34;:28,&#34;step&#34;:1,&#34;value&#34;:28,&#34;note&#34;:&#34;Last day in the WHERE clause&#34;},{&#34;id&#34;:&#34;prune&#34;,&#34;label&#34;:&#34;Filter prunes partitions (1 = yes, 0 = no)&#34;,&#34;min&#34;:0,&#34;max&#34;:1,&#34;step&#34;:1,&#34;value&#34;:1,&#34;note&#34;:&#34;0 = the filter wraps the date in an unsupported expression or uses OR with another column, so every partition is read&#34;},{&#34;id&#34;:&#34;cols&#34;,&#34;label&#34;:&#34;Share of row bytes in the selected columns&#34;,&#34;min&#34;:1,&#34;max&#34;:100,&#34;step&#34;:1,&#34;value&#34;:15,&#34;unit&#34;:&#34;%&#34;,&#34;note&#34;:&#34;100 = SELECT *&#34;},{&#34;id&#34;:&#34;runs&#34;,&#34;label&#34;:&#34;Runs per day&#34;,&#34;min&#34;:1,&#34;max&#34;:96,&#34;step&#34;:1,&#34;value&#34;:24,&#34;note&#34;:&#34;24 = an hourly dashboard refresh&#34;},{&#34;id&#34;:&#34;price&#34;,&#34;label&#34;:&#34;On-demand price per TiB&#34;,&#34;min&#34;:5,&#34;max&#34;:8,&#34;step&#34;:0.25,&#34;value&#34;:6.25,&#34;unit&#34;:&#34;$&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;day&#34;,&#34;size_gib&#34;],&#34;rows&#34;:[[1,46],[2,48.5],[3,50.2],[4,49.5],[5,46],[6,33.9],[7,32.2],[8,47.6],[9,50.2],[10,51.9],[11,51.2],[12,47.6],[13,35.1],[14,33.3],[15,49.2],[16,51.9],[17,53.7],[18,52.9],[19,49.1],[20,36.3],[21,34.4],[22,50.8],[23,53.6],[24,55.4],[25,54.6],[26,50.7],[27,37.4],[28,35.5]],&#34;note&#34;:&#34;Illustrative sizes for 28 daily partitions of a web events table (day 1 is a Monday): weekday peaks, weekend dips and 0.5% growth a day. Replace size_gib with total_logical_bytes / 2^30 from INFORMATION_SCHEMA.PARTITIONS for your own table.&#34;},&#34;pre&#34;:[{&#34;id&#34;:&#34;full&#34;,&#34;label&#34;:&#34;Whole table (GiB)&#34;,&#34;expr&#34;:&#34;sum(size_gib)&#34;,&#34;tex&#34;:&#34;S = \\sum_d s_d&#34;},{&#34;id&#34;:&#34;avg_day&#34;,&#34;label&#34;:&#34;Average partition (GiB)&#34;,&#34;expr&#34;:&#34;mean(size_gib)&#34;,&#34;hide&#34;:true}],&#34;row&#34;:[{&#34;id&#34;:&#34;in_win&#34;,&#34;label&#34;:&#34;in window&#34;,&#34;expr&#34;:&#34;ge(day, d_from)*ge(d_to, day)&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;tex&#34;:&#34;w_d = [\\,d_{from} \\le d \\le d_{to}\\,]&#34;},{&#34;id&#34;:&#34;read&#34;,&#34;label&#34;:&#34;partition read&#34;,&#34;expr&#34;:&#34;ifelse(gt(prune, 0.5), in_win, 1)&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;tex&#34;:&#34;r_d = \\begin{cases} w_d \u0026 \\text{pruning works} \\\\ 1 \u0026 \\text{otherwise}\\end{cases}&#34;},{&#34;id&#34;:&#34;scan&#34;,&#34;label&#34;:&#34;GiB scanned&#34;,&#34;expr&#34;:&#34;size_gib*read*cols/100&#34;,&#34;tex&#34;:&#34;b_d = s_d \\cdot r_d \\cdot c&#34;}],&#34;derived&#34;:[{&#34;id&#34;:&#34;parts_read&#34;,&#34;label&#34;:&#34;Partitions read&#34;,&#34;expr&#34;:&#34;sum(read)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;pruned&#34;,&#34;label&#34;:&#34;Partitions skipped&#34;,&#34;expr&#34;:&#34;n - sum(read)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;window_gib&#34;,&#34;label&#34;:&#34;Data inside the date window (GiB)&#34;,&#34;expr&#34;:&#34;dot(size_gib, in_win)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;scanned&#34;,&#34;label&#34;:&#34;GiB processed per run&#34;,&#34;expr&#34;:&#34;sum(scan)&#34;,&#34;tex&#34;:&#34;B = \\sum_d b_d&#34;},{&#34;id&#34;:&#34;billed&#34;,&#34;label&#34;:&#34;GiB billed per run&#34;,&#34;expr&#34;:&#34;max(scanned, 10/1024)&#34;,&#34;tex&#34;:&#34;B_{billed} = \\max(B,\\ 10\\text{ MB})&#34;,&#34;note&#34;:&#34;rounded up to the MB, at least 10 MB per table and per query&#34;},{&#34;id&#34;:&#34;cost_run&#34;,&#34;label&#34;:&#34;Cost per run ($)&#34;,&#34;expr&#34;:&#34;billed/1024*price&#34;,&#34;tex&#34;:&#34;\\text{cost} = \\frac{B_{billed}}{1024}\\times p_{TiB}&#34;},{&#34;id&#34;:&#34;cost_star&#34;,&#34;label&#34;:&#34;Cost of SELECT * with no pruning ($)&#34;,&#34;expr&#34;:&#34;full/1024*price&#34;},{&#34;id&#34;:&#34;saving&#34;,&#34;label&#34;:&#34;Bytes saved against SELECT * full scan&#34;,&#34;expr&#34;:&#34;1 - scanned/full&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;1 - B/S&#34;},{&#34;id&#34;:&#34;monthly_tib&#34;,&#34;label&#34;:&#34;TiB processed in 30 days&#34;,&#34;expr&#34;:&#34;scanned*runs*30/1024&#34;,&#34;tex&#34;:&#34;\\text{TiB}_{30} = \\frac{B \\cdot \\text{runs} \\cdot 30}{1024}&#34;},{&#34;id&#34;:&#34;monthly&#34;,&#34;label&#34;:&#34;Cost over 30 days ($)&#34;,&#34;expr&#34;:&#34;billed*runs*30/1024*price&#34;,&#34;note&#34;:&#34;before the free first 1 TiB a month&#34;}],&#34;chart&#34;:{&#34;title&#34;:&#34;Cost per run against the number of days in the filter&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;t&#34;,&#34;label&#34;:&#34;Days in the date filter&#34;,&#34;from&#34;:1,&#34;to&#34;:28},&#34;series&#34;:[{&#34;label&#34;:&#34;Pruned: only those days are read ($)&#34;,&#34;expr&#34;:&#34;avg_day*t*cols/100/1024*price&#34;},{&#34;label&#34;:&#34;Not pruned: every partition is read ($)&#34;,&#34;expr&#34;:&#34;full*cols/100/1024*price&#34;},{&#34;label&#34;:&#34;SELECT * and no pruning ($)&#34;,&#34;expr&#34;:&#34;full/1024*price&#34;,&#34;dash&#34;:true}],&#34;mark&#34;:&#34;parts_read&#34;},&#34;level&#34;:&#34;beginner&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;GiB scanned in each daily partition&#34;,&#34;y&#34;:&#34;scan&#34;,&#34;label&#34;:&#34;day&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Know the table&#34;,&#34;text&#34;:&#34;The table holds {full} GiB in 28 daily partitions. A query that reads all of it, every column, would cost ${cost_star} at ${price} per TiB.&#34;,&#34;show&#34;:[&#34;full&#34;,&#34;cost_star&#34;]},{&#34;title&#34;:&#34;Prune partitions&#34;,&#34;text&#34;:&#34;The WHERE clause keeps days {d_from} to {d_to}. When the filter compares the partitioning column with constants, BigQuery reads {parts_read} partitions and skips {pruned}.&#34;,&#34;show&#34;:[&#34;parts_read&#34;,&#34;pruned&#34;]},{&#34;title&#34;:&#34;Read only the selected columns&#34;,&#34;text&#34;:&#34;Storage is columnar, so only the selected columns are read: {cols}% of each row&#39;s bytes. LIMIT would not reduce this.&#34;},{&#34;title&#34;:&#34;Add up the bytes&#34;,&#34;text&#34;:&#34;The run processes {scanned} GiB and is billed for {billed} GiB, {saving:p} less than SELECT * over the whole table.&#34;,&#34;show&#34;:[&#34;scanned&#34;,&#34;billed&#34;,&#34;saving&#34;]},{&#34;title&#34;:&#34;Price one run&#34;,&#34;text&#34;:&#34;{billed} GiB is {billed}/1024 TiB; at ${price} per TiB one run costs ${cost_run}.&#34;,&#34;show&#34;:[&#34;cost_run&#34;]},{&#34;title&#34;:&#34;Multiply by the schedule&#34;,&#34;text&#34;:&#34;At {runs} runs a day the dashboard processes {monthly_tib} TiB in 30 days, about ${monthly} before the free first TiB.&#34;,&#34;show&#34;:[&#34;monthly_tib&#34;,&#34;monthly&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

The table in the simulation is a web events table split into 28 daily partitions, one per day. Each row of the data grid is one partition and its size in GiB. The sizes are illustrative but shaped like real traffic: busy weekdays, quiet weekends and slow growth, about 1,289 GiB in total.

A dashboard runs the same query every hour. The query asks for the last seven days and reads a handful of columns. In BigQuery on-demand pricing you pay for the bytes the query processes, so two things decide the bill: which partitions are read and which columns are read.

The controls let you change the date window, whether the filter actually prunes partitions, the share of each row's bytes that sits in the columns you select (100% is `SELECT *`), how often the query runs and the price per TiB. The bars show how many GiB each partition contributes. The chart shows the cost of one run against the number of days in the filter, with and without pruning, and the vertical line marks how many partitions the current query reads.

Think of the table as a library with one shelf per day and one drawer per column. Pruning means you walk only to the shelves for the days you asked about. Column selection means you open only the drawers you need. You pay for every drawer you open, however few lines you copy out.

## The algorithm

1. **Find the partitions to read.** A partition for day d is inside the window when d from ≤ d ≤ d to. If the WHERE clause compares the partitioning column with constants, BigQuery skips every other partition. If it does not (a subquery, an unsupported function, an `OR` with another column), every partition is read.
2. **Read only the selected columns.** BigQuery stores each column separately, so the bytes read from a partition are its size times the share of bytes in the selected columns. A `LIMIT` does not change this for a non-clustered table.
3. **Add up the bytes.** The bytes processed are the sum over the partitions that are read.
4. **Apply the billing rules.** Charges are rounded up to the nearest MB, with a minimum of 10 MB per table referenced and per query.
5. **Price it.** Cost per run is the billed bytes in TiB times the on-demand price, $6.25 per TiB, after the first free TiB each month.
6. **Multiply by the schedule.** A query that runs 24 times a day runs 720 times in 30 days.

**Worked example (the default settings).** The window is days 22 to 28, so 7 partitions are read and 21 are skipped. Those 7 days hold 338.0 GiB. The query selects 15% of each row's bytes, so it processes 338.0 × 0.15 = 50.7 GiB. At $6.25 per TiB one run costs 50.7 / 1024 × 6.25 = $0.31. Running every hour, the dashboard processes 50.7 × 24 × 30 / 1024 = 35.65 TiB in 30 days, which is $222.80 before the free TiB.

Now set pruning to 0. The same seven-day query reads all 28 partitions: 1,288.7 × 0.15 = 193.3 GiB, $1.18 per run and $849.48 a month, almost four times as much for the same answer. A `SELECT *` over the whole table with no pruning reads 1,288.7 GiB and costs $7.87 per run.

## Things to try

- Set pruning to 0 and leave everything else alone. The cost per run jumps from $0.31 to $1.18, because a filter that BigQuery cannot use for pruning reads every partition.
- Set the share of row bytes to 100% (`SELECT *`) with pruning on. The seven days now cost $2.06 per run: column choice matters as much as the date filter.
- Narrow the window to a single day (from 28 to 28). The run processes 5.3 GiB and costs about $0.03, and the 10 MB minimum is still far away.
- Raise runs per day from 24 to 96 (a refresh every 15 minutes). The monthly bill grows four times, to $891.21, while each run costs the same.
- Paste the real partition sizes of one of your tables from `INFORMATION_SCHEMA.PARTITIONS` (total_logical_bytes divided by 2^30) and check what your own dashboard query costs.

## How it works in real systems

BigQuery estimates bytes before running a query: a dry run, or the estimate in the console, shows the bytes processed. Google notes that this estimate is an upper bound and can be higher than the bytes actually billed after the query runs. The `maximum bytes billed` setting makes a query fail without a charge when its estimate is above a limit, and the table option `require_partition_filter` rejects queries that have no usable filter on the partitioning column. Both are cheap protection against a dashboard that suddenly reads the whole table.

Pruning only works when the filter can be decided before the scan: compare the partitioning column with a constant expression, keep the column on its own side of the comparison, and combine other conditions with `AND`, not `OR`. Logical bytes per column depend on the data type, so long text columns are usually the expensive ones. For workloads that run all day, capacity pricing (slots) can be cheaper than paying per byte; the comparison is the same idea as [sizing a Snowflake warehouse](/ai-simulations/snowflake-warehouse-sizing). For a longer explanation of partitioning and clustering, read [BigQuery bytes billed](/articles/bigquery-bytes-billed-partitioning-clustering), and see the [BigQuery tool page](/tools/bigquery). Partition design upstream is part of the [medallion pipeline](/ai-simulations/medallion-pipeline-quality) too.

## Sources

- [BigQuery pricing (on-demand compute, free tier, rounding and minimums)](https://cloud.google.com/bigquery/pricing)
- [Query partitioned tables (pruning rules and require partition filter)](https://docs.cloud.google.com/bigquery/docs/querying-partitioned-tables)
- [Best practices for controlling costs (LIMIT, dry runs, maximum bytes billed)](https://docs.cloud.google.com/bigquery/docs/best-practices-costs)
- [INFORMATION_SCHEMA.PARTITIONS view](https://docs.cloud.google.com/bigquery/docs/information-schema-partitions)
