From 162f798df77639d029da205e0f86fd289fb71b48 Mon Sep 17 00:00:00 2001 From: codl Date: Tue, 29 Dec 2020 21:49:27 +0100 Subject: [PATCH] simplify (and hand-write) query in refresh_account_with_oldest_post. .#166 this takes about 1/5th the time of the old query. it's still kinda slow --- tasks.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tasks.py b/tasks.py index c125ec5..07c7bf7 100644 --- a/tasks.py +++ b/tasks.py @@ -419,10 +419,16 @@ def queue_deletes(): @unique def refresh_account_with_oldest_post(): then = time() - post = (Post.query.outerjoin(Post.author).join(Account.tokens) - .filter(Account.backoff_until < db.func.now()) - .filter(~Account.dormant).group_by(Post).order_by( - db.asc(Post.updated_at)).first()) + post = db.session.query(Post).from_statement(db.text(""" + SELECT posts.id, posts.author_id + FROM posts, accounts, oauth_tokens + WHERE accounts.id = posts.author_id + AND accounts.id = oauth_tokens.account_id + AND accounts.backoff_until < now() + AND NOT accounts.dormant + ORDER BY posts.updated_at ASC + LIMIT 1; + """).columns(Post.id, Post.author_id)).one() if post: aid = post.author_id refresh_account(aid)