From 612f60f12d477dc7bde88b9a7d350edee72ba46b Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:17:36 +1300 Subject: [PATCH] add indexes to speed up db --- app/models.py | 22 +++--- .../86b6fd708bd0_index_all_the_things.py | 76 +++++++++++++++++++ 2 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 migrations/versions/86b6fd708bd0_index_all_the_things.py diff --git a/app/models.py b/app/models.py index bf26c46b..16dd2c03 100644 --- a/app/models.py +++ b/app/models.py @@ -818,16 +818,16 @@ class CommunityBan(db.Model): class UserNote(db.Model): id = db.Column(db.Integer, primary_key=True) - user_id = db.Column(db.Integer, db.ForeignKey('user.id')) - target_id = db.Column(db.Integer, db.ForeignKey('user.id')) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) + target_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) body = db.Column(db.Text) created_at = db.Column(db.DateTime, default=utcnow) class UserBlock(db.Model): id = db.Column(db.Integer, primary_key=True) - blocker_id = db.Column(db.Integer, db.ForeignKey('user.id')) - blocked_id = db.Column(db.Integer, db.ForeignKey('user.id')) + blocker_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) + blocked_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) created_at = db.Column(db.DateTime, default=utcnow) @@ -845,7 +845,7 @@ class Interest(db.Model): class CommunityJoinRequest(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) - community_id = db.Column(db.Integer, db.ForeignKey('community.id')) + community_id = db.Column(db.Integer, db.ForeignKey('community.id'), index=True) class UserFollowRequest(db.Model): @@ -856,9 +856,9 @@ class UserFollowRequest(db.Model): class PostVote(db.Model): id = db.Column(db.Integer, primary_key=True) - user_id = db.Column(db.Integer, db.ForeignKey('user.id')) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) - post_id = db.Column(db.Integer, db.ForeignKey('post.id')) + post_id = db.Column(db.Integer, db.ForeignKey('post.id'), index=True) effect = db.Column(db.Float, index=True) created_at = db.Column(db.DateTime, default=utcnow) post = db.relationship('Post', foreign_keys=[post_id]) @@ -866,9 +866,9 @@ class PostVote(db.Model): class PostReplyVote(db.Model): id = db.Column(db.Integer, primary_key=True) - user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # who voted + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) # who voted author_id = db.Column(db.Integer, db.ForeignKey('user.id')) # the author of the reply voted on - who's reputation is affected - post_reply_id = db.Column(db.Integer, db.ForeignKey('post_reply.id')) + post_reply_id = db.Column(db.Integer, db.ForeignKey('post_reply.id'), index=True) effect = db.Column(db.Float) created_at = db.Column(db.DateTime, default=utcnow) @@ -892,7 +892,7 @@ class Filter(db.Model): filter_posts = db.Column(db.Boolean, default=True) filter_replies = db.Column(db.Boolean, default=False) hide_type = db.Column(db.Integer, default=0) # 0 = hide with warning, 1 = hide completely - user_id = db.Column(db.Integer, db.ForeignKey('user.id')) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) expire_after = db.Column(db.Date) keywords = db.Column(db.String(500)) @@ -919,7 +919,7 @@ class Notification(db.Model): title = db.Column(db.String(50)) url = db.Column(db.String(512)) read = db.Column(db.Boolean, default=False) - user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # who the notification should go to + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True) # who the notification should go to author_id = db.Column(db.Integer, db.ForeignKey('user.id')) # the person who caused the notification to happen created_at = db.Column(db.DateTime, default=utcnow) diff --git a/migrations/versions/86b6fd708bd0_index_all_the_things.py b/migrations/versions/86b6fd708bd0_index_all_the_things.py new file mode 100644 index 00000000..61f9b1fd --- /dev/null +++ b/migrations/versions/86b6fd708bd0_index_all_the_things.py @@ -0,0 +1,76 @@ +"""index all the things + +Revision ID: 86b6fd708bd0 +Revises: a4be1b198b0f +Create Date: 2024-01-24 21:17:10.102368 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '86b6fd708bd0' +down_revision = 'a4be1b198b0f' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('community_join_request', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_community_join_request_community_id'), ['community_id'], unique=False) + + with op.batch_alter_table('filter', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_filter_user_id'), ['user_id'], unique=False) + + with op.batch_alter_table('notification', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_notification_user_id'), ['user_id'], unique=False) + + with op.batch_alter_table('post_reply_vote', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_post_reply_vote_post_reply_id'), ['post_reply_id'], unique=False) + batch_op.create_index(batch_op.f('ix_post_reply_vote_user_id'), ['user_id'], unique=False) + + with op.batch_alter_table('post_vote', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_post_vote_post_id'), ['post_id'], unique=False) + batch_op.create_index(batch_op.f('ix_post_vote_user_id'), ['user_id'], unique=False) + + with op.batch_alter_table('user_block', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_user_block_blocked_id'), ['blocked_id'], unique=False) + batch_op.create_index(batch_op.f('ix_user_block_blocker_id'), ['blocker_id'], unique=False) + + with op.batch_alter_table('user_note', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_user_note_target_id'), ['target_id'], unique=False) + batch_op.create_index(batch_op.f('ix_user_note_user_id'), ['user_id'], unique=False) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user_note', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_user_note_user_id')) + batch_op.drop_index(batch_op.f('ix_user_note_target_id')) + + with op.batch_alter_table('user_block', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_user_block_blocker_id')) + batch_op.drop_index(batch_op.f('ix_user_block_blocked_id')) + + with op.batch_alter_table('post_vote', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_post_vote_user_id')) + batch_op.drop_index(batch_op.f('ix_post_vote_post_id')) + + with op.batch_alter_table('post_reply_vote', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_post_reply_vote_user_id')) + batch_op.drop_index(batch_op.f('ix_post_reply_vote_post_reply_id')) + + with op.batch_alter_table('notification', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_notification_user_id')) + + with op.batch_alter_table('filter', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_filter_user_id')) + + with op.batch_alter_table('community_join_request', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_community_join_request_community_id')) + + # ### end Alembic commands ###