From 1791c4065ba0502e6ae9c27f5bbb2fe10471c80d Mon Sep 17 00:00:00 2001 From: codl Date: Tue, 15 Aug 2017 23:58:33 +0200 Subject: [PATCH] indices indexes --- ..._replace_index_on_posts_author_id_with_.py | 26 +++++++++++++++++++ model.py | 6 +++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/f63bf9e73bc9_replace_index_on_posts_author_id_with_.py diff --git a/migrations/versions/f63bf9e73bc9_replace_index_on_posts_author_id_with_.py b/migrations/versions/f63bf9e73bc9_replace_index_on_posts_author_id_with_.py new file mode 100644 index 0000000..4f17fe4 --- /dev/null +++ b/migrations/versions/f63bf9e73bc9_replace_index_on_posts_author_id_with_.py @@ -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') diff --git a/model.py b/model.py index 330c7ed..bc89574 100644 --- a/model.py +++ b/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) 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) if(self.policy_keep_favourites): query = query.filter_by(favourite = False) @@ -147,7 +147,7 @@ class Post(db.Model, TimestampMixin, RemoteIDMixin): id = db.Column(db.String, primary_key=True) 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, 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): return ''.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): __tablename__ = 'twitter_archives'