-
Notifications
You must be signed in to change notification settings - Fork 927
chore(coderd/database): optimize GetRunningPrebuiltWorkspaces #18588
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -49,18 +49,85 @@ WHERE tvp.desired_instances IS NOT NULL -- Consider only presets that have a pre | |||||||||||||
AND (t.id = sqlc.narg('template_id')::uuid OR sqlc.narg('template_id') IS NULL); | ||||||||||||||
|
||||||||||||||
-- name: GetRunningPrebuiltWorkspaces :many | ||||||||||||||
WITH | ||||||||||||||
latest_prebuilds | ||||||||||||||
AS ( | ||||||||||||||
SELECT | ||||||||||||||
latest_build.workspace_id, | ||||||||||||||
workspaces.name, | ||||||||||||||
workspaces.template_id, | ||||||||||||||
latest_build.template_version_id, | ||||||||||||||
latest_build.template_version_preset_id, | ||||||||||||||
latest_build.job_id, | ||||||||||||||
workspaces.created_at | ||||||||||||||
FROM | ||||||||||||||
workspaces | ||||||||||||||
LEFT JOIN LATERAL ( | ||||||||||||||
Comment on lines
+63
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
As well as here, SQL is a weird syntax in that it doesn't really have any meaningful nesting. |
||||||||||||||
SELECT | ||||||||||||||
workspace_builds.id, | ||||||||||||||
workspace_builds.workspace_id, | ||||||||||||||
workspace_builds.template_version_id, | ||||||||||||||
workspace_builds.job_id, | ||||||||||||||
workspace_builds.template_version_preset_id, | ||||||||||||||
workspace_builds.transition, | ||||||||||||||
workspace_builds.created_at, | ||||||||||||||
provisioner_jobs.job_status | ||||||||||||||
FROM | ||||||||||||||
workspace_builds | ||||||||||||||
JOIN provisioner_jobs ON | ||||||||||||||
provisioner_jobs.id | ||||||||||||||
= workspace_builds.job_id | ||||||||||||||
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
What's this 😆, (see others as well) |
||||||||||||||
WHERE | ||||||||||||||
workspace_builds.workspace_id | ||||||||||||||
= workspaces.id | ||||||||||||||
ORDER BY | ||||||||||||||
workspace_builds.workspace_id, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Could remove this redundant ordering due to WHERE filter (keep build_number though). |
||||||||||||||
workspace_builds.build_number | ||||||||||||||
DESC | ||||||||||||||
LIMIT | ||||||||||||||
1 | ||||||||||||||
) | ||||||||||||||
AS latest_build ON true | ||||||||||||||
WHERE | ||||||||||||||
workspaces.deleted = false | ||||||||||||||
AND workspaces.owner_id | ||||||||||||||
= 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID | ||||||||||||||
AND latest_build.transition | ||||||||||||||
= 'start'::workspace_transition | ||||||||||||||
AND latest_build.job_status | ||||||||||||||
= 'succeeded'::provisioner_job_status | ||||||||||||||
Comment on lines
+95
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential performance improvement, move these into Also, please consider whether it should be a |
||||||||||||||
), | ||||||||||||||
ready_agents | ||||||||||||||
AS ( | ||||||||||||||
SELECT | ||||||||||||||
latest_prebuilds.job_id, | ||||||||||||||
BOOL_AND(workspace_agents.lifecycle_state = 'ready'::workspace_agent_lifecycle_state)::boolean AS ready | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
What's this, it actually works? |
||||||||||||||
FROM | ||||||||||||||
latest_prebuilds | ||||||||||||||
JOIN workspace_resources ON | ||||||||||||||
workspace_resources.job_id = latest_prebuilds.job_id | ||||||||||||||
JOIN workspace_agents ON | ||||||||||||||
workspace_agents.resource_id = workspace_resources.id | ||||||||||||||
WHERE | ||||||||||||||
workspace_agents.deleted = false | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consideration: Should we filter out sub agents? I.e. |
||||||||||||||
GROUP BY | ||||||||||||||
latest_prebuilds.job_id | ||||||||||||||
) | ||||||||||||||
SELECT | ||||||||||||||
p.id, | ||||||||||||||
p.name, | ||||||||||||||
p.template_id, | ||||||||||||||
b.template_version_id, | ||||||||||||||
p.current_preset_id AS current_preset_id, | ||||||||||||||
p.ready, | ||||||||||||||
p.created_at | ||||||||||||||
FROM workspace_prebuilds p | ||||||||||||||
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id | ||||||||||||||
WHERE (b.transition = 'start'::workspace_transition | ||||||||||||||
AND b.job_status = 'succeeded'::provisioner_job_status); | ||||||||||||||
latest_prebuilds.workspace_id AS id, | ||||||||||||||
latest_prebuilds.name, | ||||||||||||||
latest_prebuilds.template_id, | ||||||||||||||
latest_prebuilds.template_version_id, | ||||||||||||||
latest_prebuilds.template_version_preset_id AS current_preset_id, | ||||||||||||||
COALESCE(ready_agents.ready, false)::boolean AS ready, | ||||||||||||||
latest_prebuilds.created_at | ||||||||||||||
FROM | ||||||||||||||
latest_prebuilds | ||||||||||||||
LEFT JOIN ready_agents ON | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This join will duplicate rows when there are multiple agents since there is no group-by here or in the |
||||||||||||||
ready_agents.job_id = latest_prebuilds.job_id | ||||||||||||||
ORDER BY | ||||||||||||||
latest_prebuilds.workspace_id ASC | ||||||||||||||
; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: s/spaces/tabs/ |
||||||||||||||
|
||||||||||||||
-- name: CountInProgressPrebuilds :many | ||||||||||||||
-- CountInProgressPrebuilds returns the number of in-progress prebuilds, grouped by preset ID and transition. | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could reduce indentation a bit here.