From 534556052ed4aab539325dccaa16c6dd2acecf73 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 3 Mar 2021 11:24:16 +0100 Subject: [PATCH] Optimize views sort sql query --- server/initializers/constants.ts | 2 +- .../migrations/0610-views-index.ts | 20 +++++++++++++++++++ server/models/video/video.ts | 7 ++++++- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 server/initializers/migrations/0610-views-index.ts diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 6aaef3aa2..1623e6f42 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -24,7 +24,7 @@ import { CONFIG, registerConfigChangedHandler } from './config' // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 605 +const LAST_MIGRATION_VERSION = 610 // --------------------------------------------------------------------------- diff --git a/server/initializers/migrations/0610-views-index.ts b/server/initializers/migrations/0610-views-index.ts new file mode 100644 index 000000000..02ee21172 --- /dev/null +++ b/server/initializers/migrations/0610-views-index.ts @@ -0,0 +1,20 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction + queryInterface: Sequelize.QueryInterface + sequelize: Sequelize.Sequelize + db: any +}): Promise { + + await utils.sequelize.query('DROP INDEX IF EXISTS video_views;') +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} diff --git a/server/models/video/video.ts b/server/models/video/video.ts index b4c7da655..b3543ddf2 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -426,7 +426,12 @@ export type AvailableForListIDsOptions = { ] }, { fields: [ 'duration' ] }, - { fields: [ 'views' ] }, + { + fields: [ + { name: 'views', order: 'DESC' }, + { name: 'id', order: 'ASC' } + ] + }, { fields: [ 'channelId' ] }, { fields: [ 'originallyPublishedAt' ],