Skip to content

chore: optimize workspace_latest_builds view query #17789

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 3 commits into from
May 13, 2025

Conversation

deansheather
Copy link
Member

@deansheather deansheather commented May 13, 2025

Avoids two sequential scans of massive tables (workspace_builds, provisioner_jobs) and uses index scans instead. This new view largely replicates our already optimized query GetWorkspaces to fetch the latest build.

The original query and the new query were compared against the dogfood database to ensure they return the exact same data in the exact same order (minus the new workspaces.deleted = false filter to improve performance even more). The performance is massively improved even without the workspaces.deleted = false filter, but it was added to improve it even more.

Note: these query times are probably inflated due to high database load on our dogfood environment that this intends to partially resolve.

Before: 2,139ms (explain)

After: 33ms (explain)

Co-authored-by: Cian Johnston cian@coder.com

johnstcn
johnstcn previously approved these changes May 13, 2025
@deansheather deansheather force-pushed the dean/optimize-latest-builds-query branch from 0d2d188 to 9811abe Compare May 13, 2025 11:19
@dannykopping
Copy link
Contributor

Let's please wait for @evgeniy-scherbina to review this before merging.

Thank you for the effort put into this ❤️

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.

LGTM, nice work!

…mization.up.sql

Co-authored-by: Mathias Fredriksson <mafredri@gmail.com>
Copy link
Contributor

@evgeniy-scherbina evgeniy-scherbina left a comment

Choose a reason for hiding this comment

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

LGTM

Good idea with LATERAL JOIN and filtering out deleted workspaces.

Optionally we can preserve formatting & comments when we recreate workspace_prebuilds view.

Signed-off-by: Danny Kopping <dannykopping@gmail.com>
@dannykopping
Copy link
Contributor

I'm going to merge #17792 first so we can observe the impact on database load from that, and then merge this in to see it further drop.

Thanks all 🎉

dannykopping added a commit that referenced this pull request May 13, 2025
`Collect()` is called whenever the `/metrics` endpoint is hit to
retrieve metrics.

The queries used in prebuilds metrics collection are quite heavy, and we
want to avoid having them running concurrently / too often to keep db
load down.

Here I'm moving towards a background retrieval of the state required to
set the metrics, which gets invalidated every interval.

Also introduces `coderd_prebuilt_workspaces_metrics_last_updated` which
operators can use to determine when these metrics go stale.

See #17789 as well.

---------

Signed-off-by: Danny Kopping <dannykopping@gmail.com>
@dannykopping dannykopping merged commit ef745c0 into main May 13, 2025
34 checks passed
@dannykopping dannykopping deleted the dean/optimize-latest-builds-query branch May 13, 2025 18:51
@github-actions github-actions bot locked and limited conversation to collaborators May 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants