From b00d44ddc1d43b1a775cae670d9f5545884fea97 Mon Sep 17 00:00:00 2001 From: Lephe Date: Wed, 7 Jul 2021 16:30:21 +0200 Subject: [PATCH] model: index comments by thread for faster lookup --- app/models/comment.py | 2 +- .../0abd1c81e3aa_index_coments_by_thread.py | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/0abd1c81e3aa_index_coments_by_thread.py diff --git a/app/models/comment.py b/app/models/comment.py index e9834d1..e8e0882 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -15,7 +15,7 @@ class Comment(Post): # Parent thread thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'), - nullable=False) + nullable=False, index=True) thread = db.relationship('Thread', backref=backref('comments', lazy='dynamic'), foreign_keys=thread_id) diff --git a/migrations/versions/0abd1c81e3aa_index_coments_by_thread.py b/migrations/versions/0abd1c81e3aa_index_coments_by_thread.py new file mode 100644 index 0000000..d3bbc63 --- /dev/null +++ b/migrations/versions/0abd1c81e3aa_index_coments_by_thread.py @@ -0,0 +1,28 @@ +"""Index coments by thread + +Revision ID: 0abd1c81e3aa +Revises: 44c2e37ef899 +Create Date: 2021-07-07 16:26:23.230696 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '0abd1c81e3aa' +down_revision = '44c2e37ef899' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_index(op.f('ix_comment_thread_id'), 'comment', ['thread_id'], unique=False) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_comment_thread_id'), table_name='comment') + # ### end Alembic commands ###