Skip to content

Add missing foreign key indexes - #4131

Merged
r4victor merged 4 commits into
masterfrom
pr_add_jobs_instance_id_index
Aug 11, 2026
Merged

Add missing foreign key indexes#4131
r4victor merged 4 commits into
masterfrom
pr_add_jobs_instance_id_index

Conversation

@r4victor

Copy link
Copy Markdown
Collaborator

Fixes #4129

  • Add foreign key indexes for Parent.children joinedloads. The biggest impact should be missing JobModel.instance_id index that led to full jobs table scan. BackendModel.project_id is also expected to have a huge benefit.
  • Stop auto-loading ProjectModel.owner since it's often not needed.

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.
@r4victor
r4victor merged commit c6f7a18 into master Aug 11, 2026
27 checks passed
@r4victor
r4victor deleted the pr_add_jobs_instance_id_index branch August 11, 2026 12:07
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.

Slow /metrics endpoint

1 participant