Add more indexes and optimize usage

This commit is contained in:
2026-01-18 14:45:39 +01:00
parent ec470ac0a7
commit 44a965eb62
8 changed files with 324 additions and 3 deletions

View File

@@ -0,0 +1,65 @@
# Generated by Django 6.0.1 on 2026-01-18 13:42
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
(
"metadata",
"0027_company_company_slug_idx_company_company_country_idx_and_more",
),
("roster", "0040_alter_rollingstock_decoder_interface_order"),
]
operations = [
migrations.AddIndex(
model_name="rollingclass",
index=models.Index(fields=["company"], name="roster_rc_company_idx"),
),
migrations.AddIndex(
model_name="rollingclass",
index=models.Index(fields=["type"], name="roster_rc_type_idx"),
),
migrations.AddIndex(
model_name="rollingclass",
index=models.Index(
fields=["company", "identifier"], name="roster_rc_co_ident_idx"
),
),
migrations.AddIndex(
model_name="rollingstock",
index=models.Index(fields=["published"], name="roster_published_idx"),
),
migrations.AddIndex(
model_name="rollingstock",
index=models.Index(fields=["featured"], name="roster_featured_idx"),
),
migrations.AddIndex(
model_name="rollingstock",
index=models.Index(
fields=["item_number_slug"], name="roster_item_slug_idx"
),
),
migrations.AddIndex(
model_name="rollingstock",
index=models.Index(fields=["road_number_int"], name="roster_road_num_idx"),
),
migrations.AddIndex(
model_name="rollingstock",
index=models.Index(
fields=["published", "featured"], name="roster_pub_feat_idx"
),
),
migrations.AddIndex(
model_name="rollingstock",
index=models.Index(
fields=["manufacturer", "item_number_slug"], name="roster_mfr_item_idx"
),
),
migrations.AddIndex(
model_name="rollingstock",
index=models.Index(fields=["scale"], name="roster_scale_idx"),
),
]

View File

@@ -38,6 +38,14 @@ class RollingClass(models.Model):
ordering = ["company", "identifier"]
verbose_name = "Class"
verbose_name_plural = "Classes"
indexes = [
models.Index(fields=["company"], name="roster_rc_company_idx"),
models.Index(fields=["type"], name="roster_rc_type_idx"),
models.Index(
fields=["company", "identifier"],
name="roster_rc_co_ident_idx", # Shortened to fit 30 char limit
),
]
def __str__(self):
return "{0} {1}".format(self.company, self.identifier)
@@ -125,6 +133,30 @@ class RollingStock(BaseModel):
class Meta:
ordering = ["rolling_class", "road_number_int"]
verbose_name_plural = "Rolling stock"
indexes = [
# Index for published/featured filtering
models.Index(fields=["published"], name="roster_published_idx"),
models.Index(fields=["featured"], name="roster_featured_idx"),
# Index for item number searches
models.Index(
fields=["item_number_slug"], name="roster_item_slug_idx"
),
# Index for road number searches and ordering
models.Index(
fields=["road_number_int"], name="roster_road_num_idx"
),
# Composite index for common filtering patterns
models.Index(
fields=["published", "featured"], name="roster_pub_feat_idx"
),
# Composite index for manufacturer+item_number lookups
models.Index(
fields=["manufacturer", "item_number_slug"],
name="roster_mfr_item_idx",
),
# Index for scale filtering
models.Index(fields=["scale"], name="roster_scale_idx"),
]
def __str__(self):
return "{0} {1}".format(self.rolling_class, self.road_number)