Skip to content

Commit

Permalink
Clean up existing indices on Upload (aka ReportSession)
Browse files Browse the repository at this point in the history
This in particular cleans up the existing indices that exist in the production DB but not in django:

- `reports_upload_order_number_upload_type_report_id_index`:
  This index actually has the *reverse* order that the one above, which means that frequent updates
  to the `order_number` lead to a lot of costly index updates.
- `reports_upload_report_id_f6b4ffae`: Queries on `report_id` should already been covered by the
   newly added index on `report_id`+`upload_type`.
  • Loading branch information
Swatinem committed Jan 8, 2025
1 parent c3288a0 commit ca4ae14
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
40 changes: 40 additions & 0 deletions shared/django_apps/reports/migrations/0036_upload_indices_part2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.2.16 on 2024-11-28 12:37

from django.db import migrations


class Migration(migrations.Migration):
atomic = False

dependencies = [
("reports", "0035_upload_indices_part1"),
]

operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveIndex(
model_name="reportsession",
name="upload_index_id_type_number",
),
],
database_operations=[
# 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_upload_type_report_id_index`:
# This index actually has the *reverse* order that the one above, which means that frequent updates
# to the `order_number` lead to a lot of costly index updates.
# - `reports_upload_report_id_f6b4ffae`: Queries on `report_id` should already been covered by the
# newly added index on `report_id`+`upload_type`.
migrations.RunSQL(
sql="""
DROP INDEX CONCURRENTLY IF EXISTS "upload_index_id_type_number";
DROP INDEX CONCURRENTLY IF EXISTS "reports_upload_order_number_upload_type_report_id_index";
DROP INDEX CONCURRENTLY IF EXISTS "reports_upload_report_id_f6b4ffae";
"""
),
],
)
]
5 changes: 0 additions & 5 deletions shared/django_apps/reports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ class Meta:
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"],
),
]

@property
Expand Down

0 comments on commit ca4ae14

Please sign in to comment.