Skip to content

Commit 151a76b

Browse files
committed
Simplify sql query
1 parent 5bac9fc commit 151a76b

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

coderd/database/querier.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 24 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/templates.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ RETURNING
107107
*;
108108

109109
-- name: GetTemplatesAverageBuildTime :many
110+
-- Computes average build time for every template.
111+
-- Only considers last moving_average_size successful builds between start_ts and end_ts.
112+
-- If a template does not have at least min_completed_job_count such builds, it gets skipped.
110113
WITH query_with_all_job_count AS (SELECT
111114
DISTINCT t.id,
112115
AVG(pj.exec_time_sec)
@@ -122,7 +125,7 @@ FROM
122125
active_version_id
123126
FROM
124127
templates) AS t
125-
LEFT JOIN
128+
INNER JOIN
126129
(SELECT
127130
workspace_id,
128131
template_version_id,
@@ -133,7 +136,7 @@ LEFT JOIN
133136
wb
134137
ON
135138
t.id = wb.workspace_id AND t.active_version_id = wb.template_version_id
136-
LEFT JOIN
139+
INNER JOIN
137140
(SELECT
138141
id,
139142
completed_at,
@@ -155,6 +158,5 @@ SELECT
155158
FROM
156159
query_with_all_job_count
157160
WHERE
158-
avg_build_time_sec IS NOT NULL AND
159-
job_count >= GREATEST(@min_completed_job_count::integer, 1)
161+
job_count >= @min_completed_job_count::integer
160162
;

0 commit comments

Comments
 (0)