What cuts a BigQuery bill on a dbt model of web analytics events
Copying the staging view into a table cut bytes billed by 70% to 92% on five of the six ranked queries.
The ranking
Each change is scored by the share of bytes billed it saves on each of the six ranked queries, counted as zero where it does not apply, averaged over all six and divided by its work: 1 for a query edit, 2 for a config change, 3 for a new model.
| Change | Work | Traffic | Funnel | Revenue | Landing pages | Users | Return rate | Score |
|---|---|---|---|---|---|---|---|---|
| One-line date filter on the daily tables | a query edit | 74% | 94% | 87% | 0.424 | |||
| Copy the staging view into a table | a new model | 84% | 87% | 70% | 89% | 92% | 0.234 | |
| Partition that table by date | a config change | 73% | 93% | 86% | 0% | 0% | 0.211 | |
| Daily mart for the dashboard tiles | a new model | 85% | 94% | 94% | 0.152 | |||
| A sessions table for the landing-page query | a new model | 84% | 0.047 | |||||
| Cluster the partitioned table by event name | a config change | 5% | 7% | 0% | 0% | 0% | 0.010 | |
| Approximate distinct count | a query edit | 0% | 0.000 |
Each saving compares a layout with the one before it. Clustering is measured on top of the partitioned table, and partitioning on top of the table copy. A blank cell means the change does not apply to that query.
By mean saving alone, with no work scores, the order is: copy the staging view into a table, daily mart for the dashboard tiles, one-line date filter on the daily tables, partition that table by date, a sessions table for the landing-page query, cluster the partitioned table by event name, approximate distinct count. A change outranks the filter when its work counts for fewer query edits than the figure below. The table copy needs 1.66; it is scored 3.
| Change | Mean saving | Work | Outranks the filter below |
|---|---|---|---|
| Copy the staging view into a table | 70% | a new model | 1.66 query edits |
| Daily mart for the dashboard tiles | 46% | a new model | 1.07 query edits |
| One-line date filter on the daily tables | 42% | a query edit | |
| Partition that table by date | 42% | a config change | 0.99 query edits |
| A sessions table for the landing-page query | 14% | a new model | 0.33 query edits |
| Cluster the partitioned table by event name | 2% | a config change | 0.05 query edits |
| Approximate distinct count | 0% | a query edit | never |
Every query on every layout
Median of three runs with the cache off. Slot seconds are the compute BigQuery spent across its workers; elapsed seconds are the time from start to finish of the job.
Daily sessions and users by source and medium, last 30 days (dashboard tile)
| Layout | MiB billed | Slot seconds | Elapsed seconds | Rows |
|---|---|---|---|---|
| Staging view, as built | 1,525 | 8.23 | 3.10 | 30 |
| Same view, one-line date filter | 401 | 3.75 | 1.40 | 30 |
| Copied into a table | 247 | 2.16 | 0.38 | 30 |
| Table partitioned by date | 66 | 0.95 | 0.18 | 30 |
| Partitioned and clustered by event name | 63 | 0.78 | 0.24 | 30 |
| Daily mart | 10 | 0.17 | 0.18 | 30 |
Purchase funnel by device, last 7 days
| Layout | MiB billed | Slot seconds | Elapsed seconds | Rows |
|---|---|---|---|---|
| Staging view, as built | 1,560 | 7.45 | 2.57 | 3 |
| Same view, one-line date filter | 95 | 0.84 | 0.56 | 3 |
| Copied into a table | 206 | 0.90 | 0.23 | 3 |
| Table partitioned by date | 14 | 0.10 | 0.17 | 3 |
| Partitioned and clustered by event name | 13 | 0.11 | 0.18 | 3 |
Weekly revenue by item category, all 92 days (dashboard tile)
| Layout | MiB billed | Slot seconds | Elapsed seconds | Rows |
|---|---|---|---|---|
| Staging view, as built | 172 | 3.10 | 3.06 | 266 |
| Daily mart | 10 | 0.01 | 0.16 | 266 |
Top 20 landing pages with engagement rate, last 14 days
| Layout | MiB billed | Slot seconds | Elapsed seconds | Rows |
|---|---|---|---|---|
| Staging view, as built | 1,505 | 26.25 | 3.15 | 20 |
| Same view, one-line date filter | 198 | 7.44 | 1.30 | 20 |
| Copied into a table | 456 | 6.45 | 0.61 | 20 |
| Table partitioned by date | 62 | 1.40 | 0.39 | 20 |
| Partitioned and clustered by event name | 62 | 1.51 | 0.45 | 20 |
| Sessions table | 10 | 0.07 | 0.16 | 20 |
Distinct users by country and month, all 92 days (dashboard tile)
| Layout | MiB billed | Slot seconds | Elapsed seconds | Rows |
|---|---|---|---|---|
| Staging view, as built | 1,520 | 57.83 | 3.73 | 327 |
| Staging view, approximate count | 1,520 | 66.83 | 3.56 | 327 |
| Copied into a table | 166 | 6.04 | 1.96 | 327 |
| Table partitioned by date | 166 | 1.50 | 0.35 | 327 |
| Partitioned table, approximate count | 166 | 1.24 | 0.20 | 327 |
| Partitioned and clustered by event name | 166 | 1.23 | 0.23 | 327 |
| Daily mart | 10 | 0.58 | 0.38 | 327 |
Week-1 return rate by first-visit week, all 92 days
| Layout | MiB billed | Slot seconds | Elapsed seconds | Rows |
|---|---|---|---|---|
| Staging view, as built | 1,472 | 63.17 | 4.20 | 13 |
| Copied into a table | 118 | 8.99 | 1.10 | 13 |
| Table partitioned by date | 118 | 2.32 | 0.55 | 13 |
| Partitioned and clustered by event name | 118 | 2.35 | 0.68 | 13 |
100 recent rows, every column against eight named, last 7 days
| Layout | MiB billed | Slot seconds | Elapsed seconds | Rows |
|---|---|---|---|---|
| Raw export, every column | 206 | 0.07 | 0.49 | 100 |
| Raw export, eight named columns | 25 | 0.06 | 0.41 | 100 |
| Filtered view, every column | 108 | 0.11 | 0.46 | 100 |
| Filtered view, eight named columns | 100 | 0.07 | 0.79 | 100 |
| Partitioned table, every column | 65 | 0.15 | 0.17 | 100 |
| Partitioned table, eight named columns | 37 | 0.08 | 0.16 | 100 |
The last query asks for 100 rows with no order, so its rows differ by layout. It shows the cost of every column: on the raw export, eight named columns billed 25 MiB against 206 MiB for every column with the same limit of 100 rows.
Do the cheap versions give the same answer
On every layout from the view to the clustered table, each of the six ranked queries gave the same rows. The marts, the sessions table and the approximate count give near answers. Each was compared with the exact one, row by row.
| Version | Column | Largest gap | Median gap | Rows that differ | Exact total | Total |
|---|---|---|---|---|---|---|
| Traffic tile on the daily mart | users | 0.17% | 0.05% | 80% | 102,736 | 102,730 |
| Traffic tile on the daily mart | sessions | 0.00% | 0.00% | 0% | 114,225 | 114,225 |
| Revenue tile on the daily mart | revenue | 0.00% | 0.00% | 0% | 362,110 | 362,110 |
| Landing pages on the sessions table | sessions | 0.56% | 0.00% | 15% | 46,609 | 46,604 |
| Users by country and month, approximate count | users | 0.83% | 0.00% | 16% | 278,526 | 278,384 |
| Users by country and month on the daily mart | users | 10.53% | 0.94% | 67% | 278,526 | 275,127 |
The users mart counts users seen at a session start, and merges approximate daily counts. That is why its country-by-month counts drift most.
Why the staging view is so expensive
Dry runs report the bytes a query would process, without running it; the bill rounds that up and charges at least 10 MiB per query. A query on the view that names one column read as much as one that reads the nested parameters. The view makes eight parameter lookups on that column, and BigQuery keeps those lookups even when the query drops their result.
| Query | GiB |
|---|---|
| Staging view, country column only | 1.361 |
| Staging view, date column only | 1.354 |
| Raw export, country column only | 0.047 |
| Raw export, the nested parameters column only | 1.314 |
| Raw export through one unused parameter lookup, country only | 0.839 |
| Raw export through an unused hash of the parameters, country only | 0.047 |
Ranked by slot time instead
The same rule, scored on slot time instead of bytes. The filter still ranks first.
Partitioning ranks second partly on a gain the bytes do not explain. On the two queries that read all 92 days it cut slot time by 74% to 75%, with the same bytes billed on both layouts and run ranges that do not overlap. Without that gain, the table copy would rank second.
| Change | Work | Score on slot time | Rank on bytes | Rank on slot time |
|---|---|---|---|---|
| One-line date filter on the daily tables | a query edit | 0.358 | 1 | 1 |
| Partition that table by date | a config change | 0.310 | 3 | 2 |
| Copy the staging view into a table | a new model | 0.229 | 2 | 3 |
| Daily mart for the dashboard tiles | a new model | 0.135 | 4 | 4 |
| A sessions table for the landing-page query | a new model | 0.053 | 5 | 5 |
| Approximate distinct count | a query edit | 0.029 | 7 | 6 |
| Cluster the partitioned table by event name | a config change | 0.014 | 6 | 7 |
What a dashboard would cost
A three-tile dashboard: traffic, weekly revenue by category, and users by country and month, loaded 1,000 times a day, at US$5 to US$7.50 per TiB read.
- On the staging view: 92 TiB a month, US$460 to US$690, and 576 slot hours.
- With the filter: 60 TiB, US$299 to US$449.
- On the partitioned table: 11.6 TiB, US$58 to US$87.
- As recommended, two tiles on marts and users on the partitioned table: 5.3 TiB, US$27 to US$40, and 14 slot hours. Rebuilding the tables daily adds 0.08 TiB.
- All three tiles on marts: 0.86 TiB, but 1.2% of users missed.
What the model and its tests cost to build
Bytes billed to build each table once, and to run each dbt test once.
| Model | MiB billed |
|---|---|
| int_events | 1,767 |
| int_sessions | 668 |
| fct_daily_traffic | 330 |
| fct_daily_item_sales | 172 |
| bench_flat_table | 1,767 |
| bench_flat_part | 1,025 |
| bench_flat_part_clust | 1,025 |
| Test | Reads | Result | Failing rows | MiB billed |
|---|---|---|---|---|
| accepted_values_stg_ga4__events_device_category__desktop__mo | staging view | pass | 0 | 1,381 |
| assert_page_view_has_session_id | staging view | pass | 0 | 1,400 |
| assert_purchase_has_real_transaction_id | staging view | fail | 906 | 1,407 |
| dbt_utils_free_unique_combination_fct_daily_item_sales_event | table or mart | pass | 0 | 10 |
| dbt_utils_free_unique_combination_fct_daily_traffic_event_da | table or mart | pass | 0 | 10 |
| not_null_int_events_event_key | table or mart | pass | 0 | 140 |
| not_null_int_sessions_session_key | table or mart | pass | 0 | 11 |
| not_null_stg_ga4__events_event_date | staging view | pass | 0 | 1,387 |
| not_null_stg_ga4__events_event_key | staging view | pass | 0 | 1,550 |
| not_null_stg_ga4__events_event_name | staging view | pass | 0 | 1,400 |
| unique_int_events_event_key | table or mart | pass | 0 | 140 |
| unique_int_sessions_session_key | table or mart | pass | 0 | 11 |
| unique_stg_ga4__events_event_key | staging view | pass | 0 | 1,550 |
What went wrong or was missed
- The purchase test failed. 906 of 5,692 purchase events carry no real transaction id: 883 read "(not set)" and 23 have none. They carry US$31,817 of US$362,165 in revenue. The publisher says the obfuscated data holds placeholder values. A revenue model would need a rule for these rows.
- Three predictions missed. The table copy ranked second, not partitioning. The marts hit the 10 MiB minimum, so they saved less than predicted. The approximate count saved less slot time than predicted.
- The approximate count's slot time moved within run-to-run noise. It used 18% less than the exact count on the partitioned table and 16% more on the view, and the three runs' ranges overlap on both.
- Bytes on the clustered table moved between runs. The traffic tile's dry run read 68.9 million bytes before the runs, the same as the unclustered table, and 65.6 million in them. BigQuery sorts a new clustered table in the background, so the saving measured is what it had reached by then.
Limits
One small dataset, three runs per layout, and one region. The work scores are a judgement. The run records do not show why partitioning cut slot time on the queries that read all 92 days. The slot-time ranking and the dry runs on the view were added after the results. The table of where the ranking would change was added after them too, written down before it was computed.
Short queries are noisy. On the partitioned tables and marts they took 0.16 to 0.68 seconds, so slot time is the compute figure.
The dashboard costs rest on an assumed price, not read from a price list, and count every load as a full read. BigQuery's result cache and its free first TiB a month would lower them, so they are ceilings. They were added after the results.