Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Conversation

@matt-codecov
Copy link
Contributor

@matt-codecov matt-codecov commented Apr 7, 2025

fetching a badge uses 4+ queries:

  • select * from owners where owners.name = ${request.username} and owners.service = ${request.service}
  • select * from repos where repos.ownerid = ${owner.ownerid} and repos.name = ${request.repo_name}
  • select * from branches where branches.name = ${request.branch_name} and branches.repo_id = ${repo.repoid}
  • select * from commits where commits.commitid = ${branch.head} and commits.repoid = ${repo.repoid}

(side note: the first two queries actually have a LIMIT 21 which suggests they were logged while in queryset form. i am guessing a subsequent .first() call happens but doesn't need to re-run the query?)

in principle they could be folded into one:

select
  commits.*
from
  owners
inner join
  repos
on
  owners.ownerid = repos.ownerid
inner join
  branches
on
  branches.repo_id = repos.repoid
inner join
  commits
on
  commits.repoid = repos.repoid
where
  owners.username = ${request.username}
  and owners.service = ${request.service}
  and repos.name = ${request.repo_name}
  and branches.name = ${request.branch_name}
  and branches.head = commits.commitid

doing so might improve the performance of the badges endpoint. don't know

this PR doesn't actually do that:

  • i'm not actually sure where the owners and repos queries happen, so those are left out
  • because branches and commits don't actually have a FK relationship, Django won't perform a join between them

instead, this PR would result in SQL that looks something like:

select * from commits
where
  commits.repoid = ${repo.repoid}
  and commits.commit in (
    select * from branches
    where
      branches.name = ${request.branch_name}
      and branches.repoid = ${repo.repoid}
  )

maybe adding LIMIT = 1 to the subquery would help. not sure

i don't know whether the performance would actually improve, so i am closing this. just created a PR to document the result of this little exploration.

@seer-by-sentry
Copy link
Contributor

seer-by-sentry bot commented Apr 7, 2025

✅ Sentry found no issues in your recent changes ✅

@matt-codecov matt-codecov deleted the matt/unify-badge-queries branch April 7, 2025 22:28
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.

2 participants