mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-11 21:10:29 +01:00
Poor performance in cable trace API calls: N+1 problem #10657
Closed
opened 2025-12-29 21:34:18 +01:00 by adam
·
19 comments
No Branch/Tag Specified
main
update-changelog-comments-docs
feature-removal-issue-type
20911-dropdown
20239-plugin-menu-classes-mutable-state
21097-graphql-id-lookups
feature
fix_module_substitution
20923-dcim-templates
20044-elevation-stuck-lightmode
feature-ip-prefix-link
v4.5-beta1-release
20068-import-moduletype-attrs
20766-fix-german-translation-code-literals
20378-del-script
7604-filter-modifiers-v3
circuit-swap
12318-case-insensitive-uniqueness
20637-improve-device-q-filter
20660-script-load
19724-graphql
20614-update-ruff
14884-script
02496-max-page
19720-macaddress-interface-generic-relation
19408-circuit-terminations-export-templates
20203-openapi-check
fix-19669-api-image-download
7604-filter-modifiers
19275-fixes-interface-bulk-edit
fix-17794-get_field_value_return_list
11507-show-aggregate-and-rir-on-api
9583-add_column_specific_search_field_to_tables
v4.5.0
v4.4.10
v4.4.9
v4.5.0-beta1
v4.4.8
v4.4.7
v4.4.6
v4.4.5
v4.4.4
v4.4.3
v4.4.2
v4.4.1
v4.4.0
v4.3.7
v4.4.0-beta1
v4.3.6
v4.3.5
v4.3.4
v4.3.3
v4.3.2
v4.3.1
v4.3.0
v4.2.9
v4.3.0-beta2
v4.2.8
v4.3.0-beta1
v4.2.7
v4.2.6
v4.2.5
v4.2.4
v4.2.3
v4.2.2
v4.2.1
v4.2.0
v4.1.11
v4.1.10
v4.1.9
v4.1.8
v4.2-beta1
v4.1.7
v4.1.6
v4.1.5
v4.1.4
v4.1.3
v4.1.2
v4.1.1
v4.1.0
v4.0.11
v4.0.10
v4.0.9
v4.1-beta1
v4.0.8
v4.0.7
v4.0.6
v4.0.5
v4.0.3
v4.0.2
v4.0.1
v4.0.0
v3.7.8
v3.7.7
v4.0-beta2
v3.7.6
v3.7.5
v4.0-beta1
v3.7.4
v3.7.3
v3.7.2
v3.7.1
v3.7.0
v3.6.9
v3.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r1
v1.0.3
1.0.0
Labels
Clear labels
beta
breaking change
complexity: high
complexity: low
complexity: medium
needs milestone
netbox
pending closure
plugin candidate
pull-request
severity: high
severity: low
severity: medium
status: accepted
status: backlog
status: blocked
status: duplicate
status: needs owner
status: needs triage
status: revisions needed
status: under review
topic: GraphQL
topic: Internationalization
topic: OpenAPI
topic: UI/UX
topic: cabling
topic: event rules
topic: htmx navigation
topic: industrialization
topic: migrations
topic: plugins
topic: scripts
topic: templating
topic: testing
type: bug
type: deprecation
type: documentation
type: feature
type: housekeeping
type: translation
Mirrored from GitHub Pull Request
Milestone
No items
No Milestone
Projects
Clear projects
No project
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: starred/netbox#10657
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Tishka17 on GitHub (Jan 16, 2025).
Originally assigned to: @Tishka17 on GitHub.
Deployment Type
Self-hosted
Triage priority
N/A
NetBox Version
v3.7, v4.2
Python Version
3.11
Steps to Reproduce
http://localhost:8000/api/dcim/interfaces/?device_id=3971&limit=100&offset=0
Expected Behavior
Data is loaded quite fast, <0.5 seconds. Number of db queries pretty small up to 10-20 per HTTP-request
Observed Behavior
Some requests are pretty slow. Up to 3 seconds.
130 sql queries are produced on 3.7 and 160 db queries on 4.2
CabledObjectModel.link_peershas N+1 problem: https://github.com/netbox-community/netbox/blob/v3.7.8/netbox/dcim/models/device_components.py#L192CablePath._get_pathhas N+1 problem as well: https://github.com/netbox-community/netbox/blob/v3.7.8/netbox/dcim/models/cables.py#L746I tried to comment out these methods and got only 20 db queries on the same http request
Additional info: n+1 problem should be eliminated
@bctiemann commented on GitHub (Jan 16, 2025):
It would be ideal if each of these identified points of improvement could be opened as a separate bug, so they can be individually fixed (i.e. if one is easier than another).
@jeremystretch commented on GitHub (Jan 17, 2025):
I think we need something more explicit here. Asserting that the application should perform faster is not sufficient justification for a bug report. @Tishka17 please extend your post above to cite the specific optimizations you believe are missing.
@Tishka17 commented on GitHub (Jan 17, 2025):
@jeremystretch sorry, I thought the "N+1 problem" words with links to corresponding functions are enough. What else can I add? For me it is a performance issue, but according to a code - it's just a technical bug with improper related data loading
@jeremystretch commented on GitHub (Jan 17, 2025):
Please detail the specific changes you are proposing.
@Tishka17 commented on GitHub (Jan 17, 2025):
I am proposing to fix code in a way, that it has no N+1 problem or lazy loading. I am not Django developer, so I am not familiar how it's technics like prefetch_related can be used in this specific case
@n-rodriguez commented on GitHub (Jan 20, 2025):
https://dev.to/herchila/how-to-avoid-n1-queries-in-django-tips-and-solutions-2ajo
My 2 cents
@Tishka17 commented on GitHub (Jan 20, 2025):
The problem is that some fields are calculated are not just generated based on model data, but explicitly do requests to a database.
I've tried to fix
link_peersreplacing.excludequery with filtering already loadedself.cable.terminationsbut it references thenGenericForeignKeyand we need its children in response..., so I did not succceded.@jsenecal commented on GitHub (Jan 20, 2025):
To answer to that specific example (prefetch w/ generic foreignkeys) there was an upstream issue to try to mitigate this (
https://code.djangoproject.com/ticket/33651) which has been implemented in Django 4.0.
Not sure if the netbox codebase uses that everywhere, but it is implemented in https://github.com/netbox-community/netbox/blob/main/netbox/utilities/fields.py#L74
@github-actions[bot] commented on GitHub (Jan 31, 2025):
This is a reminder that additional information is needed in order to further triage this issue. If the requested details are not provided, the issue will soon be closed automatically.
@Tishka17 commented on GitHub (Jan 31, 2025):
Really?
@bctiemann commented on GitHub (Feb 4, 2025):
@Tishka17 I do want to give some attention to this. There's a lot of work that's being prioritized for current and upcoming releases and things like this have to be tackled on an opportunistic basis.
We need to research @jsenecal 's comment and see whether
get_prefetch_querysetscan help in more places. If we can reproduce slowness in a specific area, and optimizations can be made piecemeal, they can be raised as individual issues/PRs which might be more actionable than an umbrella effort like this.@Tishka17 commented on GitHub (Feb 19, 2025):
I am trying to implement Prefetch for
path_objectsfield. Here is my experiment:Fieldclass https://github.com/Tishka17/django-generic-array/blob/f303550/genericarray/main/myfk.pyHow it works?
C.datafield as an array of arrays of pairs "type id", "model id". This would be later similar to current_nodes.Currently it is not yet finished, I need to handle some cases, but I'll try to bring it as a solution if it will be accepted.
Another question from me is regarding GenericPrefetch. We are still on Netbox 3.7 and GenericPrefetch is not supported on django 4.2 (which is used in Netbox 3.7). Did anyone try to update Django on that version of netbox?
@Tishka17 commented on GitHub (Feb 21, 2025):
Update on my draft:
It look, that we do not have more N+1 for Interfaces, though I need to test it more and cleanup all code.
https://github.com/Tishka17/netbox/pull/3/files
Some of this files are not needed for latest NetBox versions as they use actual Django, but there are more different fields, so I need to rework it. Please, have a look now and I will continue working and create a PR for actual NetBox as well.
@Tishka17 commented on GitHub (Feb 24, 2025):
The result on current moment:
I have a device with many connections and cables. I am doing a request http://localhost:8000/api/dcim/interfaces/?device_id=3926&limit=50
On master branch it generates 409
SELECTqueriesOn my branch it is 28
SELECTwith identical json outputhttps://github.com/netbox-community/netbox/pull/18719
@Tishka17 commented on GitHub (Feb 26, 2025):
@bctiemann did you get a chance to have a look on my PR? I do not know about your release plan, but we are interested in solving this issue. If you see any problems, I am eager to fix them
@Tishka17 commented on GitHub (Feb 28, 2025):
@bctiemann what information do you need from me to reopen and merge PR?
@bctiemann commented on GitHub (Mar 6, 2025):
I'm going to go ahead and accept this for backlog work; I don't want to lose track of the PR. Please go ahead and reopen it and we'll review it as resources permit.
@Tishka17 commented on GitHub (Apr 24, 2025):
I see a regression in
/interfacescall in latest release, so I sent a bunch of new fixes. Should I create new issue?@bctiemann commented on GitHub (Apr 24, 2025):
@Tishka17 Yes, please open a new issue, and target it specifically at the area(s) where you see problems. Please don't submit a PR until the issue has been triaged and accepted. Thanks!