From cffee2a00730d8428e228100a6717422c6de11ab Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Sun, 29 Dec 2024 15:07:41 +0200 Subject: [PATCH] Speed-up opening the authentication.authtoken admin by indexing `created_at` column The reason is the default ORDER BY `created_at` , which makes a sequence scan on the whole table. On the local database the difference is significant with and without that index: before: Execution Time: 175.196 ms after: Execution Time: 0.562 ms --- ...authtoken_authenticat_created_ee68f9_idx.py | 18 ++++++++++++++++++ .../qfieldcloud/authentication/models.py | 1 + 2 files changed, 19 insertions(+) create mode 100644 docker-app/qfieldcloud/authentication/migrations/0003_authtoken_authenticat_created_ee68f9_idx.py diff --git a/docker-app/qfieldcloud/authentication/migrations/0003_authtoken_authenticat_created_ee68f9_idx.py b/docker-app/qfieldcloud/authentication/migrations/0003_authtoken_authenticat_created_ee68f9_idx.py new file mode 100644 index 000000000..554c744a3 --- /dev/null +++ b/docker-app/qfieldcloud/authentication/migrations/0003_authtoken_authenticat_created_ee68f9_idx.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.16 on 2024-12-29 12:55 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("authentication", "0002_alter_authtoken_client_type"), + ] + + operations = [ + migrations.AddIndex( + model_name="authtoken", + index=models.Index( + fields=["created_at"], name="authenticat_created_ee68f9_idx" + ), + ), + ] diff --git a/docker-app/qfieldcloud/authentication/models.py b/docker-app/qfieldcloud/authentication/models.py index c9125be6c..dd069fec5 100644 --- a/docker-app/qfieldcloud/authentication/models.py +++ b/docker-app/qfieldcloud/authentication/models.py @@ -90,6 +90,7 @@ class Meta: verbose_name = _("Token") verbose_name_plural = _("Tokens") ordering = ("-created_at",) + indexes = (models.Index(fields=["created_at"]),) def __str__(self): return self.key