Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up existing indices on Upload (aka ReportSession) #442

Open
wants to merge 1 commit into
base: swatinem/upload-idx-part1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading