Skip to content
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

Remove O(N) queries from workspace status API #3860

Merged
merged 1 commit into from
Dec 15, 2023

Conversation

ghickman
Copy link
Contributor

@ghickman ghickman commented Dec 11, 2023

Previously this was doing O(N) queries, which we accepted when the API was addede because we did not, at the time, have workspaces with more than low-10s of jobs. We now have workspaces with hundreds of jobs defined. On a particularly large workspace (long-covid-symptoms) this endpoint could take as long as 5s (and ~300 queries) to calculate.

This change uses raw SQL so we can make use of a GROUP BY in the CTE to get the latest actions and then JOIN to it in the outer query to match those up with their statuses.

Found while investigating #3781. This isn't the cause of the slow down mentioned in that ticket, but it's running at least once on every page load, and eating away at our resources needlessly.

jobserver/models/core.py Outdated Show resolved Hide resolved
jobserver/models/core.py Outdated Show resolved Hide resolved
@ghickman ghickman force-pushed the optimise-workspace-status branch from f5f48cb to a3db731 Compare December 11, 2023 16:20
Previously this was doing O(N) queries, which we accepted when the API
was addede because we did not, at the time, have workspaces with more
than low-10s of jobs.  We now have workspaces with hundreds of jobs
defined.  On a particularly large workspace (long-covid-symptoms) this
endpoint could take as long as 5s (and ~300 queries) to calculate.

This change uses raw SQL so we can make use of a GROUP BY in the CTE to
get the latest actions and then JOIN to it in the outer query to match
those up with their statuses.
@ghickman ghickman force-pushed the optimise-workspace-status branch from a3db731 to cfad45f Compare December 11, 2023 16:21
Copy link
Contributor

@Jongmassey Jongmassey left a comment

Choose a reason for hiding this comment

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

🚀

@ghickman ghickman merged commit 2341f64 into main Dec 15, 2023
6 checks passed
@ghickman ghickman deleted the optimise-workspace-status branch December 15, 2023 12:34
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.

2 participants