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 on segment overrides for environment-document endpoint #3512

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/environments/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def filter_for_document_builder(self, *args, **kwargs):
Prefetch(
"project__segments__feature_segments__feature_states",
queryset=FeatureState.objects.select_related(
"feature", "feature_state_value"
"feature", "feature_state_value", "environment"
),
),
Prefetch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from rest_framework.test import APIClient

from environments.models import Environment, EnvironmentAPIKey
from features.models import Feature
from features.feature_types import MULTIVARIATE
from features.models import Feature, FeatureSegment, FeatureState
from features.multivariate.models import MultivariateFeatureOption
from segments.models import Condition, Segment, SegmentRule


Expand All @@ -22,7 +24,7 @@ def test_get_environment_document(
client.credentials(HTTP_X_ENVIRONMENT_KEY=api_key.key)

# and some other sample data to make sure we're testing all of the document
Feature.objects.create(name="test_feature", project=project)
feature = Feature.objects.create(name="test_feature", project=project)
for i in range(10):
segment = Segment.objects.create(project=project)
segment_rule = SegmentRule.objects.create(
Expand All @@ -44,11 +46,37 @@ def test_get_environment_document(
rule=nested_rule,
)

# Let's create segment override for each segment too
feature_segment = FeatureSegment.objects.create(
segment=segment, feature=feature, environment=environment
)
FeatureState.objects.create(
feature=feature,
environment=environment,
feature_segment=feature_segment,
enabled=True,
)

for i in range(10):
mv_feature = Feature.objects.create(
name=f"mv_feature_{i}", project=project, type=MULTIVARIATE
)
MultivariateFeatureOption.objects.create(
feature=mv_feature,
default_percentage_allocation=10,
string_value="option-1",
)
MultivariateFeatureOption.objects.create(
feature=mv_feature,
default_percentage_allocation=10,
string_value="option-2",
)

# and the relevant URL to get an environment document
url = reverse("api-v1:environment-document")

# When
with django_assert_num_queries(11):
with django_assert_num_queries(13):
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 number increased to 23 before I added the select related. I think the additional 2 queries are legitimate for retrieving the MV data and the segment overrides.

response = client.get(url)

# Then
Expand Down
3 changes: 3 additions & 0 deletions api/util/mappers/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ def _get_prioritised_feature_states(
) -> List["FeatureState"]:
prioritised_feature_state_by_feature_id = {}
for feature_state in feature_states:
# TODO: this call to is_live was causing an N+1 issue.
# For now, we have solved it with an extra select_related, but
# there is probably a neater solution here.
if not feature_state.is_live:
continue
if existing_feature_state := prioritised_feature_state_by_feature_id.get(
Expand Down
Loading