From ddc07312b041c1c533b68a919681fc9bce83430d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 8 Jan 2020 11:36:37 +0100 Subject: Optimize videos list SQL queries workflow --- server/models/video/video.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'server') diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 87ad80caf..e85c5e38e 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1750,17 +1750,25 @@ export class VideoModel extends Model { ] } - const [ count, ids ] = await Promise.all([ + const [ count, rows ] = await Promise.all([ countVideos ? VideoModel.scope(countScope).count(countQuery) : Promise.resolve(undefined), VideoModel.scope(idsScope) - .findAll(query) + .findAll(Object.assign({}, query, { raw: true })) .then(rows => rows.map(r => r.id)) + .then(ids => VideoModel.loadCompleteVideosForApi(ids, query, options)) ]) - if (ids.length === 0) return { data: [], total: count } + return { + data: rows, + total: count + } + } + + private static loadCompleteVideosForApi (ids: number[], query: FindOptions, options: AvailableForListIDsOptions) { + if (ids.length === 0) return [] const secondQuery: FindOptions = { offset: 0, @@ -1789,12 +1797,7 @@ export class VideoModel extends Model { ] }) - const rows = await VideoModel.scope(apiScope).findAll(secondQuery) - - return { - data: rows, - total: count - } + return VideoModel.scope(apiScope).findAll(secondQuery) } private static isPrivacyForFederation (privacy: VideoPrivacy) { -- cgit v1.2.3