Request a demo

Glossary · Cost and FinOps

What is cost attribution?

Cost attribution assigns data platform spend to whoever or whatever caused it: a project, a user or service account, a label, a pipeline, a table or a data product. On BigQuery it combines the Cloud Billing export with job and storage metadata from INFORMATION_SCHEMA.

Also called: cost allocation, BigQuery cost allocation, BigQuery cost by user, BigQuery cost by label, cost per pipeline, cost per team

Why BigQuery costs are hard to attribute

Cloud Billing reports cost per project, service and SKU. That is enough when every team has its own project and runs on-demand. It breaks down quickly:

  • Shared projects. One project often hosts dbt, Airflow, a BI tool and several analysts, and the bill shows a single number.
  • Reservations. With editions, compute is billed to the reservation's administration project and query projects show $0 compute. Billing data adds an informational Analysis Slots Attribution line with slot-hours per project. An opt-in mode in Preview, billed cost attribution, moves the charges to the query projects by slot usage; unused baseline and autoscaler overhead stay with the administration project, and every project must use the same billing account.
  • Labels only go so far. Job labels reach billing data, but not for reservation compute. Dataset labels appear in storage billing data only, table labels don't appear at all, and tables don't inherit dataset labels.
  • Storage is billed to the project that holds the dataset, no matter who reads it.

Attribution keys, from coarse to fine

  1. Project: free and always present, but coarse.
  2. User or service account (user_email in JOBS): service accounts usually map to one tool such as dbt, Airflow, an ingestion service or Looker, and people map to teams through your directory.
  3. Job labels: set with bq query --label, the labels field of the job configuration (for example in Airflow's BigQueryInsertJobOperator), or SET @@query_label = "team:growth" inside a session. In dbt, model labels label the table; add query-comment with job-label: true in dbt_project.yml to label the jobs.
  4. Table and pipeline: build cost comes from jobs whose destination_table is the table, read cost from jobs that list it in referenced_tables. Group tables into data products for a per-product view.

Example: compute by pipeline, with a fallback

Most organizations have unlabeled jobs, so attribute by label first and fall back to the account that ran the job:

SELECT
  project_id,
  COALESCE(
    (SELECT value FROM UNNEST(labels) WHERE key = 'pipeline'),
    CONCAT('unlabeled: ', user_email)) AS cost_owner,
  COUNT(*) AS jobs,
  ROUND(SUM(total_slot_ms) / 3600000, 1) AS slot_hours,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4), 2) AS tib_billed
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_ORGANIZATION
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND (statement_type != 'SCRIPT' OR statement_type IS NULL)
GROUP BY project_id, cost_owner
ORDER BY slot_hours DESC
LIMIT 50;

The OR statement_type IS NULL keeps load and copy jobs, which have no statement type. For on-demand projects, tib_billed times your price per TiB is the cost (BigQuery pricing); the pricing calculator prices the same totals on each edition. For reservations, convert slot-hours with a rate, or split the reservation's monthly charge by slot share as described in showback and chargeback.

Pitfalls

  • Double counting scripts. The parent row of a multi-statement script repeats its children's totals.
  • Hidden bytes. For queries on tables with row-level access policies, total_bytes_billed is empty in the JOBS views.
  • Readers outside the organization. JOBS_BY_ORGANIZATION lists only jobs from projects in your organization, even when outside projects read your shared datasets.
  • Short history. JOBS keeps 180 days; copy it to a table on a schedule for year-over-year views.
  • Label drift. Label keys and values accept only lowercase letters, digits, underscores and dashes. Agree on a short, fixed list of keys and check that new pipelines use them.

Masthead attributes BigQuery compute and storage to projects, users, pipelines and data products from logs and metadata, with no label mapping needed (how it works).

FAQ

How do I see BigQuery cost by user?

Group INFORMATION_SCHEMA.JOBS, or JOBS_BY_ORGANIZATION for every project, by user_email. Sum total_bytes_billed for on-demand jobs and total_slot_ms for jobs that ran in a reservation, and exclude statement_type = 'SCRIPT' so multi-statement scripts are not counted twice.

Do BigQuery job labels show up in the billing export?

Yes, labels on a job are included in billing data, which works for on-demand queries. Under capacity pricing, compute is billed per reservation to the administration project, so job labels can't split it in Cloud Billing reports; use slot usage from INFORMATION_SCHEMA instead.

Who pays when another project queries my dataset?

The project that runs the query pays for the compute; the data owner is not charged when their data is read. The project that holds the dataset keeps paying for its storage.

Read next

Browse the full glossary

Cut BigQuery spend without guessing

Masthead attributes compute and storage cost to every project, user, pipeline and table, finds unused and dead-end tables, and shows where editions or on-demand pricing is cheaper. Mnemonic cut its BigQuery compute cost by 20% within 24 hours of onboarding.