From 3af2101dc6b708b976cc58826a29d608e1467cd4 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Thu, 28 Nov 2024 13:33:11 +0100 Subject: [PATCH] Add a proper index to `Upload` (aka `ReportSession`) This adds an index covering `report_id` and `upload_type`, in that order. The most frequent queries use `report_id`, and some further index by `upload_type`. So this newly added index will cover those two frequent queries. Indexing additionally on `order_number` is not necessary. While we do run queries using all three fields, those are not as frequent, and they should be served by the newly added index as well. One more argument against indexing on the `order_number` is that we `UPDATE` this field when processing an upload, which means we would have to touch and update the index on such operation as well, which we could also avoid. --- .../migrations/0035_upload_indices_part1.py | 25 +++++++++++++++++++ shared/django_apps/reports/models.py | 7 +++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 shared/django_apps/reports/migrations/0035_upload_indices_part1.py diff --git a/shared/django_apps/reports/migrations/0035_upload_indices_part1.py b/shared/django_apps/reports/migrations/0035_upload_indices_part1.py new file mode 100644 index 000000000..4bb5e6671 --- /dev/null +++ b/shared/django_apps/reports/migrations/0035_upload_indices_part1.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.16 on 2024-11-28 12:26 + +from django.contrib.postgres.operations import AddIndexConcurrently +from django.db import migrations, models + + +class Migration(migrations.Migration): + atomic = False + + dependencies = [ + ( + "reports", + "0034_remove_flake_created_at_remove_flake_updated_at", + ), + ] + + operations = [ + AddIndexConcurrently( + model_name="reportsession", + index=models.Index( + name="upload_report_type_idx", + fields=["report_id", "upload_type"], + ), + ), + ] diff --git a/shared/django_apps/reports/models.py b/shared/django_apps/reports/models.py index 304249fdb..707809571 100644 --- a/shared/django_apps/reports/models.py +++ b/shared/django_apps/reports/models.py @@ -199,8 +199,13 @@ class Meta: db_table = "reports_upload" indexes = [ models.Index( - fields=["report_id", "upload_type", "order_number"], + name="upload_report_type_idx", + fields=["report_id", "upload_type"], + ), + # TODO(swatinem): remove the index below in a followup migration: + models.Index( name="upload_index_id_type_number", + fields=["report_id", "upload_type", "order_number"], ), ]