Filtering devices using the q filter takes excessively long when many inventory items are present #11751

Closed
opened 2025-12-29 21:49:26 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Oct 20, 2025).

Originally assigned to: @jeremystretch on GitHub.

NetBox Edition

NetBox Community

NetBox Version

v4.4.4

Python Version

3.12

Steps to Reproduce

  1. Have thousands of devices, each with ~100 or more inventory items installed.
  2. Attempt to search for a device by name using the q filter, e.g.

GET /api/dcim/devices/?brief=true&limit=100&q=foobar

Expected Behavior

The set of matching devices should be returned fairly quickly.

Observed Behavior

The query takes an inordinate amount of time due to the underlying search() method pulling in all installed inventory items:

return queryset.filter(
    Q(name__icontains=value) |
    Q(virtual_chassis__name__icontains=value) |
    Q(serial__icontains=value.strip()) |
    Q(inventoryitems__serial__icontains=value.strip()) |  # Queries *all* inventory items
    Q(asset_tag__icontains=value.strip()) |
    Q(description__icontains=value.strip()) |
    Q(comments__icontains=value) |
    Q(primary_ip4__address__startswith=value) |
    Q(primary_ip6__address__startswith=value)
).distinct()

While I can appreciate the utility in including these, it has a very pronounced impact on performance when many inventory items have been created. I propose removing the inventoryitems__serial__icontains lookup from the search() method, and perhaps introducing a more narrowly-focused filter to search specifically on serial number and/or asset tag of the device and its inventory items.

Originally created by @jeremystretch on GitHub (Oct 20, 2025). Originally assigned to: @jeremystretch on GitHub. ### NetBox Edition NetBox Community ### NetBox Version v4.4.4 ### Python Version 3.12 ### Steps to Reproduce 1. Have thousands of devices, each with ~100 or more inventory items installed. 2. Attempt to search for a device by name using the `q` filter, e.g. `GET /api/dcim/devices/?brief=true&limit=100&q=foobar` ### Expected Behavior The set of matching devices should be returned fairly quickly. ### Observed Behavior The query takes an inordinate amount of time due to the [underlying `search()` method](https://github.com/netbox-community/netbox/blob/0b97df098403b24d60889a69a5de9cb2409efe29/netbox/dcim/filtersets.py#L1284) pulling in _all_ installed inventory items: ```python return queryset.filter( Q(name__icontains=value) | Q(virtual_chassis__name__icontains=value) | Q(serial__icontains=value.strip()) | Q(inventoryitems__serial__icontains=value.strip()) | # Queries *all* inventory items Q(asset_tag__icontains=value.strip()) | Q(description__icontains=value.strip()) | Q(comments__icontains=value) | Q(primary_ip4__address__startswith=value) | Q(primary_ip6__address__startswith=value) ).distinct() ``` While I can appreciate the utility in including these, it has a very pronounced impact on performance when many inventory items have been created. I propose removing the `inventoryitems__serial__icontains` lookup from the `search()` method, and perhaps introducing a more narrowly-focused filter to search specifically on serial number and/or asset tag of the device and its inventory items.
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 21:49:26 +01:00
adam closed this issue 2025-12-29 21:49:26 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11751