-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Custom field accessor database performance #3185
Comments
Related to #3190 |
First, I should point out that There are a few opportunities for improvement here. First, we have this line in
Here, we're retrieving all applicable custom fields for the current instance. (This is needed to ensure we also represent fields which have no value assigned.) This line is called each time Further down, we have this line:
This forfeits any benefit from having pre-fetched values on the parent queryset. We should be referencing And finally there's the
This means that accessing e.g. |
Environment
Use Case
One of the Netbox reports that we've written, needs to basically look at multiple Devices, and for each of them check various fields against another database for consistency. Some of them are Netbox's built-in fields, some of them are custom fields. Basically, semi-realistic example:
Observed behavior
Netbox issues hundreds of database queries, making this inefficient, slow and tough on the database. The
.cf()
accessor seems to be the culprit, as it's making a lot of queries for each of the ContentTypes etc., for each of those Device objects.Proposed behavior
We've worked around that, with this piece of code:
This reduces the hotpath queries to just 1 big one, making this code path about 20-30x more efficient.
I think this could be generalized, and potentially replace the
.cf()
accessor.I'm unaware of the history of
.cf()
, and it is a complicated piece of code, so I'm a bit hesitant diving in and submitting a PR there. Would love some guidance :)The text was updated successfully, but these errors were encountered: