Add missing foreign key indexes - #4131
Merged
Merged
Conversation
joinedload(InstanceModel.jobs) joins on jobs.instance_id, which had no index, so every instance refetch sequentially scanned the whole jobs table.
These foreign keys are loaded as collections from the parent side, so each load sequentially scanned the whole child table. jobs.instance_id accounted for 69.7% of prod DB time: 12M calls at 354ms mean, reading 15k blocks to return 0 rows.
The query loaded all 46 InstanceModel columns plus every column of the joined project and fleet. ProjectModel.owner is lazy="joined", so it also joined users, which the metrics loop never reads.
The implicit eager load joined users into every project query, including the many that only use projects for scoping and never read the owner.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4129
JobModel.instance_idindex that led to full jobs table scan.BackendModel.project_idis also expected to have a huge benefit.ProjectModel.ownersince it's often not needed.