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
This commit is contained in:
codl 2020-12-29 21:49:27 +01:00
parent ed9146231e
commit 162f798df7
1 changed files with 10 additions and 4 deletions

View File

@ -419,10 +419,16 @@ def queue_deletes():
@unique @unique
def refresh_account_with_oldest_post(): def refresh_account_with_oldest_post():
then = time() then = time()
post = (Post.query.outerjoin(Post.author).join(Account.tokens) post = db.session.query(Post).from_statement(db.text("""
.filter(Account.backoff_until < db.func.now()) SELECT posts.id, posts.author_id
.filter(~Account.dormant).group_by(Post).order_by( FROM posts, accounts, oauth_tokens
db.asc(Post.updated_at)).first()) 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: if post:
aid = post.author_id aid = post.author_id
refresh_account(aid) refresh_account(aid)