From 5bd8025b2b900f6954505ae5400e14bfd28b9c54 Mon Sep 17 00:00:00 2001 From: keyan Date: Wed, 6 Oct 2021 18:50:18 -0700 Subject: [PATCH] speed hack hot page --- api/resolvers/item.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 68272b1b..155d0f8d 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -48,7 +48,26 @@ export default { LIMIT ${LIMIT}`, Number(userId), decodedCursor.time, decodedCursor.offset) break case 'hot': - items = await models.$queryRaw(` + // HACK we can speed hack the first hot page, by limiting our query to only + // the most recently created items so that the tables doesn't have to + // fully be computed + // if the offset is 0, we limit our search to posts from the last week + // if there are 21 items, return them ... if not do the unrestricted query + // instead of doing this we should materialize a view ... but this is easier for now + + if (decodedCursor.offset === 0) { + items = await models.$queryRaw(` + ${SELECT} + FROM "Item" + ${timedLeftJoinSats(1)} + WHERE "parentId" IS NULL AND created_at <= $1 AND created_at > $3 + ${timedOrderBySats(1)} + OFFSET $2 + LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, new Date(new Date() - 7)) + } + + if (decodedCursor.offset !== 0 || items.length < LIMIT) { + items = await models.$queryRaw(` ${SELECT} FROM "Item" ${timedLeftJoinSats(1)} @@ -56,6 +75,7 @@ export default { ${timedOrderBySats(1)} OFFSET $2 LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset) + } break default: items = await models.$queryRaw(`