From e2db5e02f4ed2fac6aaabc2c73117f43cb26ecb9 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Thu, 18 Jul 2024 23:51:39 +0900 Subject: [PATCH] fix: use faster single query instead of batch --- src/helpers/spaces.ts | 59 +++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/helpers/spaces.ts b/src/helpers/spaces.ts index 1cfd4f6b..cf80a684 100644 --- a/src/helpers/spaces.ts +++ b/src/helpers/spaces.ts @@ -127,46 +127,39 @@ function mapSpaces() { } async function loadSpaces() { - const limit = 25e3; - let newSpaces = {}; - let start = 0; + const newSpaces = {}; + const startTime = +Date.now(); - while (true) { - const query = ` + const query = ` SELECT id, settings, flagged, verified, turbo, hibernated, follower_count, proposal_count, vote_count FROM spaces WHERE deleted = 0 ORDER BY id ASC - LIMIT ${start}, ${limit} `; - const results = await db.queryAsync(query); - - if (!results.length) break; - - const formattedSpaces = Object.fromEntries( - results.map(space => [ - space.id, - { - ...JSON.parse(space.settings), - flagged: space.flagged === 1, - verified: space.verified === 1, - turbo: space.turbo === 1, - hibernated: space.hibernated === 1, - follower_count: space.follower_count, - vote_count: space.vote_count, - proposal_count: space.proposal_count - } - ]) - ); - - newSpaces = { ...newSpaces, ...formattedSpaces }; - - start += limit; - } - - spaces = newSpaces; + const results = await db.queryAsync(query); + + spaces = Object.fromEntries( + results.map(space => [ + space.id, + { + ...JSON.parse(space.settings), + flagged: space.flagged === 1, + verified: space.verified === 1, + turbo: space.turbo === 1, + hibernated: space.hibernated === 1, + follower_count: space.follower_count, + vote_count: space.vote_count, + proposal_count: space.proposal_count + } + ]) + ); - log.info(`[spaces] total spaces ${Object.keys(spaces).length}`); + log.info( + `[spaces] total spaces ${Object.keys(spaces).length}, in (${( + (+Date.now() - startTime) / + 1000 + ).toFixed()}s)` + ); mapSpaces(); }