#20327: Device queries now faster when including ConfigContexts #1255

Closed
opened 2026-04-05 22:38:19 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @jnovinger on 9/12/2025

Fixes: #20327

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.

This is also likely to have positive impacts in other places that make use of these queryset methods, including:

  • /api/virtualization/virtual-machines
  • /dcim/devices/
  • /virtualization/virtual-machines/
*Originally created by @jnovinger on 9/12/2025* ### Fixes: #20327 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. This is also likely to have positive impacts in other places that make use of these queryset methods, including: - /api/virtualization/virtual-machines - /dcim/devices/ - /virtualization/virtual-machines/
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#1255