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

Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
88 changes: 77 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.

89 changes: 78 additions & 11 deletions coderd/database/queries/prebuilds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Comment on lines +53 to +54
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
latest_prebuilds
AS (
latest_prebuilds AS (

We could reduce indentation a bit here.

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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
FROM
workspaces
LEFT JOIN LATERAL (
FROM
workspaces
LEFT JOIN LATERAL (

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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
provisioner_jobs.id
= workspace_builds.job_id
provisioner_jobs.id = workspace_builds.job_id

What's this 😆, (see others as well)

WHERE
workspace_builds.workspace_id
= workspaces.id
ORDER BY
workspace_builds.workspace_id,
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

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

Potential performance improvement, move these into latest_build subquery.

Also, please consider whether it should be a LEFT JOIN LATERAL or (INNER) JOIN LATERAL. With these filters it's essentially an INNER join currently.

),
ready_agents
AS (
SELECT
latest_prebuilds.job_id,
BOOL_AND(workspace_agents.lifecycle_state = 'ready'::workspace_agent_lifecycle_state)::boolean AS ready
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
BOOL_AND(workspace_agents.lifecycle_state = 'ready'::workspace_agent_lifecycle_state)::boolean AS ready
workspace_agents.lifecycle_state = 'ready'::workspace_agent_lifecycle_state AS ready

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
Copy link
Member

Choose a reason for hiding this comment

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

For consideration: Should we filter out sub agents? I.e. AND workspace_agents.parent_id IS NULL.

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
Copy link
Member

Choose a reason for hiding this comment

The 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 CTE, was this considered?

ready_agents.job_id = latest_prebuilds.job_id
ORDER BY
latest_prebuilds.workspace_id ASC
;
Copy link
Member

Choose a reason for hiding this comment

The 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.
Expand Down
Loading