Ensure database indexes are created for all generic foreign keys #7662

Closed
opened 2025-12-29 20:26:35 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Feb 20, 2023).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

Audit all NetBox models and ensure that appropriate PostgreSQL indexes have been created for the combination of content type and object ID wherever generic foreign keys are in use.

Justification

This was prompted by #11775, which identified the absence of an index for cached search values as a performance detractor. Per the Django docs:

Unlike for the ForeignKey, a database index is not automatically created on the GenericForeignKey, so it’s recommended that you use Meta.indexes to add your own multiple column index.

Originally created by @jeremystretch on GitHub (Feb 20, 2023). Originally assigned to: @jeremystretch on GitHub. ### Proposed Changes Audit all NetBox models and ensure that appropriate PostgreSQL indexes have been created for the combination of content type and object ID wherever generic foreign keys are in use. ### Justification This was prompted by #11775, which identified the absence of an index for cached search values as a performance detractor. Per [the Django docs](https://docs.djangoproject.com/en/4.1/ref/contrib/contenttypes/#django.contrib.contenttypes.fields.GenericForeignKey): > Unlike for the [ForeignKey](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.ForeignKey), a database index is not automatically created on the [GenericForeignKey](https://docs.djangoproject.com/en/4.1/ref/contrib/contenttypes/#django.contrib.contenttypes.fields.GenericForeignKey), so it’s recommended that you use [Meta.indexes](https://docs.djangoproject.com/en/4.1/ref/models/options/#django.db.models.Options.indexes) to add your own multiple column index.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 20:26:35 +01:00
adam closed this issue 2025-12-29 20:26:35 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7662