Skip to content

Commit 5a1ec8d

Browse files
committed
Fixes #20327: Device queries are now faster when including ConfidContexts
Move .distinct() from main queryset to tag subquery to eliminate performance bottleneck when querying devices with config contexts. The .distinct() call on the main device queryset was causing PostgreSQL to sort all devices before pagination, resulting in 15x slower API responses for large installations (10k+ devices, 100+ config contexts). Moving .distinct() to the tag subquery eliminates duplicates at their source (GenericForeignKey tag relationships) while preserving the fix for issues #5314 and #5387 without impacting overall query performance.
1 parent 103939a commit 5a1ec8d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

netbox/extras/querysets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def annotate_config_context_data(self):
9393
_data=EmptyGroupByJSONBAgg('data', ordering=['weight', 'name'])
9494
).values("_data").order_by()
9595
)
96-
).distinct()
96+
)
9797

9898
def _get_config_context_filters(self):
9999
# Construct the set of Q objects for the specific object types
@@ -117,7 +117,7 @@ def _get_config_context_filters(self):
117117
).values_list(
118118
'tag_id',
119119
flat=True
120-
)
120+
).distinct()
121121
)
122122
) | Q(tags=None),
123123
is_active=True,

0 commit comments

Comments
 (0)