From c7680480b30809f2d6e4d7fd85666493ef5e5d05 Mon Sep 17 00:00:00 2001 From: Deborah Kaplan Date: Wed, 15 Jan 2025 21:47:20 +0000 Subject: [PATCH] feat: performance tuning credentials admin this adds a single index to UserCredential.credential_id. You would think this would be implicitly indexed by the presence of a unique_together, but it isn't. I don't want to get overly exuberant with indices because we have a lot of costly writes during grading already, but I can see using SQL `EXPLAIN` that making this change does convert the django admin page from `using filesort` to `using index`, so theoretically this should speed it up. If it doesn't, I will try a different approach. --- .../0031_usercredential_explicit_index.py | 18 ++++++++++++++++++ credentials/apps/credentials/models.py | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 credentials/apps/credentials/migrations/0031_usercredential_explicit_index.py diff --git a/credentials/apps/credentials/migrations/0031_usercredential_explicit_index.py b/credentials/apps/credentials/migrations/0031_usercredential_explicit_index.py new file mode 100644 index 000000000..68e32178c --- /dev/null +++ b/credentials/apps/credentials/migrations/0031_usercredential_explicit_index.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.17 on 2025-01-15 21:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("credentials", "0030_revoke_certificates_management_command"), + ] + + operations = [ + migrations.AlterField( + model_name="usercredential", + name="credential_id", + field=models.PositiveIntegerField(db_index=True), + ), + ] diff --git a/credentials/apps/credentials/models.py b/credentials/apps/credentials/models.py index 85e1dac9e..310b346c8 100644 --- a/credentials/apps/credentials/models.py +++ b/credentials/apps/credentials/models.py @@ -172,7 +172,7 @@ class UserCredential(TimeStampedModel): limit_choices_to={"model__in": ("coursecertificate", "programcertificate", "credlybadgetemplate")}, on_delete=models.CASCADE, ) - credential_id = models.PositiveIntegerField() + credential_id = models.PositiveIntegerField(db_index=True) credential = GenericForeignKey("credential_content_type", "credential_id") username = models.CharField(max_length=255, db_index=True)