Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

JoshFerge
Copy link
Member

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.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Dec 13, 2024
@JoshFerge JoshFerge requested a review from a team December 13, 2024 22:52
@JoshFerge JoshFerge enabled auto-merge (squash) December 13, 2024 22:54
Comment on lines 25 to 28
orgs = {}
for team in teams:
if team.organization_id not in orgs:
orgs[team.organization_id] = team.organization
Copy link
Member

@wedamija wedamija Dec 13, 2024

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

Copy link
Member Author

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

Copy link
Member

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

@JoshFerge JoshFerge disabled auto-merge December 13, 2024 22:59
@@ -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
Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how feels to delete a # TODO(dcramer):
image

Comment on lines 24 to 25
prefetch_related_objects(project_teams, "project__organization")
orgs = {pt.project.organization_id: pt.project.organization for pt in project_teams}
Copy link
Member

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...

Copy link
Member Author

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.

Copy link
Member

@wedamija wedamija left a 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!

Copy link

codecov bot commented Dec 14, 2024

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
21150 1 21149 209
View the top 1 failed tests by shortest run time
tests.sentry.sentry_metrics.test_snuba.SnubaMetricsInterfaceTest::test_count_query
Stack Traces | 3.5s run time
#x1B[1m#x1B[.../sentry/sentry_metrics/test_snuba.py#x1B[0m:57: in test_count_query
    data = get_series(
#x1B[1m#x1B[.../snuba/metrics/datasource.py#x1B[0m:1071: in get_series
    ).get_snuba_queries()
#x1B[1m#x1B[.../snuba/metrics/query_builder.py#x1B[0m:1133: in get_snuba_queries
    component_entities = metric_field_obj.get_entity(
#x1B[1m#x1B[.../metrics/fields/base.py#x1B[0m:784: in get_entity
    return _get_entity_of_metric_mri(projects, self.metric_object.metric_mri, use_case_id).value
#x1B[1m#x1B[.../metrics/fields/base.py#x1B[0m:278: in _get_entity_of_metric_mri
    raise InvalidParams(f"Raw metric {get_public_name_from_mri(metric_mri)} does not exist")
#x1B[1m#x1B[31mE   sentry.exceptions.InvalidParams: Raw metric measurements.speed does not exist#x1B[0m

To view more test analytics, go to the Test Analytics Dashboard
📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants