Optimize comment RSS sql query

pull/3112/head
Chocobozzz 2020-08-20 08:52:16 +02:00
parent 1c5c31a1ce
commit 1c58423f6c
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 40 additions and 14 deletions

View File

@ -129,6 +129,30 @@ function buildBlockedAccountSQL (blockerIds: number[]) {
'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')' '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) { function buildServerIdsFollowedBy (actorId: any) {
const actorIdNumber = parseInt(actorId + '', 10) const actorIdNumber = parseInt(actorId + '', 10)
@ -201,6 +225,7 @@ function searchAttribute (sourceField?: string, targetField?: string) {
export { export {
buildBlockedAccountSQL, buildBlockedAccountSQL,
buildBlockedAccountSQLOptimized,
buildLocalActorIdsIn, buildLocalActorIdsIn,
SortType, SortType,
buildLocalAccountIdsIn, buildLocalAccountIdsIn,

View File

@ -1,6 +1,6 @@
import * as Bluebird from 'bluebird' import * as Bluebird from 'bluebird'
import { uniq } from 'lodash' import { uniq } from 'lodash'
import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize' import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
import { import {
AllowNull, AllowNull,
BelongsTo, BelongsTo,
@ -40,7 +40,7 @@ import {
import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse'
import { AccountModel } from '../account/account' import { AccountModel } from '../account/account'
import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' 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 { VideoModel } from './video'
import { VideoChannelModel } from './video-channel' import { VideoChannelModel } from './video-channel'
@ -460,19 +460,20 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
const serverActor = await getServerActor() const serverActor = await getServerActor()
const { start, count, videoId, accountId, videoChannelId } = parameters const { start, count, videoId, accountId, videoChannelId } = parameters
const accountExclusion = { const whereAnd: WhereOptions[] = buildBlockedAccountSQLOptimized(
[Op.notIn]: Sequelize.literal( '"VideoCommentModel"."accountId"',
'(' + buildBlockedAccountSQL([ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]) + ')' [ serverActor.Account.id, '"Video->VideoChannel"."accountId"' ]
) )
}
const accountWhere = accountId if (accountId) {
? { whereAnd.push({
[Op.and]: {
...accountExclusion,
[Op.eq]: accountId [Op.eq]: accountId
})
} }
const accountWhere = {
[Op.and]: whereAnd
} }
: accountExclusion
const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined const videoChannelWhere = videoChannelId ? { id: videoChannelId } : undefined