forked from GoogleCloudPlatform/bigquery-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_performance_insights.sql
51 lines (48 loc) · 1.77 KB
/
query_performance_insights.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This script retrieves the top 100 queries that have had performance insights
* generated for them in the past 30 days.
* 30 days is the default timeframe, but you can change this by setting the
* num_days_to_scan variable to a different value.
*/
DECLARE num_days_to_scan INT64 DEFAULT 30;
CREATE SCHEMA IF NOT EXISTS optimization_workshop;
CREATE OR REPLACE TABLE optimization_workshop.query_performance_insights AS
SELECT
bqutil.fn.job_url(project_id || ':us.' || job_id) AS job_url,
query_info.performance_insights AS performance_insights
FROM
`region-us`.INFORMATION_SCHEMA.JOBS_BY_ORGANIZATION
WHERE
DATE(creation_time) >= CURRENT_DATE - num_days_to_scan
AND job_type = 'QUERY'
AND state = 'DONE'
AND error_result IS NULL
AND statement_type != 'SCRIPT'
AND EXISTS ( -- Only include queries which had performance insights
SELECT 1
FROM UNNEST(
query_info.performance_insights.stage_performance_standalone_insights
)
WHERE slot_contention OR insufficient_shuffle_quota
UNION ALL
SELECT 1
FROM UNNEST(
query_info.performance_insights.stage_performance_change_insights
)
WHERE input_data_change.records_read_diff_percentage IS NOT NULL
);