make notifications faster distinct -> group by

This commit is contained in:
keyan 2022-01-31 16:33:00 -06:00
parent ebb3da07d1
commit 32ea1bd12a
1 changed files with 3 additions and 2 deletions

View File

@ -64,13 +64,14 @@ export default {
// queries ... we only ever need at most LIMIT+current offset in the child queries to // queries ... we only ever need at most LIMIT+current offset in the child queries to
// have enough items to return in the union // have enough items to return in the union
const notifications = await models.$queryRaw(` const notifications = await models.$queryRaw(`
(SELECT DISTINCT ON ("Item".created_at, "Item".id) "Item".id::TEXT, "Item".created_at AS "sortTime", NULL as "earnedSats", (SELECT "Item".id::TEXT, "Item".created_at AS "sortTime", NULL as "earnedSats",
'Reply' AS type 'Reply' AS type
FROM "Item" FROM "Item"
JOIN "Item" p ON "Item".path <@ p.path JOIN "Item" p ON "Item".path <@ p.path
WHERE p."userId" = $1 WHERE p."userId" = $1
AND "Item"."userId" <> $1 AND "Item".created_at <= $2 AND "Item"."userId" <> $1 AND "Item".created_at <= $2
ORDER BY "Item".created_at DESC, "Item".id DESC GROUP BY "Item".id
ORDER BY "Item".created_at DESC
LIMIT ${LIMIT}+$3) LIMIT ${LIMIT}+$3)
UNION ALL UNION ALL
(SELECT "Item".id::TEXT, MAX("ItemAct".created_at) AS "sortTime", (SELECT "Item".id::TEXT, MAX("ItemAct".created_at) AS "sortTime",