Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47fd190

Browse files
authored
fix(coderd/database): improve perf of GetTemplateInsightsByInterval (coder#12773)
Refs coder#12122
1 parent ba1eace commit 47fd190

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

coderd/database/queries.sql.go

+13-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/insights.sql

+13-9
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ WITH
355355
CROSS JOIN
356356
generate_series(
357357
date_trunc('minute', was.session_started_at),
358-
-- Subtract 1 microsecond to avoid creating an extra series.
358+
-- Subtract 1 μs to avoid creating an extra series.
359359
date_trunc('minute', was.session_ended_at - '1 microsecond'::interval),
360360
'1 minute'::interval
361361
) AS s(minute_bucket)
@@ -389,14 +389,17 @@ WITH
389389
ts AS (
390390
SELECT
391391
d::timestamptz AS from_,
392-
CASE
393-
WHEN (d::timestamptz + (@interval_days::int || ' day')::interval) <= @end_time::timestamptz
394-
THEN (d::timestamptz + (@interval_days::int || ' day')::interval)
395-
ELSE @end_time::timestamptz
396-
END AS to_
392+
LEAST(
393+
(d::timestamptz + (@interval_days::int || ' day')::interval)::timestamptz,
394+
@end_time::timestamptz
395+
)::timestamptz AS to_
397396
FROM
398-
-- Subtract 1 microsecond from end_time to avoid including the next interval in the results.
399-
generate_series(@start_time::timestamptz, (@end_time::timestamptz) - '1 microsecond'::interval, (@interval_days::int || ' day')::interval) AS d
397+
generate_series(
398+
@start_time::timestamptz,
399+
-- Subtract 1 μs to avoid creating an extra series.
400+
(@end_time::timestamptz) - '1 microsecond'::interval,
401+
(@interval_days::int || ' day')::interval
402+
) AS d
400403
)
401404

402405
SELECT
@@ -410,6 +413,7 @@ LEFT JOIN
410413
template_usage_stats AS tus
411414
ON
412415
tus.start_time >= ts.from_
416+
AND tus.start_time < ts.to_ -- End time exclusion criteria optimization for index.
413417
AND tus.end_time <= ts.to_
414418
AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN tus.template_id = ANY(@template_ids::uuid[]) ELSE TRUE END
415419
GROUP BY
@@ -473,7 +477,7 @@ WITH
473477
CROSS JOIN
474478
generate_series(
475479
date_trunc('minute', was.session_started_at),
476-
-- Subtract 1 microsecond to avoid creating an extra series.
480+
-- Subtract 1 μs to avoid creating an extra series.
477481
date_trunc('minute', was.session_ended_at - '1 microsecond'::interval),
478482
'1 minute'::interval
479483
) AS s(minute_bucket)

0 commit comments

Comments
 (0)