From 1c58423f6c42f494ea6358043bcb5a735b7bd5d7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 20 Aug 2020 08:52:16 +0200 Subject: [PATCH] Optimize comment RSS sql query --- server/models/utils.ts | 25 ++++++++++++++++++++++++ server/models/video/video-comment.ts | 29 ++++++++++++++-------------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/server/models/utils.ts b/server/models/utils.ts index d706d9ea8..6e5522346 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -129,6 +129,30 @@ function buildBlockedAccountSQL (blockerIds: number[]) { 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')' } +function buildBlockedAccountSQLOptimized (columnNameJoin: string, blockerIds: number[]) { + const blockerIdsString = blockerIds.join(', ') + + return [ + literal( + `NOT EXISTS (` + + ` SELECT 1 FROM "accountBlocklist" ` + + ` WHERE "targetAccountId" = ${columnNameJoin} ` + + ` AND "accountId" IN (${blockerIdsString})` + + `)` + ), + + literal( + `NOT EXISTS (` + + ` SELECT 1 FROM "account" ` + + ` INNER JOIN "actor" ON account."actorId" = actor.id ` + + ` INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` + + ` WHERE "account"."id" = ${columnNameJoin} ` + + ` AND "serverBlocklist"."accountId" IN (${blockerIdsString})` + + `)` + ) + ] +} + function buildServerIdsFollowedBy (actorId: any) { const actorIdNumber = parseInt(actorId + '', 10) @@ -201,6 +225,7 @@ function searchAttribute (sourceField?: string, targetField?: string) { export { buildBlockedAccountSQL, + buildBlockedAccountSQLOptimized, buildLocalActorIdsIn, SortType, buildLocalAccountIdsIn, diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 1d5c7280d..de27b3d87 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,6 +1,6 @@ import * as Bluebird from 'bluebird' import { uniq } from 'lodash' -import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' +import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BelongsTo, @@ -40,7 +40,7 @@ import { import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' import { AccountModel } from '../account/account' import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' -import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' +import { buildBlockedAccountSQL, buildBlockedAccountSQLOptimized, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoChannelModel } from './video-channel' @@ -460,19 +460,20 @@ export class VideoCommentModel extends Model { const serverActor = await getServerActor() const { start, count, videoId, accountId, videoChannelId } = parameters - const accountExclusion = { - [Op.notIn]: Sequelize.literal( - '(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')' - ) + const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized( + '"VideoCommentModel"."accountId"', + [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ] + ) + + if (accountId) { + whereAnd.push({ + [Op.eq]: accountId + }) + } + + const accountWhere = { + [Op.and]: whereAnd } - const accountWhere = accountId - ? { - [Op.and]: { - ...accountExclusion, - [Op.eq]: accountId - } - } - : accountExclusion const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined