-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
perf(organization-teams): fix n+1 org query #82109
base: master
Are you sure you want to change the base?
Conversation
src/sentry/models/projectteam.py
Outdated
orgs = {} | ||
for team in teams: | ||
if team.organization_id not in orgs: | ||
orgs[team.organization_id] = team.organization |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're here, let's make this a bulk query to fetch all the orgs at once? You can use prefetch_related_objects
to do this:
prefetch_related_objects(item_list, "project__organization") |
Then there's no need for the orgs dict at all. Just iterate through all the project teams and do set_cached_field_value
with the org directly
Nm, we still need the orgs dict because project_teams
doesn't have it cached
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there will only ever be one org as where this is called asserts that, but i'll make this change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's simpler to just stick the prefetch in so that if someone uses this elsewhere it works
src/sentry/models/projectteam.py
Outdated
@@ -21,9 +21,11 @@ def get_for_teams_with_org_cache(self, teams: Sequence["Team"]) -> QuerySet["Pro | |||
.order_by("project__name", "project__slug") | |||
.select_related("project") | |||
) | |||
|
|||
# TODO(dcramer): we should query in bulk for ones we're missing here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can remove this comment after we bulk fetch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/sentry/models/projectteam.py
Outdated
prefetch_related_objects(project_teams, "project__organization") | ||
orgs = {pt.project.organization_id: pt.project.organization for pt in project_teams} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, actually I was thinking you'd do this on the teams
. But doing it on the project_teams
actually does the caching you want in the next loop. So actually, you can just delete everything after line 24, since all that set_cached_field_value
does is populate project_team.project.organization
.
Reading this even more... We could also just do .select_related("project", "project__organization")
and return that list of project teams. No idea why it wasn't done this way when originally implemented...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah this makes sense. simplified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, so much cleaner!
❌ 1 Tests Failed:
View the top 1 failed tests by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
the way this is currently written it will make the same sql query over and over again 🤦🏼
this fixes it so we simply do once for each org_id. (there should never be more than one org_id in a set of teams being queried, but will leave that for future refactors.