Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API optimizations for tagged objects #2438

Closed
remyleone opened this issue Sep 17, 2018 · 8 comments
Closed

API optimizations for tagged objects #2438

remyleone opened this issue Sep 17, 2018 · 8 comments
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application

Comments

@remyleone
Copy link
Contributor

Environment

  • Python version: 3.7.0
  • NetBox version: 2.4.5-dev

Steps to Reproduce

  • Insert over 4000 devices in NetBox and fetch a device listing.
  • When you open the debug toolbar you can observe that a large number of SQL queries are performed.

Expected Behavior

I expected the number of SQL queries to not grow as the number of devices grows

Observed Behavior

The amount of SQL queries increased

#2437

@Armadill0
Copy link

Do you have custom fields for your devices? Afaik each custom field generates another query for each device into the custom fields table. This is why the usage of custom fields should be limited.

@remyleone
Copy link
Contributor Author

remyleone commented Sep 21, 2018

I think @Anthony25 can answer that but I don't think we got custom fields. Also with the fix provided ( #2437 ) we reduced the query load with the same data.

@sdktr
Copy link
Contributor

sdktr commented Sep 24, 2018

/offtopic: hi @sieben , could you provide some help to get this 'debug toolbar' to show up? I'm assuming that the debug=True flag should do the trick with the buildin django-debug plugin, but that alone doesn't change my web interface at all. How did you troubleshoot this?

@aruhier
Copy link

aruhier commented Sep 24, 2018

We do have custom fields, but it's the tags that are not prefetched which is causing the issue. As the default behavior of django is to fetch the foreign objects JIT, it has to do 1 query per item to select the associated tags. We managed to fix that for the devices in our PR, but it has to be done for every model that uses tags.

Edit: FYI, we managed to decrease the number of queries from ~1000 to 10, with 1000 devices listed from the API

@jeremystretch
Copy link
Member

Have you done any benchmarking before and after the change? For example, populate 1000 devices with 10 tags each. I'm curious what the overall response time is before and after the change to prefetch_related.

@jeremystretch jeremystretch added the status: under review Further discussion is needed to determine this issue's scope and/or implementation label Sep 26, 2018
@sdktr
Copy link
Contributor

sdktr commented Sep 27, 2018

I benchmarked on the API call api/ipam/ip-addresses/?limit=1000&offset=0;

Without the 'tags' being prefetched in the IPAddressViewSet on my setup it takes 15000ms to fetch 1000 ipaddresses. The amount of SQL queries (measured using django-debug-toolbar) is 1009 taking 6029ms total.

Fixing the IPAddressViewSet to include 'tags' in the prefetch these numbers go down to
Total load time of 2033ms, containing 10 SQL queries (which take 230ms total)

prefetch_related(
        'nat_outside', 'tags'
)

File patched to gain these improvements: https://github.com/digitalocean/netbox/blob/bcf22831e2cc8a0c3021f0a73afd0ddc4a1b6b8d/netbox/ipam/api/views.py#L248
Note that in my tests there are no actual tags assigned to the ipaddresses.

@remyleone
Copy link
Contributor Author

@sdktr Thank you very much for the benchmark :) Could you see other places where we should add the tags? I can upgrade #2437 to fix it in several places.

@jeremystretch jeremystretch changed the title Large number of SQL queries with device listing API optimizations for tagged objects Sep 28, 2018
@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Sep 28, 2018
@aruhier
Copy link

aruhier commented Sep 28, 2018

@sdktr: thanks a lot for the benchmark!

@lock lock bot locked as resolved and limited conversation to collaborators Jan 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants