From 64f282fff2b0c9170aea68e22f1b9e6fc2c87761 Mon Sep 17 00:00:00 2001 From: William Chargin Date: Sat, 17 Aug 2019 03:58:55 -0700 Subject: [PATCH] discourse: consolidate parallel `MAX` queries (#1296) Summary: As mentioned in . Test Plan: Running `yarn test` passes. wchargin-branch: discourse-single-max-query --- src/plugins/discourse/mirror.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/plugins/discourse/mirror.js b/src/plugins/discourse/mirror.js index 8adddc0..2f71bf5 100644 --- a/src/plugins/discourse/mirror.js +++ b/src/plugins/discourse/mirror.js @@ -216,17 +216,15 @@ export class Mirror implements DiscourseData { async update() { const db = this._db; const latestTopicId = await this._fetcher.latestTopicId(); - const lastLocalPostId = - db - .prepare("SELECT MAX(id) FROM posts") - .pluck() - .get() || 0; - - const lastLocalTopicId = - db - .prepare("SELECT MAX(id) FROM topics") - .pluck() - .get() || 0; + const {max_post: lastLocalPostId, max_topic: lastLocalTopicId} = db + .prepare( + dedent`\ + SELECT + (SELECT IFNULL(MAX(id), 0) FROM posts) AS max_post, + (SELECT IFNULL(MAX(id), 0) FROM topics) AS max_topic + ` + ) + .get(); const encounteredPostIds = new Set();