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

Add index on extras_cachedvalue.object_id for better update performance with many interfaces #14966

Closed
arjanhak opened this issue Jan 29, 2024 · 1 comment · Fixed by #15199
Closed
Assignees
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application

Comments

@arjanhak
Copy link

NetBox version

v3.7.0

Feature type

Data model extension

Proposed functionality

Add an index to the object_id column on the extras_cachedvalue table.

This was suggested before in #11775 but for inserting new interfaces, and that was solved in #11785 by skipping the clearing of the cache on inserts, but that logic does of course not apply to updating interfaces.
Then there was an implementation of indexes for all generic foreign keys in #11790, but that did not include this object_id as it is something that can refer to multiple tables and is therefore not a formal foreign key.

Use case

When there are many interfaces (we are currently working with 300k+, times 7 attributes gives 2.2 million rows in the extras_cachedvalue table), updating (many of) them takes a lot of time. Looking at the PostgreSQL stats, this is the query that takes a lot of time:

DELETE FROM "extras_cachedvalue" WHERE ("extras_cachedvalue"."object_id" = 268011 AND "extras_cachedvalue"."object_type_id" = 38)

where in this instance 38 is the ID of dcim.interface, and that object_id is just an example.

Adding the suggested index made a big improvement in the time it takes to process the updates.

Database changes

CREATE INDEX extras_cachedvalue_object_id_TBD ON extras_cachedvalue USING btree(object_id);

External dependencies

No response

@arjanhak arjanhak added the type: feature Introduction of new functionality to the application label Jan 29, 2024
@jeremystretch
Copy link
Member

Then there was an implementation of indexes for all generic foreign keys in #11790, but that did not include this object_id as it is something that can refer to multiple tables and is therefore not a formal foreign key.

That's what "generic" foreign key means in Django vernacular: a foreign key that can refer to multiple object types. This should have been implemented under #11790 as you point out, and more recently under #14436 for v3.7. I suspect it was missed because CachedValue is using our custom RestrictedGenericForeignKey field instead of the normal GenericForeignKey, so it escaped both audits.

CREATE INDEX extras_cachedvalue_object_id_TBD ON extras_cachedvalue USING btree(object_id);

We should create an index for both the object type and object ID fields, as we've done for other GFKs, because both are needed to query a record by a related object. This is done under the model's Meta class:

class Meta:
    indexes = (
        models.Index(fields=('object_type', 'object_id')),
    )

Thanks for the report!

@jeremystretch jeremystretch added the status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation label Jan 29, 2024
@abhi1693 abhi1693 self-assigned this Jan 29, 2024
@abhi1693 abhi1693 added status: accepted This issue has been accepted for implementation and removed status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels Jan 29, 2024
abhi1693 added a commit that referenced this issue Feb 20, 2024
jeremystretch added a commit that referenced this issue Feb 21, 2024
* added index on cachevalue #14966

* Update netbox/extras/models/search.py

Co-authored-by: Jeremy Stretch <[email protected]>

* fixed migration

---------

Co-authored-by: Jeremy Stretch <[email protected]>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 22, 2024
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.

3 participants