Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(coderd/database): improve perf ofGetTemplateInsightsByInterval#12773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletionscoderd/database/queries.sql.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

22 changes: 13 additions & 9 deletionscoderd/database/queries/insights.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -355,7 +355,7 @@ WITH
CROSS JOIN
generate_series(
date_trunc('minute', was.session_started_at),
-- Subtract 1microsecond to avoid creating an extra series.
-- Subtract 1μs to avoid creating an extra series.
date_trunc('minute', was.session_ended_at - '1 microsecond'::interval),
'1 minute'::interval
) AS s(minute_bucket)
Expand DownExpand Up@@ -389,14 +389,17 @@ WITH
ts AS (
SELECT
d::timestamptz AS from_,
CASE
WHEN (d::timestamptz + (@interval_days::int || ' day')::interval) <= @end_time::timestamptz
THEN (d::timestamptz + (@interval_days::int || ' day')::interval)
ELSE @end_time::timestamptz
END AS to_
LEAST(
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LEAST is more performant than case.

mtojek reacted with eyes emoji
(d::timestamptz + (@interval_days::int || ' day')::interval)::timestamptz,
@end_time::timestamptz
)::timestamptz AS to_
FROM
-- Subtract 1 microsecond from end_time to avoid including the next interval in the results.
generate_series(@start_time::timestamptz, (@end_time::timestamptz) - '1 microsecond'::interval, (@interval_days::int || ' day')::interval) AS d
generate_series(
@start_time::timestamptz,
-- Subtract 1 μs to avoid creating an extra series.
(@end_time::timestamptz) - '1 microsecond'::interval,
(@interval_days::int || ' day')::interval
) AS d
)

SELECT
Expand All@@ -410,6 +413,7 @@ LEFT JOIN
template_usage_stats AS tus
ON
tus.start_time >= ts.from_
AND tus.start_time < ts.to_ -- End time exclusion criteria optimization for index.
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Main performance optimization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Nice spotting 👍 👍

AND tus.end_time <= ts.to_
AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN tus.template_id = ANY(@template_ids::uuid[]) ELSE TRUE END
GROUP BY
Expand DownExpand Up@@ -473,7 +477,7 @@ WITH
CROSS JOIN
generate_series(
date_trunc('minute', was.session_started_at),
-- Subtract 1microsecond to avoid creating an extra series.
-- Subtract 1μs to avoid creating an extra series.
date_trunc('minute', was.session_ended_at - '1 microsecond'::interval),
'1 minute'::interval
) AS s(minute_bucket)
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp