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`.
- `reports_upload_created_at_index`: We never query by `created_at` directly. The only place where we
  use this is in an `ORDER BY` that is already covered by the `report_id`.
  • Loading branch information
Swatinem committed Nov 28, 2024
1 parent 04713aa commit 20fc6fa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
43 changes: 43 additions & 0 deletions shared/django_apps/reports/migrations/0035_upload_indices_part2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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", "0034_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`.
# - `reports_upload_created_at_index`: We never query by `created_at` directly. The only place where we
# use this is in an `ORDER BY` that is already covered by the `report_id`.
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";
DROP INDEX CONCURRENTLY IF EXISTS "reports_upload_created_at_index";
"""
),
],
)
]
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 20fc6fa

Please sign in to comment.