diff --git a/add_index_tags_assign.sql b/add_index_tags_assign.sql new file mode 100644 index 0000000..670841d --- /dev/null +++ b/add_index_tags_assign.sql @@ -0,0 +1 @@ +CREATE INDEX CONCURRENTLY IF NOT EXISTS tags_assign_tag_id_idx ON public.tags_assign (tag_id); diff --git a/src/inc/routes/apiv2/index.mjs b/src/inc/routes/apiv2/index.mjs index 1fc9a47..867e49c 100644 --- a/src/inc/routes/apiv2/index.mjs +++ b/src/inc/routes/apiv2/index.mjs @@ -25,7 +25,15 @@ export default router => { const hasSession = !!req.session; const modequery = mime.startsWith("audio") ? lib.getMode(0) : lib.getMode(req.session?.mode ?? 0); + // ID Seek Strategy + const maxIdResult = await db`select max(id) as id from "items"`; + const maxId = maxIdResult[0].id || 0; + + const randomId = Math.floor(Math.random() * maxId); + + // 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` @@ -42,26 +50,24 @@ export default router => { ${!hasSession && globalfilter ? db`and not exists (select 1 from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``} `; - const count = await db` - select count(*) as total + // Try seeking forward from random ID + let rows = await db` ${baseQuery} + and "items".id >= ${randomId} + order by "items".id asc + limit 1 `; - if (count[0].total == 0) { - return res.json({ - success: false, - items: [] - }); + // Fallback: wrap around if nothing found + if (rows.length === 0) { + rows = await db` + ${baseQuery} + and "items".id >= 0 + order by "items".id asc + limit 1 + `; } - const offset = Math.floor(Math.random() * count[0].total); - - const rows = await db` - select "items".* - ${baseQuery} - limit 1 offset ${offset} - `; - return res.json({ success: rows.length > 0, items: rows.length > 0 ? rows[0] : []