diff options
author | Chocobozzz <me@florianbigard.com> | 2020-02-21 14:11:11 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-02-25 16:30:09 +0100 |
commit | 1647e4effd9481203de8bd6204c54bc023b37ae6 (patch) | |
tree | 9f39b186a487826960520a5b90ccd8df9b63666e /server/models/video/video.ts | |
parent | e962e1c3c52b77075ec7b46ed0984476684551ab (diff) | |
download | PeerTube-1647e4effd9481203de8bd6204c54bc023b37ae6.tar.gz PeerTube-1647e4effd9481203de8bd6204c54bc023b37ae6.tar.zst PeerTube-1647e4effd9481203de8bd6204c54bc023b37ae6.zip |
Fix CPU usage on PostgreSQL
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r-- | server/models/video/video.ts | 49 |
1 files changed, 19 insertions, 30 deletions
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index bd4ca63ea..2e518317d 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -361,42 +361,31 @@ export type AvailableForListIDsOptions = { | |||
361 | } | 361 | } |
362 | 362 | ||
363 | if (options.followerActorId) { | 363 | if (options.followerActorId) { |
364 | let localVideosReq: WhereOptions = {} | 364 | let localVideosReq = '' |
365 | if (options.includeLocalVideos === true) { | 365 | if (options.includeLocalVideos === true) { |
366 | localVideosReq = { remote: false } | 366 | localVideosReq = ' UNION ALL SELECT "video"."id" FROM "video" WHERE remote IS FALSE' |
367 | } | 367 | } |
368 | 368 | ||
369 | // Force actorId to be a number to avoid SQL injections | 369 | // Force actorId to be a number to avoid SQL injections |
370 | const actorIdNumber = parseInt(options.followerActorId.toString(), 10) | 370 | const actorIdNumber = parseInt(options.followerActorId.toString(), 10) |
371 | whereAnd.push({ | 371 | whereAnd.push({ |
372 | [Op.or]: [ | 372 | id: { |
373 | { | 373 | [Op.in]: Sequelize.literal( |
374 | id: { | 374 | '(' + |
375 | [Op.in]: Sequelize.literal( | 375 | 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' + |
376 | '(' + | 376 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + |
377 | 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' + | 377 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + |
378 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' + | 378 | ' UNION ALL ' + |
379 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + | 379 | 'SELECT "video"."id" AS "id" FROM "video" ' + |
380 | ')' | 380 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + |
381 | ) | 381 | 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + |
382 | } | 382 | 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + |
383 | }, | 383 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' + |
384 | { | 384 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + |
385 | id: { | 385 | localVideosReq + |
386 | [Op.in]: Sequelize.literal( | 386 | ')' |
387 | '(' + | 387 | ) |
388 | 'SELECT "video"."id" AS "id" FROM "video" ' + | 388 | } |
389 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | ||
390 | 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' + | ||
391 | 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' + | ||
392 | 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' + | ||
393 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + | ||
394 | ')' | ||
395 | ) | ||
396 | } | ||
397 | }, | ||
398 | localVideosReq | ||
399 | ] | ||
400 | }) | 389 | }) |
401 | } | 390 | } |
402 | 391 | ||