add indexes to speed up db

This commit is contained in:
rimu 2024-01-24 21:17:36 +13:00
parent 75f2c8d2ef
commit 612f60f12d
2 changed files with 87 additions and 11 deletions

View file

@ -818,16 +818,16 @@ class CommunityBan(db.Model):
class UserNote(db.Model): class UserNote(db.Model):
id = db.Column(db.Integer, primary_key=True) 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)
target_id = db.Column(db.Integer, db.ForeignKey('user.id')) target_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True)
body = db.Column(db.Text) body = db.Column(db.Text)
created_at = db.Column(db.DateTime, default=utcnow) created_at = db.Column(db.DateTime, default=utcnow)
class UserBlock(db.Model): class UserBlock(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
blocker_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')) blocked_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True)
created_at = db.Column(db.DateTime, default=utcnow) created_at = db.Column(db.DateTime, default=utcnow)
@ -845,7 +845,7 @@ class Interest(db.Model):
class CommunityJoinRequest(db.Model): class CommunityJoinRequest(db.Model):
id = db.Column(db.Integer, primary_key=True) 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'))
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): class UserFollowRequest(db.Model):
@ -856,9 +856,9 @@ class UserFollowRequest(db.Model):
class PostVote(db.Model): class PostVote(db.Model):
id = db.Column(db.Integer, primary_key=True) 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')) 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) effect = db.Column(db.Float, index=True)
created_at = db.Column(db.DateTime, default=utcnow) created_at = db.Column(db.DateTime, default=utcnow)
post = db.relationship('Post', foreign_keys=[post_id]) post = db.relationship('Post', foreign_keys=[post_id])
@ -866,9 +866,9 @@ class PostVote(db.Model):
class PostReplyVote(db.Model): class PostReplyVote(db.Model):
id = db.Column(db.Integer, primary_key=True) 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 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) effect = db.Column(db.Float)
created_at = db.Column(db.DateTime, default=utcnow) 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_posts = db.Column(db.Boolean, default=True)
filter_replies = db.Column(db.Boolean, default=False) filter_replies = db.Column(db.Boolean, default=False)
hide_type = db.Column(db.Integer, default=0) # 0 = hide with warning, 1 = hide completely 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) expire_after = db.Column(db.Date)
keywords = db.Column(db.String(500)) keywords = db.Column(db.String(500))
@ -919,7 +919,7 @@ class Notification(db.Model):
title = db.Column(db.String(50)) title = db.Column(db.String(50))
url = db.Column(db.String(512)) url = db.Column(db.String(512))
read = db.Column(db.Boolean, default=False) 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 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) created_at = db.Column(db.DateTime, default=utcnow)

View file

@ -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 ###