From 94ce34d9aad35dac6079b3a2e61fbe8bd4a5df27 Mon Sep 17 00:00:00 2001 From: ds283 Date: Wed, 27 Mar 2024 15:37:10 +0000 Subject: [PATCH] Add index to liveproject_id in PopularityRecord - attempting to speed up queries that involve joins to PopularityRecord --- app/models.py | 2 +- ...84579fb_add_index_to_liveproject_id_in_.py | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/12c4584579fb_add_index_to_liveproject_id_in_.py diff --git a/app/models.py b/app/models.py index 6e033d69..7c320bbe 100644 --- a/app/models.py +++ b/app/models.py @@ -12349,7 +12349,7 @@ class PopularityRecord(db.Model): id = db.Column(db.Integer(), primary_key=True) # tag LiveProject to which this record applies - liveproject_id = db.Column(db.Integer(), db.ForeignKey("live_projects.id")) + liveproject_id = db.Column(db.Integer(), db.ForeignKey("live_projects.id"), index=True) liveproject = db.relationship( "LiveProject", uselist=False, backref=db.backref("popularity_data", lazy="dynamic", cascade="all, delete, delete-orphan") ) diff --git a/migrations/versions/12c4584579fb_add_index_to_liveproject_id_in_.py b/migrations/versions/12c4584579fb_add_index_to_liveproject_id_in_.py new file mode 100644 index 00000000..1c539cb3 --- /dev/null +++ b/migrations/versions/12c4584579fb_add_index_to_liveproject_id_in_.py @@ -0,0 +1,32 @@ +"""Add index to liveproject_id in PopularityRecord + +Revision ID: 12c4584579fb +Revises: 95dce27755c3 +Create Date: 2024-03-27 15:35:47.978259 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '12c4584579fb' +down_revision = '95dce27755c3' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('popularity_record', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_popularity_record_liveproject_id'), ['liveproject_id'], unique=False) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('popularity_record', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_popularity_record_liveproject_id')) + + # ### end Alembic commands ###