Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit c3288a0

Browse files
authored
Add a proper index to Upload (aka ReportSession) (#441)
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.
1 parent b186b3c commit c3288a0

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 4.2.16 on 2024-11-28 12:26
2+
3+
from django.contrib.postgres.operations import AddIndexConcurrently
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
atomic = False
9+
10+
dependencies = [
11+
(
12+
"reports",
13+
"0034_remove_flake_created_at_remove_flake_updated_at",
14+
),
15+
]
16+
17+
operations = [
18+
AddIndexConcurrently(
19+
model_name="reportsession",
20+
index=models.Index(
21+
name="upload_report_type_idx",
22+
fields=["report_id", "upload_type"],
23+
),
24+
),
25+
]

shared/django_apps/reports/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,13 @@ class Meta:
199199
db_table = "reports_upload"
200200
indexes = [
201201
models.Index(
202-
fields=["report_id", "upload_type", "order_number"],
202+
name="upload_report_type_idx",
203+
fields=["report_id", "upload_type"],
204+
),
205+
# TODO(swatinem): remove the index below in a followup migration:
206+
models.Index(
203207
name="upload_index_id_type_number",
208+
fields=["report_id", "upload_type", "order_number"],
204209
),
205210
]
206211

0 commit comments

Comments
 (0)