diff --git a/reversion/migrations/0002_add_index_on_version_for_content_type_and_db.py b/reversion/migrations/0002_add_index_on_version_for_content_type_and_db.py new file mode 100644 index 00000000..75a5a691 --- /dev/null +++ b/reversion/migrations/0002_add_index_on_version_for_content_type_and_db.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2.6 on 2021-08-16 18:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('reversion', '0001_squashed_0004_auto_20160611_1202'), + ] + + operations = [ + migrations.AddIndex( + model_name='version', + index=models.Index(fields=['content_type', 'db'], name='reversion_v_content_f95daf_idx'), + ), + ] diff --git a/reversion/models.py b/reversion/models.py index 5813654c..267c0cd8 100644 --- a/reversion/models.py +++ b/reversion/models.py @@ -189,7 +189,8 @@ def get_deleted(self, model, model_db=None): latest_pk=models.Max("pk") ).order_by().values_list("latest_pk", flat=True) # Perform the subquery. - return self.filter(pk__in=subquery) + # Filter by model to reduce query execution time. + return self.get_for_model(model, model_db=model_db).filter(pk__in=subquery) def get_unique(self): last_key = None @@ -328,6 +329,11 @@ class Meta: unique_together = ( ("db", "content_type", "object_id", "revision"), ) + indexes = ( + models.Index( + fields=["content_type", "db"] + ), + ) ordering = ("-pk",)