add some probably good indexes

This commit is contained in:
codl 2017-08-14 20:29:49 +02:00
parent dc45cddf96
commit cebfab2542
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
2 changed files with 40 additions and 6 deletions

View File

@ -0,0 +1,34 @@
"""add some probably good indexes (???)
Revision ID: 6d298e6406f2
Revises: 8fac6e10bdb3
Create Date: 2017-08-14 20:27:49.103672
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '6d298e6406f2'
down_revision = '8fac6e10bdb3'
branch_labels = None
depends_on = None
def upgrade():
op.create_index(op.f('ix_accounts_last_delete'), 'accounts', ['last_delete'], unique=False)
op.create_index(op.f('ix_accounts_last_fetch'), 'accounts', ['last_fetch'], unique=False)
op.create_index(op.f('ix_accounts_last_refresh'), 'accounts', ['last_refresh'], unique=False)
op.create_index(op.f('ix_oauth_tokens_account_id'), 'oauth_tokens', ['account_id'], unique=False)
op.create_index(op.f('ix_posts_author_id'), 'posts', ['author_id'], unique=False)
op.create_index(op.f('ix_sessions_account_id'), 'sessions', ['account_id'], unique=False)
def downgrade():
op.drop_index(op.f('ix_sessions_account_id'), table_name='sessions')
op.drop_index(op.f('ix_posts_author_id'), table_name='posts')
op.drop_index(op.f('ix_oauth_tokens_account_id'), table_name='oauth_tokens')
op.drop_index(op.f('ix_accounts_last_refresh'), table_name='accounts')
op.drop_index(op.f('ix_accounts_last_fetch'), table_name='accounts')
op.drop_index(op.f('ix_accounts_last_delete'), table_name='accounts')

View File

@ -52,9 +52,9 @@ class Account(TimestampMixin, RemoteIDMixin):
avatar_url = db.Column(db.String) avatar_url = db.Column(db.String)
reported_post_count = db.Column(db.Integer) reported_post_count = db.Column(db.Integer)
last_fetch = db.Column(db.DateTime, server_default='epoch') last_fetch = db.Column(db.DateTime, server_default='epoch', index=True)
last_delete = db.Column(db.DateTime, server_default='epoch') last_delete = db.Column(db.DateTime, server_default='epoch', index=True)
last_refresh = db.Column(db.DateTime, server_default='epoch') last_refresh = db.Column(db.DateTime, server_default='epoch', index=True)
def touch_fetch(self): def touch_fetch(self):
self.last_fetch = db.func.now() self.last_fetch = db.func.now()
@ -120,7 +120,7 @@ class OAuthToken(db.Model, TimestampMixin):
token = db.Column(db.String, primary_key=True) token = db.Column(db.String, primary_key=True)
token_secret = db.Column(db.String, nullable=False) token_secret = db.Column(db.String, nullable=False)
account_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=True) account_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=True, index=True)
account = db.relationship(Account, backref=db.backref('tokens', order_by=lambda: db.desc(OAuthToken.created_at))) account = db.relationship(Account, backref=db.backref('tokens', order_by=lambda: db.desc(OAuthToken.created_at)))
# note: account_id is nullable here because we don't know what account a token is for # note: account_id is nullable here because we don't know what account a token is for
@ -132,7 +132,7 @@ class Session(db.Model, TimestampMixin):
id = db.Column(db.String, primary_key=True, default=lambda: secrets.token_urlsafe()) id = db.Column(db.String, primary_key=True, default=lambda: secrets.token_urlsafe())
account_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False) account_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False, index=True)
account = db.relationship(Account, lazy='joined', backref='sessions') account = db.relationship(Account, lazy='joined', backref='sessions')
@ -142,7 +142,7 @@ class Post(db.Model, TimestampMixin, RemoteIDMixin):
id = db.Column(db.String, primary_key=True) id = db.Column(db.String, primary_key=True)
body = db.Column(db.String) body = db.Column(db.String)
author_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False) author_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False, index=True)
author = db.relationship(Account, author = db.relationship(Account,
backref=db.backref('posts', order_by=lambda: db.desc(Post.created_at))) backref=db.backref('posts', order_by=lambda: db.desc(Post.created_at)))