Skip to content

Commit

Permalink
Add a proper index to Upload (aka ReportSession)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Swatinem committed Jan 8, 2025
1 parent b186b3c commit 3af2101
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
25 changes: 25 additions & 0 deletions shared/django_apps/reports/migrations/0035_upload_indices_part1.py
Original file line number Diff line number Diff line change
@@ -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"],
),
),
]
7 changes: 6 additions & 1 deletion shared/django_apps/reports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
),
]

Expand Down

0 comments on commit 3af2101

Please sign in to comment.