Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Billed Bytes on BigQuery: Billing Page is different from Audit Logging and INFORMATION_SCHEMA

I'm interested in knowing exactly how much each of my queries are spending on BigQuery. The problem is that the sum of billed bytes for the queries DO NOT add up to the bytes billed in the Billing page. The sum is ~10% less than I see on the Billing Page.

As suggested in the official BigQuery Monitoring page, I tried:

Audit Log: Creating a sink with Cloud Logs and after that summing the totalBilledBytes for each query collected by the logs.

Screenshot_1_1_x350.png

INFORMATION_SCHEMA view: Querying the `INFORMATION_SCHEMA.JOBS_BY_PROJECT` dataset for the total_billed_bytes:

 

DECLARE timezone STRING DEFAULT "America/Sao_Paulo";
DECLARE gb_divisor INT64 DEFAULT CAST(POWER(2, 30) AS INT64);
DECLARE tb_divisor INT64 DEFAULT CAST(POWER(2, 40) AS INT64);
DECLARE cost_per_tb_in_dollar INT64 DEFAULT 5;

WITH q AS (
SELECT
DATE(creation_time, timezone) creation_date,
FORMAT_TIMESTAMP("%F %H:%I:%S", creation_time, timezone) as query_time,
job_id,
total_bytes_processed / gb_divisor AS bytes_processed_in_gb,
(total_bytes_processed / tb_divisor)*cost_per_tb_in_dollar AS cost_in_dollar,
project_id,
user_email,
FROM
`region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE
DATE(creation_time, timezone) = "2023-06-02"
AND project_id = <<my_project>>
ORDER BY
bytes_processed_in_gb DESC
)

SELECT SUM(q.bytes_processed_in_gb)
FROM q

 

But each of these solutions gave me an answer **~10% smaller** from the total BigQuery Analysis I see on Billing (below).

x_1_x400.png

Am I doing something wrong in the calculations? Any idea of what's missing?

7 1 5,643
1 REPLY 1

Good day @andre-queiroz,

Welcome to Google Cloud Community!

total_bytes_processed is different from total_bytes_billed, I suggest you use total_bytes_billed in your query if you want to know about the jobs total billed, total_bytes_processed is used if you want to know about the jobs total bytes processed. Please note that it is only applicable if you are using on-demand pricing. For flat-rate pricing it is informational only, you can check this link for more information: https://cloud.google.com/bigquery/docs/information-schema-jobs#estimate_slot_usage_and_cost_for_quer...
You should also include WHERE statement_type != "SCRIPT" in your query to exclude the parent job and to avoid double counting. You can check this link fore more information: https://cloud.google.com/bigquery/docs/information-schema-jobs#estimate_slot_usage_and_cost_for_quer...

You can also check this sample code in the  documentation, total bytes billed for query jobs per user: https://cloud.google.com/bigquery/docs/information-schema-jobs#bytes_processed_per_user_identity

Hope this helps!