N+1 issue on the /api/ipam/ip-addresses/ endpoint #9540

Closed
opened 2025-12-29 20:51:13 +01:00 by adam · 6 comments
Owner

Originally created by @mulmat on GitHub (Apr 25, 2024).

Originally assigned to: @mulmat on GitHub.

Deployment Type

Self-hosted

NetBox Version

v3.7.6

Python Version

3.10

Steps to Reproduce

Call the /api/ipam/ip-addresses/ endpoint with more than 1 assigned ip address in the database (important that the ip addresses are assigned and you are listing assigned ip addresses, for example by using assigned_to_interface=true filter)

Expected Behavior

No N+1 issue.

I can submit I PR if wanted (I think the issue is that IPAddressViewSet are missing assigned_object_type in the prefetch_related)

Observed Behavior

N+1 issue with the following query:

SELECT ••• FROM "django_content_type" WHERE "django_content_type"."id" = X LIMIT 21

Originally created by @mulmat on GitHub (Apr 25, 2024). Originally assigned to: @mulmat on GitHub. ### Deployment Type Self-hosted ### NetBox Version v3.7.6 ### Python Version 3.10 ### Steps to Reproduce Call the `/api/ipam/ip-addresses/` endpoint with more than 1 assigned ip address in the database (important that the ip addresses are assigned and you are listing assigned ip addresses, for example by using `assigned_to_interface=true` filter) ### Expected Behavior No N+1 issue. I can submit I PR if wanted (I think the issue is that `IPAddressViewSet` are missing `assigned_object_type` in the `prefetch_related`) ### Observed Behavior N+1 issue with the following query: `SELECT ••• FROM "django_content_type" WHERE "django_content_type"."id" = X LIMIT 21`
adam added the type: bugstatus: needs ownerseverity: low labels 2025-12-29 20:51:13 +01:00
adam closed this issue 2025-12-29 20:51:13 +01:00
Author
Owner

@jeremystretch commented on GitHub (Apr 25, 2024):

Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v3.7.6. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data.

@jeremystretch commented on GitHub (Apr 25, 2024): Thank you for opening a bug report. I was unable to reproduce the reported behavior on NetBox v3.7.6. Please re-confirm the reported behavior on the current stable release and adjust your post above as necessary. Remember to provide detailed steps that someone else can follow using a clean installation of NetBox to reproduce the issue. Remember to include the steps taken to create any initial objects or other data.
Author
Owner

@mulmat commented on GitHub (Apr 25, 2024):

@jeremystretch - forgot to mention that the ips must be assigned. I've updated the post now above + also verified that the issue also affects 3.7.6

As a reference, I created ips like this to reproduce the issue:

IPAddress.objects.create(
    address=ip,
    assigned_object_type=ContentType.objects.get(app_label="dcim", model="interface"),
    assigned_object_id=52,
)
@mulmat commented on GitHub (Apr 25, 2024): @jeremystretch - forgot to mention that the ips must be assigned. I've updated the post now above + also verified that the issue also affects 3.7.6 As a reference, I created ips like this to reproduce the issue: ``` IPAddress.objects.create( address=ip, assigned_object_type=ContentType.objects.get(app_label="dcim", model="interface"), assigned_object_id=52, ) ```
Author
Owner

@mulmat commented on GitHub (Apr 29, 2024):

@jeremystretch - I can fix a PR for this

@mulmat commented on GitHub (Apr 29, 2024): @jeremystretch - I can fix a PR for this
Author
Owner

@jeremystretch commented on GitHub (Apr 29, 2024):

@mulmat thanks, I've assigned this to you. A word of caution: be aware that the tables wipe out any prefetches set on the view's queryset, so addressing this will likely need work on the column itself.

@jeremystretch commented on GitHub (Apr 29, 2024): @mulmat thanks, I've assigned this to you. A word of caution: be aware that the tables wipe out any prefetches set on the view's queryset, so addressing this will likely need work on the column itself.
Author
Owner

@mulmat commented on GitHub (Apr 29, 2024):

@mulmat thanks, I've assigned this to you. A word of caution: be aware that the tables wipe out any prefetches set on the view's queryset, so addressing this will likely need work on the column itself.

Since this in in the api part I think https://github.com/netbox-community/netbox/pull/15886 should be enough? Or did you also discover this to be an issue in the IPAddressListView view? @jeremystretch

@mulmat commented on GitHub (Apr 29, 2024): > @mulmat thanks, I've assigned this to you. A word of caution: be aware that the tables wipe out any prefetches set on the view's queryset, so addressing this will likely need work on the column itself. Since this in in the api part I think https://github.com/netbox-community/netbox/pull/15886 should be enough? Or did you also discover this to be an issue in the `IPAddressListView` view? @jeremystretch
Author
Owner

@jeremystretch commented on GitHub (Apr 29, 2024):

Sorry; I had it in my head that this was for the UI. Disregard my note above!

@jeremystretch commented on GitHub (Apr 29, 2024): Sorry; I had it in my head that this was for the UI. Disregard my note above!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9540