What cuts a BigQuery bill on a dbt model of web analytics events
Storing Google Analytics events as a table split by day, not a dbt view, cut a date-filtered dashboard query's data by a further 84%. Filter first, table second.
Data billed per run of the 30-day traffic question, in GB, a billion bytes.
Real public data: Google Analytics events from Google's merchandise store, 4,295,584 events over 92 days, Nov 2020 to Jan 2021.
BigQuery bills a company for every byte its dashboards read. I tried seven fixes on six everyday dashboard questions over Google's public sample of its store's web events. The data model was built with dbt, a tool that turns SQL queries into tables. It started with a view, as dbt's guide advises: a saved query that re-reads all the raw events on every question. So: the date filter first, then the table split by day, then summary tables. A dashboard loaded 1,000 times a day would read 94% less data a month.
What to do
Before any run, I scored each fix's work (1 a query edit, 2 a setting, 3 a new table) and ranked fixes by data saved per point.
1. Add the date filter to every dated question. One line on the daily tables' names cut the three dated questions by 74% to 94%.
2. Store the events as a table split by day. The view pulled each dashboard field out of a nested list of event details with a small lookup:
(select ep.value.string_value
from unnest(event_params) as ep
where ep.key = 'page_location' limit 1)BigQuery read the whole list even for questions that used none of the fields: one plain field read 1.46 GB through the view, 0.05 GB from the raw tables. As a table, five questions were billed 70% to 92% less.
3. Put the traffic and revenue tiles on daily summary tables. Each was billed 0.01 GB, BigQuery's minimum. Keep users by country and month on the table: its summary counts only users who started a session, 1.2% short in total and 10.5% in the worst cell.
Leave sorting by event for much bigger tables, and skip approximate counts. Sorting saved under 8% here. Approximate counts save no data billed, and no compute beyond run-to-run noise.
The six dashboard questions: traffic by channel, the purchase funnel, weekly revenue by category, top landing pages, users by country and month, and first-week return by cohort.
How the data model is built
- Raw daily tables: Google's export, one row per event, one table per day.
- The staging view of events: one row per raw event, keyed by a hash of its fields. Tests: key present and unique, date and event name present, device type valid. An items view has one row per item.
- The events table: the same rows with duplicates removed. Tests: key present and unique.
- The sessions table: one row per session, keyed by user and session id. Tests: key present and unique.
- The daily traffic summary: one row per day, source, medium, device and country. Test: those are unique together.
- The daily sales summary: one row per day and item category, tested the same way.
A row test, that every purchase has a real order id, flagged 906 of 5,692 purchases with no usable order id, 883 of them "(not set)". They hold 9% of the revenue, and counting orders by id would fold the 883 into one.
What it is worth
Take a dashboard with three tiles, loaded 1,000 times a day. The filter alone cuts its monthly data by 35%. On the table, each question's date range already skips other days, so the filter is no longer needed. Steps 2 and 3 cut it by 94%: about 101,000 GB on the view against 5,900 GB. At an assumed US$5 to US$7.50 per 1,100 GB, BigQuery's billing unit, that saves up to US$650 a month.
How I would put this to work with a team
- The analytics engineer, first: add the filter to the heaviest dated questions.
- Then one model change: build the events table split by day and move the 13 dbt tests onto it. They read 3.5 times the daily build, 97% of it through the view.
- The dashboard owner, next: move a tile to a summary table only if every cell stays within 1% of the table's.
The three numbers that matter
- 74%: one line of date filter, on the 30-day traffic question.
- 1.46 GB against 0.05 GB: one field through the view's lookup, and from the raw tables.
- 94%: steps 2 and 3, on a three-tile dashboard's month.
What would change the conclusion
- If the filter would save under half of a dated question's data, build the table first.
- If sorting by event saves 25% or more on questions that pick events by name, add it. The test: a month of GH Archive's bigger public event tables, billed with and without sorting by event type, kept under 100 GB.
- If you pay for compute, not data, the order holds: the filter cut compute by 54% to 89%.
Limits
One small dataset: 92 days, 3.6 GB in all. Sorting by event may pay more on bigger data.
The filter's cut follows the date range: the 30-day question kept 26% of the data for 33% of the days. Held-out questions, three per fix, gave 80% (filter) and 86% (table): a check of the arithmetic, not new ground.
The work scores are a judgement: if building the table counted as under 1.7 times the work of the filter, the table would rank first.
The price is assumed, and the saving is a ceiling: BigQuery's result cache and free first 1,100 GB a month would lower the real bill.
The under-half rule assumes this mix, half of it dated questions.
Google altered the sample, so the 906 purchases without an order id may come from the store or from Google. A study of the same sample's funnel found the same gap.
One model is untested: the items view.
Added after the results: the costs, BigQuery's cost estimates before a query runs, the summary-table checks, the compute ranking and the 1% bar. Three of ten predictions missed (results page).