[PR #20346] [MERGED] #20327: Device queries now faster when including ConfigContexts #15898

Closed
opened 2025-12-30 00:24:40 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/20346
Author: @jnovinger
Created: 9/13/2025
Status: Merged
Merged: 9/15/2025
Merged by: @jeremystretch

Base: mainHead: 20327-slow-device-queries-with-config-contexts


📝 Commits (3)

  • e5fc23f Fixes #20327: Device queries are now faster when including ConfidContexts
  • f51b377 Add performance regression test for config context annotation
  • 52c9e54 Address PR feedback, clean up new regression test

📊 Changes

2 files changed (+71 additions, -27 deletions)

View changed files

📝 netbox/extras/querysets.py (+2 -2)
📝 netbox/extras/tests/test_models.py (+69 -25)

📄 Description

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/

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/20346 **Author:** [@jnovinger](https://github.com/jnovinger) **Created:** 9/13/2025 **Status:** ✅ Merged **Merged:** 9/15/2025 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `main` ← **Head:** `20327-slow-device-queries-with-config-contexts` --- ### 📝 Commits (3) - [`e5fc23f`](https://github.com/netbox-community/netbox/commit/e5fc23f2f31a4fbac34e04a4de99400ee8a4ff66) Fixes #20327: Device queries are now faster when including ConfidContexts - [`f51b377`](https://github.com/netbox-community/netbox/commit/f51b377f639ad303f26cb19d9559187b03341f80) Add performance regression test for config context annotation - [`52c9e54`](https://github.com/netbox-community/netbox/commit/52c9e5429f873b7e3426589951a2e236d00ea1d8) Address PR feedback, clean up new regression test ### 📊 Changes **2 files changed** (+71 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `netbox/extras/querysets.py` (+2 -2) 📝 `netbox/extras/tests/test_models.py` (+69 -25) </details> ### 📄 Description ### 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/ --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:24:40 +01:00
adam closed this issue 2025-12-30 00:24:40 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15898