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

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Jun 25, 2025

@johnstcn johnstcn marked this pull request as ready for review June 26, 2025 20:50
@johnstcn johnstcn requested review from mafredri and SasSwart June 26, 2025 20:50
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

I was a bit thrown off by the indentation, but I tried my best 😆, left some suggestions and questions.

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/

Comment on lines +53 to +54
latest_prebuilds
AS (
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.

Comment on lines +63 to +65
FROM
workspaces
LEFT JOIN LATERAL (
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.

Comment on lines +78 to +79
provisioner_jobs.id
= workspace_builds.job_id
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)

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?

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?

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.

Comment on lines +95 to +98
AND latest_build.transition
= 'start'::workspace_transition
AND latest_build.job_status
= 'succeeded'::provisioner_job_status
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.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: GetRunningPrebuiltWorkspaces creates lots of DB load
2 participants