— Create temp table to persist query log data
CREATE TABLE `project-id.dataset.sample_query_log`
OPTIONS(
expiration_timestamp=TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 1 DAY)
) AS
SELECT concat(‘IDWWM’, ‘~~’,
ifnull(user_email,’NA’), ‘~~’,
‘app_u’, ‘~~’,
‘client_u’, ‘~~’,
ifnull(start_time,’9999-01-01 00:00:00′), ‘~~’,
round(ifnull(AmpIO,0)), ‘~~’,
ifnull(total_bytes_processed,0), ‘~~’,
ifnull(creation_time, ‘9999-01-01 00:00:00’), ‘~~’,
ifnull(start_time,’9999-01-01 00:00:00′), ‘~~’,
ifnull(start_time,’9999-01-01 00:00:00′), ‘~~’,
0, ‘~~’,
CASE WHEN TBl1.Query_ID IS NULL THEN jb.job_id ELSE TBl1.Query_ID END, ‘~~’,
(max(ifnull(AmpIO,0)) OVER(
ORDER BY NULL)), ‘~~’,
(max(ifnull(total_bytes_processed,0)) OVER (ORDER BY NULL )) , ‘~~’,
(SUM(ifnull(AmpIO,0)) OVER (ORDER BY NULL)), ‘~~’,
(SUM(ifnull(total_bytes_processed,0)) OVER (ORDER BY NULL )), ‘~~’,
IFNULL(total_slot_ms, 0) / 1000, ‘~~’,
CASE WHEN LEFT(TBl1.dataset_id,7) = ‘_script’ THEN ‘Not Present’ ELSE IFNULL(TBl1.dataset_id, ‘Not Present’) END, ‘~~’,
0, ‘~~’,
COALESCE (statement_type,”), ‘~~’,
”, ‘~~’,
”, ‘~~’,
COALESCE (query,”), ‘~~’,
COALESCE (job_type,’NA’), ‘~~’,
COALESCE (TBl1.tables_used, ”), ‘~~’,
ifnull(priority,”), ‘~~’,
COALESCE (dml_statistics.inserted_row_count,0 )+ COALESCE (dml_statistics.deleted_row_count,0) + COALESCE (dml_statistics.updated_row_count, 0)) as query_log
FROM `region-us`.INFORMATION_SCHEMA.JOBS AS JB
LEFT JOIN (
SELECT DISTINCT JOB_ID, query_info.query_hashes.normalized_literals AS query_id, r.dataset_id, string_agg(DISTINCT r.table_id ORDER BY r.table_id) AS tables_used
FROM `region-us`.INFORMATION_SCHEMA.JOBS
LEFT JOIN UNNEST (referenced_tables) AS r
GROUP BY 1, 2,3
) AS TBl1
ON (JB.job_id = TBl1.JOB_ID)
LEFT JOIN (SELECT job_id, round(sum(j.compute_ratio_avg) + sum(j.compute_ratio_max) + sum(j.compute_ms_avg) + sum(j.compute_ms_max)
+ sum(read_ratio_avg) + sum(read_ms_avg) + sum(read_ratio_max)
+ sum(read_ms_max) + sum(write_ratio_avg) + sum(write_ms_avg) + sum(write_ratio_max) + sum(write_ms_max)) AS AmpIO
FROM `region-us`.INFORMATION_SCHEMA.JOBS, unnest(job_stages) AS j
GROUP BY job_id) AS TB12
ON (JB.JOB_ID = TB12.JOB_ID)
WHERE error_result.message IS NULL AND state = ‘DONE’
AND CAST(start_time AS date) > ‘2024-01-01’ AND CAST(start_time AS date) < ‘2024-05-31’
;
— Export Data from temp table
EXPORT DATA OPTIONS(
uri=‘gs://path/to/gcs/*.gzip’,
format=’CSV’,
overwrite=true,
header=false,
compression=GZIP,
field_delimiter=’~’) AS
select * from `project-id.dataset.sample_query_log` ;