Skip to content

Commit

Permalink
Closes #11790: Add SQL indexes for all generic foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Mar 17, 2023
1 parent 0ddd7b0 commit 36da925
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/release-notes/version-3.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ A new ASN range model has been introduced to facilitate the provisioning of new
* [#11694](https://github.com/netbox-community/netbox/issues/11694) - Remove obsolete `SmallTextarea` form widget
* [#11737](https://github.com/netbox-community/netbox/issues/11737) - `ChangeLoggedModel` now inherits `WebhooksMixin`
* [#11765](https://github.com/netbox-community/netbox/issues/11765) - Retire the `StaticSelect` and `StaticSelectMultiple` form widgets
* [#11790](https://github.com/netbox-community/netbox/issues/11790) - Add PostgreSQL indexes for all generic foreign key relationships to improve performance
21 changes: 21 additions & 0 deletions netbox/dcim/migrations/0172_gfk_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.1.7 on 2023-03-17 19:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dcim', '0171_devicetype_add_bridge'),
]

operations = [
migrations.AddIndex(
model_name='inventoryitem',
index=models.Index(fields=['component_type', 'component_id'], name='dcim_invent_compone_0560bb_idx'),
),
migrations.AddIndex(
model_name='inventoryitemtemplate',
index=models.Index(fields=['component_type', 'component_id'], name='dcim_invent_compone_77b5f8_idx'),
),
]
3 changes: 3 additions & 0 deletions netbox/dcim/models/device_component_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,9 @@ class Meta:
name='%(app_label)s_%(class)s_unique_device_type_parent_name'
),
)
indexes = (
models.Index(fields=('component_type', 'component_id')),
)

def instantiate(self, **kwargs):
parent = InventoryItem.objects.get(name=self.parent.name, **kwargs) if self.parent else None
Expand Down
3 changes: 3 additions & 0 deletions netbox/dcim/models/device_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,9 @@ class Meta:
name='%(app_label)s_%(class)s_unique_device_parent_name'
),
)
indexes = (
models.Index(fields=('component_type', 'component_id')),
)

def get_absolute_url(self):
return reverse('dcim:inventoryitem', kwargs={'pk': self.pk})
Expand Down
33 changes: 33 additions & 0 deletions netbox/extras/migrations/0091_gfk_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.1.7 on 2023-03-17 19:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('extras', '0090_objectchange_index_request_id'),
]

operations = [
migrations.AddIndex(
model_name='imageattachment',
index=models.Index(fields=['content_type', 'object_id'], name='extras_imag_content_94728e_idx'),
),
migrations.AddIndex(
model_name='journalentry',
index=models.Index(fields=['assigned_object_type', 'assigned_object_id'], name='extras_jour_assigne_76510f_idx'),
),
migrations.AddIndex(
model_name='objectchange',
index=models.Index(fields=['changed_object_type', 'changed_object_id'], name='extras_obje_changed_927fe5_idx'),
),
migrations.AddIndex(
model_name='objectchange',
index=models.Index(fields=['related_object_type', 'related_object_id'], name='extras_obje_related_bfcdef_idx'),
),
migrations.AddIndex(
model_name='stagedchange',
index=models.Index(fields=['object_type', 'object_id'], name='extras_stag_object__4734d5_idx'),
),
]
4 changes: 4 additions & 0 deletions netbox/extras/models/change_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class ObjectChange(models.Model):

class Meta:
ordering = ['-time']
indexes = (
models.Index(fields=('changed_object_type', 'changed_object_id')),
models.Index(fields=('related_object_type', 'related_object_id')),
)

def __str__(self):
return '{} {} {} by {}'.format(
Expand Down
6 changes: 6 additions & 0 deletions netbox/extras/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ class ImageAttachment(ChangeLoggedModel):

class Meta:
ordering = ('name', 'pk') # name may be non-unique
indexes = (
models.Index(fields=('content_type', 'object_id')),
)

def __str__(self):
if self.name:
Expand Down Expand Up @@ -564,6 +567,9 @@ class JournalEntry(CustomFieldsMixin, CustomLinksMixin, TagsMixin, ExportTemplat

class Meta:
ordering = ('-created',)
indexes = (
models.Index(fields=('assigned_object_type', 'assigned_object_id')),
)
verbose_name_plural = 'journal entries'

def __str__(self):
Expand Down
3 changes: 3 additions & 0 deletions netbox/extras/models/staging.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class StagedChange(ChangeLoggedModel):

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

def __str__(self):
action = self.get_action_display()
Expand Down
29 changes: 29 additions & 0 deletions netbox/ipam/migrations/0067_gfk_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.1.7 on 2023-03-17 19:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ipam', '0066_iprange_mark_utilized'),
]

operations = [
migrations.AddIndex(
model_name='fhrpgroupassignment',
index=models.Index(fields=['interface_type', 'interface_id'], name='ipam_fhrpgr_interfa_2acc3f_idx'),
),
migrations.AddIndex(
model_name='ipaddress',
index=models.Index(fields=['assigned_object_type', 'assigned_object_id'], name='ipam_ipaddr_assigne_890ab8_idx'),
),
migrations.AddIndex(
model_name='l2vpntermination',
index=models.Index(fields=['assigned_object_type', 'assigned_object_id'], name='ipam_l2vpnt_assigne_bac7ae_idx'),
),
migrations.AddIndex(
model_name='vlangroup',
index=models.Index(fields=['scope_type', 'scope_id'], name='ipam_vlangr_scope_t_9da557_idx'),
),
]
3 changes: 3 additions & 0 deletions netbox/ipam/models/fhrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class Meta:
name='%(app_label)s_%(class)s_unique_interface_group'
),
)
indexes = (
models.Index(fields=('interface_type', 'interface_id')),
)
verbose_name = 'FHRP group assignment'

def __str__(self):
Expand Down
3 changes: 3 additions & 0 deletions netbox/ipam/models/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,9 @@ class IPAddress(PrimaryModel):

class Meta:
ordering = ('address', 'pk') # address may be non-unique
indexes = (
models.Index(fields=('assigned_object_type', 'assigned_object_id')),
)
verbose_name = 'IP address'
verbose_name_plural = 'IP addresses'

Expand Down
5 changes: 4 additions & 1 deletion netbox/ipam/models/l2vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ class L2VPNTermination(NetBoxModel):

class Meta:
ordering = ('l2vpn',)
verbose_name = 'L2VPN termination'
constraints = (
models.UniqueConstraint(
fields=('assigned_object_type', 'assigned_object_id'),
name='ipam_l2vpntermination_assigned_object'
),
)
indexes = (
models.Index(fields=('assigned_object_type', 'assigned_object_id')),
)
verbose_name = 'L2VPN termination'

def __str__(self):
if self.pk is not None:
Expand Down
3 changes: 3 additions & 0 deletions netbox/ipam/models/vlans.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class Meta:
name='%(app_label)s_%(class)s_unique_scope_slug'
),
)
indexes = (
models.Index(fields=('scope_type', 'scope_id')),
)
verbose_name = 'VLAN group'
verbose_name_plural = 'VLAN groups'

Expand Down
17 changes: 17 additions & 0 deletions netbox/tenancy/migrations/0011_gfk_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.7 on 2023-03-17 19:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tenancy', '0010_tenant_relax_uniqueness'),
]

operations = [
migrations.AddIndex(
model_name='contactassignment',
index=models.Index(fields=['content_type', 'object_id'], name='tenancy_con_content_693ff4_idx'),
),
]
3 changes: 3 additions & 0 deletions netbox/tenancy/models/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class Meta:
name='%(app_label)s_%(class)s_unique_object_contact_role'
),
)
indexes = (
models.Index(fields=('content_type', 'object_id')),
)

def __str__(self):
if self.priority:
Expand Down

0 comments on commit 36da925

Please sign in to comment.