Last active
November 4, 2021 12:45
-
-
Save paslandau/980f251cbbb2f0dddff9da6425007e3f to your computer and use it in GitHub Desktop.
Monitor Query Costs in BigQuery via INFORMATION_SCHEMA views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Monitor Query costs in BigQuery; standard-sql; 2020-06-21 | |
# @see http://www.pascallandau.com/bigquery-snippets/monitor-query-costs/ | |
DECLARE timezone STRING DEFAULT "Europe/Berlin"; | |
DECLARE gb_divisor INT64 DEFAULT 1024*1024*1024; | |
DECLARE tb_divisor INT64 DEFAULT gb_divisor*1024; | |
DECLARE cost_per_tb_in_dollar INT64 DEFAULT 5; | |
DECLARE cost_factor FLOAT64 DEFAULT cost_per_tb_in_dollar / tb_divisor; | |
SELECT | |
DATE(creation_time, timezone) creation_date, | |
FORMAT_TIMESTAMP("%F %H:%I:%S", creation_time, timezone) as query_time, | |
job_id, | |
ROUND(total_bytes_processed / gb_divisor,2) as bytes_processed_in_gb, | |
IF(cache_hit != true, ROUND(total_bytes_processed * cost_factor,4), 0) as cost_in_dollar, | |
project_id, | |
user_email, | |
FROM | |
`region-us`.INFORMATION_SCHEMA.JOBS_BY_USER | |
WHERE | |
DATE(creation_time) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY) and CURRENT_DATE() | |
ORDER BY | |
bytes_processed_in_gb DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: BigQuery: Monitor Query Costs via INFORMATION_SCHEMA