From cb31cfd85b14b104752394c10f34d8c65cad1ee9 Mon Sep 17 00:00:00 2001 From: Kibi Kelburton Date: Mon, 26 Jan 2026 09:58:30 +0100 Subject: [PATCH] possible random performance improvement ? --- src/inc/routes/apiv2/index.mjs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/inc/routes/apiv2/index.mjs b/src/inc/routes/apiv2/index.mjs index 867e49c..85d5ad4 100644 --- a/src/inc/routes/apiv2/index.mjs +++ b/src/inc/routes/apiv2/index.mjs @@ -31,22 +31,28 @@ export default router => { const randomId = Math.floor(Math.random() * maxId); + let tagId = null; + if (tag) { + const tagResult = await db`select id from "tags" where normalized ilike '%' || slugify(${tag}) || '%' limit 1`; + if (tagResult.length === 0) { + return res.json({ success: false, items: [] }); + } + tagId = tagResult[0].id; + } + // Reusable query parts const baseQuery = db` select "items".* from "items" ${isFav ? db`join "favorites" on "favorites".item_id = "items".id join "user" as fu on fu.id = "favorites".user_id` - : db`` } - left join tags_assign on tags_assign.item_id = items.id - left join tags on tags.id = tags_assign.tag_id where ${db.unsafe(modequery)} and mime ilike ${mime} and active = 'true' ${isFav ? db`and fu."user" = ${user}` : db`and items.username ilike ${user}`} - ${tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db``} + ${tagId ? db`and exists (select 1 from tags_assign where item_id = items.id and tag_id = ${tagId})` : db``} ${!hasSession && globalfilter ? db`and not exists (select 1 from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``} `;