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

Commit 07b39b2

Browse files
authored
Clean up existing indices on Upload (aka ReportSession) (#442)
This in particular cleans up the existing indices that exist in the production DB but not in django: We drop these indices for the following reasons: - `upload_index_id_type_number`: This matches the above state migration, and it looks like the index does not actually exist in the production DB. The following indices exist in the production DB, but not in django state/migrations: - `reports_upload_order_number_idx`: We never query by the `order_number` alone, so this index is likely unused. - `reports_upload_report_id_f6b4ffae`: Queries on `report_id` should already been covered by the newly added index on `report_id`+`upload_type`. - `reports_upload_report_id_upload_type_index_ccnew`: This seems to be a manually added variant of the `upload_report_type_idx` index and is thus duplicated. - `reports_upload_report_id_upload_type_order_number_index`: This is the same as the above, except with an additional `order_number`. We do use it in queries, but I doubt the index pulls its weight, as the `order_number` changes quite frequently so the index is costly to maintain.
1 parent 98a9a16 commit 07b39b2

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Generated by Django 4.2.16 on 2024-11-28 12:37
2+
3+
from django.contrib.postgres.operations import RemoveIndexConcurrently
4+
from django.db import migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
atomic = False
9+
10+
dependencies = [
11+
("reports", "0035_upload_indices_part1"),
12+
]
13+
14+
operations = [
15+
# We drop these indices for the following reasons:
16+
# - `upload_index_id_type_number`: This matches the above state migration,
17+
# and it looks like the index does not actually exist in the production DB.
18+
RemoveIndexConcurrently(
19+
model_name="reportsession",
20+
name="upload_index_id_type_number",
21+
),
22+
# The following indices exist in the production DB, but not in django state/migrations:
23+
# - `reports_upload_order_number_idx`:
24+
# We never query by the `order_number` alone, so this index is likely unused.
25+
# - `reports_upload_report_id_f6b4ffae`: Queries on `report_id` should already been covered by the
26+
# newly added index on `report_id`+`upload_type`.
27+
# - `reports_upload_report_id_upload_type_index_ccnew`:
28+
# This seems to be a manually added variant of the `upload_report_type_idx` index and is thus duplicated.
29+
# - `reports_upload_report_id_upload_type_order_number_index`:
30+
# This is the same as the above, except with an additional `order_number`.
31+
# We do use it in queries, but I doubt the index pulls its weight, as the `order_number` changes quite
32+
# frequently so the index is costly to maintain.
33+
*(
34+
# Interestingly, we have to run these in individual `RunSQL` statements, otherwise django would create a
35+
# transaction around them, which is not supported for these `DROP INDEX CONCURRENTLY` statements.
36+
migrations.RunSQL(
37+
sql=f"""DROP INDEX CONCURRENTLY IF EXISTS "{idx}";""",
38+
hints={"tables": ["reports_upload"]},
39+
)
40+
for idx in [
41+
"reports_upload_order_number_idx",
42+
"reports_upload_report_id_f6b4ffae",
43+
"reports_upload_report_id_upload_type_index_ccnew",
44+
"reports_upload_report_id_upload_type_order_number_index",
45+
]
46+
),
47+
]

shared/django_apps/reports/models.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,6 @@ class Meta:
202202
name="upload_report_type_idx",
203203
fields=["report_id", "upload_type"],
204204
),
205-
# TODO(swatinem): remove the index below in a followup migration:
206-
models.Index(
207-
name="upload_index_id_type_number",
208-
fields=["report_id", "upload_type", "order_number"],
209-
),
210205
]
211206

212207
@property

0 commit comments

Comments
 (0)