Skip to content

Diogo TravassosData leadership · Analytics · Decision-making

Real public data

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.

Changes ranked by saving for the work
ChangeWorkTrafficFunnelRevenueLanding pagesUsersReturn rateScore
One-line date filter on the daily tablesa query edit74%94%87%0.424
Copy the staging view into a tablea new model84%87%70%89%92%0.234
Partition that table by datea config change73%93%86%0%0%0.211
Daily mart for the dashboard tilesa new model85%94%94%0.152
A sessions table for the landing-page querya new model84%0.047
Cluster the partitioned table by event namea config change5%7%0%0%0%0.010
Approximate distinct counta query edit0%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.

Where the ranking would change
ChangeMean savingWorkOutranks the filter below
Copy the staging view into a table70%a new model1.66 query edits
Daily mart for the dashboard tiles46%a new model1.07 query edits
One-line date filter on the daily tables42%a query edit
Partition that table by date42%a config change0.99 query edits
A sessions table for the landing-page query14%a new model0.33 query edits
Cluster the partitioned table by event name2%a config change0.05 query edits
Approximate distinct count0%a query editnever

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)

Daily sessions and users by source and medium, last 30 days (dashboard tile)
LayoutMiB billedSlot secondsElapsed secondsRows
Staging view, as built1,5258.233.1030
Same view, one-line date filter4013.751.4030
Copied into a table2472.160.3830
Table partitioned by date660.950.1830
Partitioned and clustered by event name630.780.2430
Daily mart100.170.1830

Purchase funnel by device, last 7 days

Purchase funnel by device, last 7 days
LayoutMiB billedSlot secondsElapsed secondsRows
Staging view, as built1,5607.452.573
Same view, one-line date filter950.840.563
Copied into a table2060.900.233
Table partitioned by date140.100.173
Partitioned and clustered by event name130.110.183

Weekly revenue by item category, all 92 days (dashboard tile)

Weekly revenue by item category, all 92 days (dashboard tile)
LayoutMiB billedSlot secondsElapsed secondsRows
Staging view, as built1723.103.06266
Daily mart100.010.16266

Top 20 landing pages with engagement rate, last 14 days

Top 20 landing pages with engagement rate, last 14 days
LayoutMiB billedSlot secondsElapsed secondsRows
Staging view, as built1,50526.253.1520
Same view, one-line date filter1987.441.3020
Copied into a table4566.450.6120
Table partitioned by date621.400.3920
Partitioned and clustered by event name621.510.4520
Sessions table100.070.1620

Distinct users by country and month, all 92 days (dashboard tile)

Distinct users by country and month, all 92 days (dashboard tile)
LayoutMiB billedSlot secondsElapsed secondsRows
Staging view, as built1,52057.833.73327
Staging view, approximate count1,52066.833.56327
Copied into a table1666.041.96327
Table partitioned by date1661.500.35327
Partitioned table, approximate count1661.240.20327
Partitioned and clustered by event name1661.230.23327
Daily mart100.580.38327

Week-1 return rate by first-visit week, all 92 days

Week-1 return rate by first-visit week, all 92 days
LayoutMiB billedSlot secondsElapsed secondsRows
Staging view, as built1,47263.174.2013
Copied into a table1188.991.1013
Table partitioned by date1182.320.5513
Partitioned and clustered by event name1182.350.6813

100 recent rows, every column against eight named, last 7 days

100 recent rows, every column against eight named, last 7 days
LayoutMiB billedSlot secondsElapsed secondsRows
Raw export, every column2060.070.49100
Raw export, eight named columns250.060.41100
Filtered view, every column1080.110.46100
Filtered view, eight named columns1000.070.79100
Partitioned table, every column650.150.17100
Partitioned table, eight named columns370.080.16100

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.

Differences from the exact answer
VersionColumnLargest gapMedian gapRows that differExact totalTotal
Traffic tile on the daily martusers0.17%0.05%80%102,736102,730
Traffic tile on the daily martsessions0.00%0.00%0%114,225114,225
Revenue tile on the daily martrevenue0.00%0.00%0%362,110362,110
Landing pages on the sessions tablesessions0.56%0.00%15%46,60946,604
Users by country and month, approximate countusers0.83%0.00%16%278,526278,384
Users by country and month on the daily martusers10.53%0.94%67%278,526275,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.

Dry-run bytes for one-column queries
QueryGiB
Staging view, country column only1.361
Staging view, date column only1.354
Raw export, country column only0.047
Raw export, the nested parameters column only1.314
Raw export through one unused parameter lookup, country only0.839
Raw export through an unused hash of the parameters, country only0.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.

Changes ranked by slot time saved for the work
ChangeWorkScore on slot timeRank on bytesRank on slot time
One-line date filter on the daily tablesa query edit0.35811
Partition that table by datea config change0.31032
Copy the staging view into a tablea new model0.22923
Daily mart for the dashboard tilesa new model0.13544
A sessions table for the landing-page querya new model0.05355
Approximate distinct counta query edit0.02976
Cluster the partitioned table by event namea config change0.01467

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.

What the model and its tests cost to build

Bytes billed to build each table once, and to run each dbt test once.

Bytes billed per build
ModelMiB billed
int_events1,767
int_sessions668
fct_daily_traffic330
fct_daily_item_sales172
bench_flat_table1,767
bench_flat_part1,025
bench_flat_part_clust1,025
Bytes billed per test
TestReadsResultFailing rowsMiB billed
accepted_values_stg_ga4__events_device_category__desktop__mostaging viewpass01,381
assert_page_view_has_session_idstaging viewpass01,400
assert_purchase_has_real_transaction_idstaging viewfail9061,407
dbt_utils_free_unique_combination_fct_daily_item_sales_eventtable or martpass010
dbt_utils_free_unique_combination_fct_daily_traffic_event_datable or martpass010
not_null_int_events_event_keytable or martpass0140
not_null_int_sessions_session_keytable or martpass011
not_null_stg_ga4__events_event_datestaging viewpass01,387
not_null_stg_ga4__events_event_keystaging viewpass01,550
not_null_stg_ga4__events_event_namestaging viewpass01,400
unique_int_events_event_keytable or martpass0140
unique_int_sessions_session_keytable or martpass011
unique_stg_ga4__events_event_keystaging viewpass01,550

What went wrong or was missed

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.