From 8f271151a71c8af07f1efe0a744c2d652d702597 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 2 Aug 2023 08:45:56 -0400 Subject: [PATCH] Closes #11519: Add a SQL index for IPAddress host value --- .../migrations/0067_ipaddress_index_host.py | 20 +++++++++++++++++++ netbox/ipam/models/ip.py | 5 +++++ 2 files changed, 25 insertions(+) create mode 100644 netbox/ipam/migrations/0067_ipaddress_index_host.py diff --git a/netbox/ipam/migrations/0067_ipaddress_index_host.py b/netbox/ipam/migrations/0067_ipaddress_index_host.py new file mode 100644 index 0000000000..2383fd9aa2 --- /dev/null +++ b/netbox/ipam/migrations/0067_ipaddress_index_host.py @@ -0,0 +1,20 @@ +# Generated by Django 4.1.10 on 2023-08-02 12:43 + +from django.db import migrations, models +import django.db.models.functions.comparison +import ipam.fields +import ipam.lookups + + +class Migration(migrations.Migration): + + dependencies = [ + ('ipam', '0066_iprange_mark_utilized'), + ] + + operations = [ + migrations.AddIndex( + model_name='ipaddress', + index=models.Index(django.db.models.functions.comparison.Cast(ipam.lookups.Host('address'), output_field=ipam.fields.IPAddressField()), name='ipam_ipaddress_host'), + ), + ] diff --git a/netbox/ipam/models/ip.py b/netbox/ipam/models/ip.py index eba71bb45b..174bcd672a 100644 --- a/netbox/ipam/models/ip.py +++ b/netbox/ipam/models/ip.py @@ -4,6 +4,7 @@ from django.core.exceptions import ValidationError from django.db import models from django.db.models import F +from django.db.models.functions import Cast from django.urls import reverse from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ @@ -11,6 +12,7 @@ from ipam.choices import * from ipam.constants import * from ipam.fields import IPNetworkField, IPAddressField +from ipam.lookups import Host from ipam.managers import IPAddressManager from ipam.querysets import PrefixQuerySet from ipam.validators import DNSValidator @@ -778,6 +780,9 @@ class IPAddress(PrimaryModel): class Meta: ordering = ('address', 'pk') # address may be non-unique + indexes = [ + models.Index(Cast(Host('address'), output_field=IPAddressField()), name='ipam_ipaddress_host'), + ] verbose_name = 'IP address' verbose_name_plural = 'IP addresses'