Skip to content

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

@jeremystretch

Description

@jeremystretch

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.

Metadata

Metadata

Assignees

Labels

severity: lowDoes not significantly disrupt application functionality, or a workaround is availablestatus: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions