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

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.

The models, their grain and what each reads
ModelBuilt asGrainKeyReads
stg_ga4__eventsviewOne row per raw event rowevent_key, a hash of the user, time, name, bundle and parametersthe 92 daily tables
stg_ga4__itemsviewOne row per item per eventnonethe 92 daily tables
int_eventstable, partitioned by dayOne row per distinct eventevent_key, unique and not nullstg_ga4__events
int_sessionstableOne row per sessionsession_key, user and session id, uniqueint_events
fct_daily_traffictableOne row per day, source, medium, device and countrythose five columns, tested unique togetherint_events
fct_daily_item_salestableOne row per day and item categorythose two columns, tested unique togetherstg_ga4__items

The comparison layouts are copies of the staging rows, built only for this study:

The layouts
LayoutWhat it is
Staging view, as builtThe dbt staging view over the 92 daily tables, as a team would first build it.
Same view, one-line date filterThe same view, with the query's date window written as a filter on the table name.
Copied into a tableThe view copied into one table.
Table partitioned by dateThat table partitioned by event date.
Partitioned and clustered by event nameThat partitioned table clustered by event name.
Daily martDaily marts: traffic by source, medium, device and country, and item sales by category.
Sessions tableOne row per session, with its landing page.

The SQL

Every model and query is in the download. These show the harder parts:

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.

The queries
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:

The held-out queries
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

Predictions and scores
PredictionScore
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.