forked from GoogleCloudPlatform/bigquery-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_concurrency_comparison_slow.sql
More file actions
222 lines (219 loc) · 6.4 KB
/
Copy pathjob_concurrency_comparison_slow.sql
File metadata and controls
222 lines (219 loc) · 6.4 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright 2021 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.
*/
/*
* It is assumed that the following query will be run in the administration
* project where the reservations were created. If this is not the case,
* prepend the project id to the table name as follows:
* `{project_id}`.`region-{region_name}`.INFORMATION_SCHEMA.{table}
*/
/*
* Job Comparison Report: Returns information about jobs to compare performance
* and troubleshoot.
*/
WITH
job_info AS (
SELECT
creation_time,
end_time,
project_id,
reservation_id
FROM
-- PUBLIC DASHBOARD USE ONLY
-- Modify this to use your project's INFORMATION_SCHEMA table as follows:
-- `region-{region_name}`.INFORMATION_SCHEMA.{table}
`region-us`.INFORMATION_SCHEMA.JOBS_BY_ORGANIZATION
WHERE
-- Update `@job_param`, depending on if this is for the slow or fast job.
-- When creating the side-by-side comparison view, you will need to duplicate
-- this data source and update parameter to @job_param_2, or similar.
job_id = @job_param),
organization_capacity AS (
SELECT
SUM(slot_capacity) AS org_capacity
FROM (
SELECT
slot_capacity AS slot_capacity,
reservation_name,
change_timestamp,
action,
-- Find the most recent reservation update grouped by reservation
DENSE_RANK() OVER (PARTITION BY reservation_name ORDER BY change_timestamp DESC) AS rownum
FROM
-- PUBLIC DASHBOARD USE ONLY
-- Modify this to use your project's INFORMATION_SCHEMA table as follows:
-- `region-{region_name}`.INFORMATION_SCHEMA.{table}
`region-us`.INFORMATION_SCHEMA.RESERVATION_CHANGES_BY_PROJECT
WHERE
change_timestamp <= (
SELECT
creation_time
FROM
job_info) ) reservation_cap
WHERE
reservation_cap.rownum = 1
-- Remove deleted reservations, so that only active reservations are displayed
AND action != "DELETE" ),
reservation_capacity AS (
SELECT
slot_capacity AS reservation_capacity,
creation_time,
end_time,
job_info.project_id,
job_info.reservation_id
FROM (
SELECT
slot_capacity,
CONCAT(project_id, ":", "US", ".", reservation_name) AS reservation_id,
DENSE_RANK() OVER (ORDER BY change_timestamp DESC) AS rownum
FROM
-- PUBLIC DASHBOARD USE ONLY
-- Modify this to use your project's INFORMATION_SCHEMA table as follows:
-- `region-{region_name}`.INFORMATION_SCHEMA.{table}
`region-us`.INFORMATION_SCHEMA.RESERVATION_CHANGES_BY_PROJECT
WHERE
change_timestamp <= (
SELECT
creation_time
FROM
job_info)
AND CONCAT(project_id, ":", "US", ".", reservation_name) = (
SELECT
reservation_id
FROM
job_info)) reservation_cap
JOIN
job_info
ON
reservation_cap.reservation_id = job_info.reservation_id
WHERE
reservation_cap.rownum = 1 ),
joined_capacity AS (SELECT
*
FROM
reservation_capacity
CROSS JOIN
organization_capacity )
SELECT
DATETIME_TRUNC(DATETIME(period_start),
MINUTE) period_start,
COUNT(DISTINCT
IF
(state = 'RUNNING',
timeline_view.project_id,
NULL)) AS running_projects,
timeline_view.reservation_id,
job_type,
"minute" AS unit,
reservation_capacity,
org_capacity,
COUNT(DISTINCT
IF
(state = 'RUNNING'
AND timeline_view.project_id = joined_capacity.project_id,
timeline_view.job_id,
NULL)) AS running_jobs,
COUNT(DISTINCT
IF
(state = 'PENDING',
timeline_view.project_id,
NULL)) AS pending_projects,
COUNT(DISTINCT
IF
(state = 'PENDING'
AND timeline_view.project_id = joined_capacity.project_id,
timeline_view.job_id,
NULL)) AS pending_jobs,
SUM(period_slot_ms) / (1000 * 60) AS reservation_slots_used,
SUM(
IF
(timeline_view.project_id = joined_capacity.project_id,
(period_slot_ms),
0)) / (1000 * 60) AS project_slots_used,
SUM(period_slot_ms) / (1000 * 60) / reservation_capacity AS reservation_utilization,
FROM
-- PUBLIC DASHBOARD USE ONLY
-- Modify this to use your project's INFORMATION_SCHEMA table as follows:
-- `region-{region_name}`.INFORMATION_SCHEMA.{table}
`region-us`.INFORMATION_SCHEMA.JOBS_TIMELINE_BY_ORGANIZATION timeline_view
JOIN
joined_capacity
ON
timeline_view.reservation_id = joined_capacity.reservation_id
AND period_start BETWEEN joined_capacity.creation_time
AND joined_capacity.end_time
GROUP BY
1,
3,
4,
5,
6,
7
UNION ALL
SELECT
DATETIME_TRUNC(DATETIME(period_start),
HOUR) period_start,
COUNT(DISTINCT
IF
(state = 'RUNNING',
timeline_view.project_id,
NULL)) AS running_projects,
timeline_view.reservation_id,
job_type,
"hour" AS unit,
reservation_capacity,
org_capacity,
COUNT(DISTINCT
IF
(state = 'RUNNING'
AND timeline_view.project_id = joined_capacity.project_id,
timeline_view.job_id,
NULL)) AS running_jobs,
COUNT(DISTINCT
IF
(state = 'PENDING',
timeline_view.project_id,
NULL)) AS pending_projects,
COUNT(DISTINCT
IF
(state = 'PENDING'
AND timeline_view.project_id = joined_capacity.project_id,
timeline_view.job_id,
NULL)) AS pending_jobs,
SUM(period_slot_ms) / (1000 * 60 * 60) AS reservation_slots_used,
SUM(
IF
(timeline_view.project_id = joined_capacity.project_id,
(period_slot_ms),
0)) / (1000 * 60 * 60) AS project_slots_used,
SUM(period_slot_ms) / (1000 * 60 * 60) / reservation_capacity AS reservation_utilization,
FROM
-- PUBLIC DASHBOARD USE ONLY
-- Modify this to use your project's INFORMATION_SCHEMA table as follows:
-- `region-{region_name}`.INFORMATION_SCHEMA.{table}
`region-us`.INFORMATION_SCHEMA.JOBS_TIMELINE_BY_ORGANIZATION timeline_view
JOIN
joined_capacity
ON
timeline_view.reservation_id = joined_capacity.reservation_id
AND period_start BETWEEN joined_capacity.creation_time
AND joined_capacity.end_time
GROUP BY
1,
3,
4,
5,
6,
7