possible random performance improvement ?
This commit is contained in:
@@ -31,22 +31,28 @@ export default router => {
|
|||||||
|
|
||||||
const randomId = Math.floor(Math.random() * maxId);
|
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
|
// Reusable query parts
|
||||||
const baseQuery = db`
|
const baseQuery = db`
|
||||||
select "items".*
|
select "items".*
|
||||||
from "items"
|
from "items"
|
||||||
${isFav
|
${isFav
|
||||||
? db`join "favorites" on "favorites".item_id = "items".id join "user" as fu on fu.id = "favorites".user_id`
|
? 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
|
where
|
||||||
${db.unsafe(modequery)} and
|
${db.unsafe(modequery)} and
|
||||||
mime ilike ${mime} and
|
mime ilike ${mime} and
|
||||||
active = 'true'
|
active = 'true'
|
||||||
${isFav ? db`and fu."user" = ${user}` : db`and items.username ilike ${user}`}
|
${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``}
|
${!hasSession && globalfilter ? db`and not exists (select 1 from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user