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

fix: N+1 query for plans / tiers #1060

Merged
merged 1 commit into from
Feb 3, 2025
Merged

Conversation

ajay-sentry
Copy link
Contributor

This PR aims to fix an N+1 query that was introduced when swapping to the plan model as the source of truth from the consts in shared for the same.

Previously we were calling the plan query n number of times depending on how many values get returned in line 179 here:

for key, title, status_config in self.get_statuses(current_flags):
if self._should_use_status_notifier(status_type=key):
yield self._use_status_and_possibly_checks_notifiers(
key=key,
title=title,
status_config=status_config,
)

This cascaded down to

if not self.plan:
self.plan = Plan.objects.select_related("tier").get(name=owner.plan)
and
if not self.plan:
self.plan = Plan.objects.select_related("tier").get(name=owner.plan)
where since plan wasn't persisted on the NotificationService we were calling the query every single time.

My fix is to persist the plan on the NotificationService object so if it exists we skip calling the query and just use that reference instead. I tested this against some of the existing UTs with prints to confirm the plan was persisted as I expected.

Closes https://github.com/codecov/internal-issues/issues/1195

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

Copy link

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.52%. Comparing base (6302730) to head (2508bbc).
Report is 7 commits behind head on main.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #1060    +/-   ##
========================================
  Coverage   97.52%   97.52%            
========================================
  Files         462      463     +1     
  Lines       37902    38037   +135     
========================================
+ Hits        36963    37095   +132     
- Misses        939      942     +3     
Flag Coverage Δ
integration 42.85% <85.71%> (-0.04%) ⬇️
unit 90.20% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

⚠️ Impact Analysis from Codecov is deprecated and will be sunset on Jan 31 2025. See more

@codecov-notifications
Copy link

codecov-notifications bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

Copy link

github-actions bot commented Feb 3, 2025

✅ All tests successful. No failed tests were found.

📣 Thoughts on this report? Let Codecov know! | Powered by Codecov

PlanName.TEAM_MONTHLY.value,
PlanName.TEAM_YEARLY.value,
}:
if owner_plan.tier_name == TierName.TEAM.value:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a reference that was missed prior, it should be using the tier derived from the plan rather than a hard coded set of plans

Copy link
Contributor

Choose a reason for hiding this comment

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

NICE

PlanName.TEAM_MONTHLY.value,
PlanName.TEAM_YEARLY.value,
}:
if owner_plan.tier_name == TierName.TEAM.value:
Copy link
Contributor

Choose a reason for hiding this comment

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

NICE

@ajay-sentry ajay-sentry added this pull request to the merge queue Feb 3, 2025
Merged via the queue into main with commit 960a585 Feb 3, 2025
26 of 27 checks passed
@ajay-sentry ajay-sentry deleted the Ajay/fix-n+1-query-plans branch February 3, 2025 19:00
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.

2 participants