BigQuery cost query · Where the money goes
BigQuery cost by user and service account
This query adds up bytes billed, an on-demand cost estimate and slot-hours per user and service account over the last 30 days. It shows whether the bill comes from people running ad hoc queries or from pipelines running under service accounts.
The query
SELECT
user_email,
ENDS_WITH(user_email, '.gserviceaccount.com') AS is_service_account,
COUNT(*) AS queries,
ROUND(SUM(total_bytes_billed) / POW(1024, 4), 2) AS tib_billed,
ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd,
ROUND(SUM(total_slot_ms) / 3600000, 1) AS slot_hours
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
GROUP BY user_email
ORDER BY est_on_demand_usd DESC;How it works
user_email is the principal that ran the job: a person's account for console and notebook work, a service account for pipelines, BI tools and scheduled jobs. Grouping by it is the quickest cost attribution there is, and it needs no labels.
Service accounts usually dominate. When one does, the next step is to find out which pipeline or tool uses it: group its jobs by label, by dbt model or by query hash with the other queries on this page.
Output columns
| Column | Meaning |
|---|---|
is_service_account | TRUE for service accounts (pipelines, BI tools, schedulers), FALSE for people. |
queries | Query jobs run by the principal in the window. |
est_on_demand_usd | On-demand list price of the bytes billed. |
slot_hours | Slot time used, the cost driver under editions. |
Permissions and notes
Needs: bigquery.jobs.listAll on the project, for example through the BigQuery Resource Viewer role (roles/bigquery.resourceViewer). To see only your own jobs, query JOBS_BY_USER instead.
- The
6.25is the on-demand list price per TiB in the US and EU multi-regions. If your projects run on editions, cost follows slot-hours instead: multiplyslot_hoursby your edition's price per slot-hour (see BigQuery pricing). statement_type != 'SCRIPT'drops the parent row of multi-statement queries. The parent repeats the bytes and slots of its child statements, so keeping it would count them twice.- Jobs run through a service account on behalf of a person (for example by a BI tool with its own credentials) show the service account, not the person. Labels or the tool's own logs are needed to go further.
Variations
People vs service accounts, per week
SELECT
DATE_TRUNC(DATE(creation_time), WEEK(MONDAY)) AS week,
ENDS_WITH(user_email, '.gserviceaccount.com') AS is_service_account,
ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS est_on_demand_usd
FROM `region-us`.INFORMATION_SCHEMA.JOBS
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 90 DAY)
AND job_type = 'QUERY'
AND statement_type != 'SCRIPT'
GROUP BY week, is_service_account
ORDER BY week, is_service_account;FAQ
Can I set a spending limit per user?
Yes, with custom quotas: a daily limit on query bytes per user or per project, set in the Google Cloud console under Quotas. A user who hits it gets a quota error until the next day.
How do I find who ran a specific expensive query?
Filter the same view on job_id, or sort by total_bytes_billed and read user_email on the top rows, as in the most expensive queries.
Related
All BigQuery cost queries · Data FinOps playbook · Pricing calculator
