GoogleCloudPlatform / bigquery-utils

Useful scripts, udfs, views, and other utilities for migration and data warehouse operations in BigQuery.
https://cloud.google.com/bigquery/
Apache License 2.0
1.1k stars 276 forks source link

Per user cost in BigQuery #444

Open upgradedcloud opened 4 weeks ago

upgradedcloud commented 4 weeks ago

BigQuery per user cost ?

Hi all , is it possible to get the per user cost for bq in my project ?

saurabh-net commented 3 weeks ago

You could try getting a per-region and per-project estimate from the Information Schema https://cloud.google.com/bigquery/docs/information-schema-jobs

Here's a sample query for costs over the last 30 days:

SELECT
  user_email, 
  SUM(total_bytes_processed)/POWER(2,40) AS total_terabytes_processed, 
  SUM(total_bytes_processed)/POWER(2,40) * 5 AS estimated_cost -- Assuming $5 per TB
FROM 
  `<your-project>`.`region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT 
WHERE 
  creation_time BETWEEN TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) 
  AND CURRENT_TIMESTAMP()
  AND job_type = 'QUERY' 
  AND state = 'DONE'
GROUP BY
  user_email
ORDER BY
 estimated_cost DESC;