Remove O(N) queries from workspace status API #3860
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.