Repository: nautobot/nautobot

Issue:
Backport #5041 to 2.1 (`develop`)
### As ...

Cora - Coder

### I want ...

The performance enhancements made in #5041 to be available in Nautobot 2.1.x.

### So that ...

The benefits can be realized immediately instead of waiting for the Nautobot 2.2 release.

### I know this is done when...

#5041 is back-ported to `develop`, and `next` is updated as well to account for any resulting merge conflicts.

### Optional - Feature groups this request pertains to.

- [ ] Automation
- [ ] Circuits
- [ ] DCIM
- [ ] IPAM
- [ ] Misc (including Data Sources)
- [ ] Organization
- [ ] Plugins (and other Extensibility)
- [ ] Security (Secrets, etc)
- [ ] Image Management
- [ ] UI/UX
- [ ] Documentation
- [X] Other (not directly a platform feature)

### Database Changes

Migrations

### External Dependencies

None


Discussion:

Resolving patch:
diff --git a/nautobot/extras/migrations/0103_add_db_indexes_to_object_change.py b/nautobot/extras/migrations/0103_add_db_indexes_to_object_change.py
new file mode 100644
--- /dev/null
+++ b/nautobot/extras/migrations/0103_add_db_indexes_to_object_change.py
@@ -0,0 +1,37 @@
+# Generated by Django 3.2.23 on 2024-01-03 17:50
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("extras", "0102_set_null_objectchange_contenttype"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="objectchange",
+            name="user_name",
+            field=models.CharField(db_index=True, editable=False, max_length=150),
+        ),
+        migrations.AddIndex(
+            model_name="objectchange",
+            index=models.Index(fields=["changed_object_type", "changed_object_id"], name="changed_object_idx"),
+        ),
+        migrations.AddIndex(
+            model_name="objectchange",
+            index=models.Index(fields=["related_object_type", "related_object_id"], name="related_object_idx"),
+        ),
+        migrations.AddIndex(
+            model_name="objectchange",
+            index=models.Index(
+                fields=["user", "changed_object_type", "changed_object_id"], name="user_changed_object_idx"
+            ),
+        ),
+        migrations.AddIndex(
+            model_name="objectchange",
+            index=models.Index(
+                fields=["user_name", "changed_object_type", "changed_object_id"], name="user_name_changed_object_idx"
+            ),
+        ),
+    ]
diff --git a/nautobot/extras/models/change_logging.py b/nautobot/extras/models/change_logging.py
--- a/nautobot/extras/models/change_logging.py
+++ b/nautobot/extras/models/change_logging.py
@@ -80,7 +80,7 @@ class ObjectChange(BaseModel):
         blank=True,
         null=True,
     )
-    user_name = models.CharField(max_length=150, editable=False)
+    user_name = models.CharField(max_length=150, editable=False, db_index=True)
     request_id = models.UUIDField(editable=False, db_index=True)
     action = models.CharField(max_length=50, choices=ObjectChangeActionChoices)
     changed_object_type = models.ForeignKey(to=ContentType, on_delete=models.SET_NULL, null=True, related_name="+")
@@ -132,6 +132,22 @@ class Meta:
                 name="extras_objectchange_rtime_idx",
                 fields=["-time"],
             ),
+            models.Index(
+                name="changed_object_idx",
+                fields=["changed_object_type", "changed_object_id"],
+            ),
+            models.Index(
+                name="related_object_idx",
+                fields=["related_object_type", "related_object_id"],
+            ),
+            models.Index(
+                name="user_changed_object_idx",
+                fields=["user", "changed_object_type", "changed_object_id"],
+            ),
+            models.Index(
+                name="user_name_changed_object_idx",
+                fields=["user_name", "changed_object_type", "changed_object_id"],
+            ),
         ]
 
     def __str__(self):


Test patch: