Optimize abuse SQL query

This commit is contained in:
Chocobozzz 2024-03-20 08:34:14 +01:00
parent 8da3e2e9b8
commit 33101bc5b3
No known key found for this signature in database
GPG Key ID: 583A612D890159BE

View File

@ -74,7 +74,8 @@ export enum ScopeNames {
'(' +
'SELECT count(*) ' +
'FROM "videoAbuse" ' +
'WHERE "videoId" = "VideoAbuse"."videoId" AND "videoId" IS NOT NULL' +
'WHERE "videoId" IN (SELECT "videoId" FROM "videoAbuse" WHERE "abuseId" = "AbuseModel"."id") ' +
'AND "videoId" IS NOT NULL' +
')'
),
'countReportsForVideo'
@ -85,11 +86,10 @@ export enum ScopeNames {
'(' +
'SELECT t.nth ' +
'FROM ( ' +
'SELECT id, ' +
'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
'SELECT id, "abuseId", row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
'FROM "videoAbuse" ' +
') t ' +
'WHERE t.id = "VideoAbuse".id AND t.id IS NOT NULL' +
'WHERE t."abuseId" = "AbuseModel"."id" ' +
')'
),
'nthReportForVideo'
@ -645,7 +645,8 @@ export class AbuseModel extends SequelizeModel<AbuseModel> {
id: {
[Op.in]: ids
}
}
},
limit: parameters.count
})
}