Переглянути джерело

Closes #22102: Add a GIN index on CablePath to optimize filtering of cable paths by node (#22144)

Jeremy Stretch 2 тижнів тому
батько
коміт
f2187ceb8f

+ 15 - 0
netbox/dcim/migrations/0234_cablepath_nodes_index.py

@@ -0,0 +1,15 @@
+import django.contrib.postgres.indexes
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ('dcim', '0233_device_render_config_permission'),
+    ]
+
+    operations = [
+        migrations.AddIndex(
+            model_name='cablepath',
+            index=django.contrib.postgres.indexes.GinIndex(fields=['_nodes'], name='dcim_cablep__nodes_b23b96_gin'),
+        ),
+    ]

+ 6 - 0
netbox/dcim/models/cables.py

@@ -5,6 +5,7 @@ from collections import Counter
 from django.contrib.contenttypes.fields import GenericForeignKey
 from django.contrib.contenttypes.fields import GenericForeignKey
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.postgres.fields import ArrayField
 from django.contrib.postgres.fields import ArrayField
+from django.contrib.postgres.indexes import GinIndex
 from django.core.exceptions import ValidationError
 from django.core.exceptions import ValidationError
 from django.core.validators import MaxValueValidator, MinValueValidator
 from django.core.validators import MaxValueValidator, MinValueValidator
 from django.db import models
 from django.db import models
@@ -730,6 +731,11 @@ class CablePath(models.Model):
     _netbox_private = True
     _netbox_private = True
 
 
     class Meta:
     class Meta:
+        indexes = (
+            # GIN index supports @> operator used by `_nodes__contains` lookups,
+            # which fire on every cable/termination delete and path retrace.
+            GinIndex(fields=('_nodes',)),
+        )
         verbose_name = _('cable path')
         verbose_name = _('cable path')
         verbose_name_plural = _('cable paths')
         verbose_name_plural = _('cable paths')