What cuts a BigQuery bill on a dbt model of web analytics events
Six queries ran on five layouts of one dbt model, three times each with the cache off, and four held-out queries tested the winner.
The data
Real public data: Google Analytics events from Google's merchandise store, 4,295,584 events over 92 days, Nov 2020 to Jan 2021. Google publishes it so analysts can practise on a real GA4 export. It keeps the export's shape: one table per day, and the event parameters nested in a repeated column.
The dbt model
Staging views flatten the parameters dashboards use, such as the session id, the page and the source. An intermediate table removes duplicate events and is partitioned by date. A sessions table holds one row per session. Two daily marts serve the dashboard tiles; the traffic mart keeps an approximate sketch of users so days can be merged. dbt tests check keys, accepted values, a session id on every page view and a real transaction id on every purchase.
| Model | Built as | Grain | Key | Reads |
|---|---|---|---|---|
stg_ga4__events | view | One row per raw event row | event_key, a hash of the user, time, name, bundle and parameters | the 92 daily tables |
stg_ga4__items | view | One row per item per event | none | the 92 daily tables |
int_events | table, partitioned by day | One row per distinct event | event_key, unique and not null | stg_ga4__events |
int_sessions | table | One row per session | session_key, user and session id, unique | int_events |
fct_daily_traffic | table | One row per day, source, medium, device and country | those five columns, tested unique together | int_events |
fct_daily_item_sales | table | One row per day and item category | those two columns, tested unique together | stg_ga4__items |
The comparison layouts are copies of the staging rows, built only for this study:
| Layout | What it is |
|---|---|
| Staging view, as built | The dbt staging view over the 92 daily tables, as a team would first build it. |
| Same view, one-line date filter | The same view, with the query's date window written as a filter on the table name. |
| Copied into a table | The view copied into one table. |
| Table partitioned by date | That table partitioned by event date. |
| Partitioned and clustered by event name | That partitioned table clustered by event name. |
| Daily mart | Daily marts: traffic by source, medium, device and country, and item sales by category. |
| Sessions table | One row per session, with its landing page. |
The SQL
Every model and query is in the download. These show the harder parts:
dbt/models/intermediate/int_events.sql: removes duplicate events with a window function and QUALIFY.dbt/models/marts/fct_daily_traffic.sql: keeps users as HLL sketches, so days merge into distinct counts.dbt/macros/ga4_param.sql: the parameter lookup that made every query on the view read the nested column.queries/dev/Q4_top_landing_pages.sql: builds sessions inside the query, with landing page and engagement.queries/dev/Q6_week1_return_by_cohort.sql: first-visit cohorts and week-1 return with window functions.queries/dev/Q1_traffic_by_channel.sql: the dashboard tile, with the date window the layouts are filled into.dbt/tests/assert_purchase_has_real_transaction_id.sql: the test that found the placeholder transaction ids.
The queries
Six queries a dashboard or an analyst runs, plus one that compares every column with eight named ones. Each date window ends on the data's last day, so every run reads the same rows.
| Query |
|---|
| Daily sessions and users by source and medium, last 30 days (dashboard tile) |
| Purchase funnel by device, last 7 days |
| Weekly revenue by item category, all 92 days (dashboard tile) |
| Top 20 landing pages with engagement rate, last 14 days |
| Distinct users by country and month, all 92 days (dashboard tile) |
| Week-1 return rate by first-visit week, all 92 days |
| 100 recent rows, every column against eight named, last 7 days |
Four held-out queries, written and frozen with the others before any run:
| Query |
|---|
| New against returning users by week, all 92 days |
| Revenue per session by landing page, last 30 days |
| Add-to-cart rate by item category, last 21 days |
| Daily engaged sessions by country, last 7 days |
Measures and runs
Every query was dry-run first. Each real run had the cache off. Each run recorded bytes processed, bytes billed, slot time, elapsed time and whether the cache served it. Each layout ran three times, in a random order set by a fixed seed, 108 runs on the first queries and 48 on the held-out ones. A hash of the sorted rows checked that every layout gave the same answer.
The decision rule
Written before any run. A change's saving on a query is one minus its bytes billed over the bytes of the layout before it. Its score is the mean saving over the six ranked queries, zero where it does not apply, divided by its work: 1 for a query edit, 2 for a config change, 3 for a new model. A change pays little when its median saving, where it applies, is under 10%. The two best are then applied to the held-out queries. The results page shows where the ranking would change under other work scores.
Predictions, written before any run, and their scores
| Prediction | Score |
|---|---|
| Copying the view into a table bills at most 40% of the view's bytes. | held |
| Partitioning cuts bytes in proportion to the date window, and saves nothing on whole-period queries. | held |
| Clustering by event name saves under 10% in the median, and under 30% on the event-filtered queries. | held |
| The marts bill at most 5% of the bytes of the query they replace, and cut slot time by 80% or more. | missed |
| An approximate distinct count bills the same bytes and cuts slot time by at least 20%. | missed |
| A limit of 100 rows does not make every column cheap: eight named columns bill at most 25% as much. | held |
| The date filter skips days even through the staging view. | held |
| The date filter ranks first and partitioning second. | missed |
| The filter's saving holds on the held-out queries, at half its first median or more. | held |
| At least one dbt test fails on the public data. | held |
Glossary
- Bytes billed
- What BigQuery's on-demand pricing charges for: the bytes of the columns a query reads, rounded up, with a minimum of 10 MiB per query.
- MiB, GiB, TiB
- Units of 2 to the power 20, 30 and 40 bytes. BigQuery prices per TiB.
- Slot time
- The compute a query used, summed over BigQuery's workers. Teams that buy capacity pay for this.
- Elapsed time
- From the start of a query job to its end.
- Dry run
- BigQuery reports the bytes a query would read, without running it or billing it.
- Daily tables
- The GA4 export writes one table per day. A filter on the table name skips whole days.
- View
- A saved query. It stores nothing, and every query on it runs it again.
- Partition
- A table split by date, so a query that filters on the date reads only those days.
- Cluster
- A table sorted by a column within each partition, so a filter on that column can skip blocks.
- Mart
- A small table pre-aggregated for a dashboard, here one row per day and channel.
- Approximate count
- A distinct count from a sketch that trades a small error for less work.
- dbt
- A tool that builds warehouse tables from SQL files and tests them.
How to rebuild it
The download holds the dbt project, every query, the tools and the results. The README lists the commands. A BigQuery project and a service account are needed; the free sandbox is enough.
Limits
One public dataset, one region and three runs per layout. The work scores, 1 to 3, are a judgement made before the runs. Prices are assumed. The data's publisher says it holds placeholder values and is only partly consistent. Incremental models are not used: the free sandbox runs no inserts or merges.