-
Notifications
You must be signed in to change notification settings - Fork 522
fix(Segments): Fix N+1 on get_segment retrieve #7563
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -104,7 +104,7 @@ def get_queryset(self): # type: ignore[no-untyped-def] | |||||||||
| project = self.get_project() | ||||||||||
| queryset = Segment.live_objects.filter(project=project, is_system_segment=False) | ||||||||||
|
|
||||||||||
| if self.action == "list": | ||||||||||
| if self.action in ("list", "retrieve"): | ||||||||||
| # TODO: at the moment, the UI only shows the name and description of the segment in the list view. | ||||||||||
| # we shouldn't return all of the rules and conditions in the list view. | ||||||||||
| queryset = queryset.prefetch_related( | ||||||||||
|
|
@@ -116,6 +116,9 @@ def get_queryset(self): # type: ignore[no-untyped-def] | |||||||||
| "metadata", | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if self.action == "retrieve": | ||||||||||
| queryset = queryset.select_related("project__organisation") | ||||||||||
|
Comment on lines
+119
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||
|
|
||||||||||
| query_serializer = SegmentListQuerySerializer(data=self.request.query_params) | ||||||||||
| query_serializer.is_valid(raise_exception=True) | ||||||||||
|
|
||||||||||
|
|
||||||||||
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.
The N+1 optimization (prefetching rules and conditions) should also be applied to other actions that return the full segment representation, such as
update,partial_update, andclone. This ensures that the response serialization and the cloning logic (which iterates over rules/conditions) do not trigger redundant database queries.