Try to optimize tagsOneOf/tagsAllOf SQL queries

Using a CTE because the query will probably return a few results
I tried a IN clause but PG doesn't seem to be more efficient with it
The CTE seems to be the only choice
This commit is contained in:
Chocobozzz 2023-10-25 16:15:41 +02:00
parent a290fbf821
commit d3dd952cc5
No known key found for this signature in database
GPG Key ID: 583A612D890159BE

View File

@ -434,28 +434,30 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
private whereTagsOneOf (tagsOneOf: string[]) {
const tagsOneOfLower = tagsOneOf.map(t => t.toLowerCase())
this.and.push(
'EXISTS (' +
' SELECT 1 FROM "videoTag" ' +
this.cte.push(
'"tagsOneOf" AS (' +
' SELECT "videoTag"."videoId" AS "videoId" FROM "videoTag" ' +
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsOneOfLower) + ') ' +
' AND "video"."id" = "videoTag"."videoId"' +
')'
)
this.joins.push('INNER JOIN "tagsOneOf" ON "video"."id" = "tagsOneOf"."videoId"')
}
private whereTagsAllOf (tagsAllOf: string[]) {
const tagsAllOfLower = tagsAllOf.map(t => t.toLowerCase())
this.and.push(
'EXISTS (' +
' SELECT 1 FROM "videoTag" ' +
this.cte.push(
'"tagsAllOf" AS (' +
' SELECT "videoTag"."videoId" AS "videoId" FROM "videoTag" ' +
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsAllOfLower) + ') ' +
' AND "video"."id" = "videoTag"."videoId" ' +
' GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
')'
)
this.joins.push('INNER JOIN "tagsAllOf" ON "video"."id" = "tagsAllOf"."videoId"')
}
private wherePrivacyOneOf (privacyOneOf: VideoPrivacyType[]) {