Skip to content

perf(coderd): simplify insights queries to speed them up #9299

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 0 additions & 9 deletions coderd/database/dbfake/dbfake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2274,15 +2274,6 @@ func (q *FakeQuerier) GetTemplateDailyInsights(ctx context.Context, arg database
}

for _, s := range q.workspaceAppStats {
// (was.session_started_at >= ts.from_ AND was.session_started_at < ts.to_)
// OR (was.session_ended_at > ts.from_ AND was.session_ended_at < ts.to_)
// OR (was.session_started_at < ts.from_ AND was.session_ended_at >= ts.to_)
if !(((s.SessionStartedAt.After(arg.StartTime) || s.SessionStartedAt.Equal(arg.StartTime)) && s.SessionStartedAt.Before(arg.EndTime)) ||
(s.SessionEndedAt.After(arg.StartTime) && s.SessionEndedAt.Before(arg.EndTime)) ||
(s.SessionStartedAt.Before(arg.StartTime) && (s.SessionEndedAt.After(arg.EndTime) || s.SessionEndedAt.Equal(arg.EndTime)))) {
continue
}

w, err := q.getWorkspaceByIDNoLock(ctx, s.WorkspaceID)
if err != nil {
return nil, err
Expand Down
11 changes: 0 additions & 11 deletions coderd/database/queries.sql.go

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

11 changes: 0 additions & 11 deletions coderd/database/queries/insights.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ WITH ts AS (
AND was.connection_count > 0
AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN was.template_id = ANY(@template_ids::uuid[]) ELSE TRUE END
)
WHERE
-- We already handle created_at in the join, but we use an additional
-- check against a static timeframe to help speed up the query.
was.created_at >= @start_time
AND was.created_at < @end_time
GROUP BY ts.from_, ts.to_, ts.seconds, was.user_id
), template_ids AS (
SELECT array_agg(DISTINCT template_id) AS ids
Expand Down Expand Up @@ -113,12 +108,6 @@ WITH ts AS (
wa.agent_id = was.agent_id
AND wa.slug = was.slug_or_port
)
WHERE
-- We already handle timeframe in the join, but we use an additional
-- check against a static timeframe to help speed up the query.
(was.session_started_at >= @start_time AND was.session_started_at < @end_time)
OR (was.session_ended_at > @start_time AND was.session_ended_at < @end_time)
OR (was.session_started_at < @start_time AND was.session_ended_at >= @end_time)
GROUP BY ts.from_, ts.to_, ts.seconds, w.template_id, was.user_id, was.agent_id, was.access_method, was.slug_or_port, wa.display_name, wa.icon, wa.slug
)

Expand Down