indices indexes
This commit is contained in:
parent
c32332d07c
commit
1791c4065b
|
@ -0,0 +1,26 @@
|
||||||
|
"""replace index on posts.author_id with composite index on author_id and created_at
|
||||||
|
|
||||||
|
Revision ID: f63bf9e73bc9
|
||||||
|
Revises: e769c033e5c9
|
||||||
|
Create Date: 2017-08-15 23:55:46.945437
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'f63bf9e73bc9'
|
||||||
|
down_revision = 'e769c033e5c9'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_index('ix_posts_author_id_created_at', 'posts', ['author_id', 'created_at'], unique=False)
|
||||||
|
op.drop_index('ix_posts_author_id', table_name='posts')
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.create_index('ix_posts_author_id', 'posts', ['author_id'], unique=False)
|
||||||
|
op.drop_index('ix_posts_author_id_created_at', table_name='posts')
|
6
model.py
6
model.py
|
@ -101,7 +101,7 @@ class Account(TimestampMixin, RemoteIDMixin):
|
||||||
"""
|
"""
|
||||||
latest_n_posts = Post.query.with_parent(self).order_by(db.desc(Post.created_at)).limit(self.policy_keep_latest)
|
latest_n_posts = Post.query.with_parent(self).order_by(db.desc(Post.created_at)).limit(self.policy_keep_latest)
|
||||||
query = Post.query.with_parent(self).\
|
query = Post.query.with_parent(self).\
|
||||||
filter(Post.created_at + self.policy_keep_younger <= db.func.now()).\
|
filter(Post.created_at <= db.func.now() - self.policy_keep_younger).\
|
||||||
except_(latest_n_posts)
|
except_(latest_n_posts)
|
||||||
if(self.policy_keep_favourites):
|
if(self.policy_keep_favourites):
|
||||||
query = query.filter_by(favourite = False)
|
query = query.filter_by(favourite = False)
|
||||||
|
@ -147,7 +147,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, index=True)
|
author_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
|
||||||
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)))
|
||||||
|
|
||||||
|
@ -162,6 +162,8 @@ class Post(db.Model, TimestampMixin, RemoteIDMixin):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Post ({}, "{}", Author: {})>'.format(self.id, self.snippet(), self.author_id)
|
return '<Post ({}, "{}", Author: {})>'.format(self.id, self.snippet(), self.author_id)
|
||||||
|
|
||||||
|
db.Index('ix_posts_author_id_created_at', Post.author_id, Post.created_at)
|
||||||
|
|
||||||
class TwitterArchive(db.Model, TimestampMixin):
|
class TwitterArchive(db.Model, TimestampMixin):
|
||||||
__tablename__ = 'twitter_archives'
|
__tablename__ = 'twitter_archives'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue