Optimize index sizes
authorChocobozzz <me@florianbigard.com>
Thu, 20 Dec 2018 14:25:24 +0000 (15:25 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 20 Dec 2018 14:25:49 +0000 (15:25 +0100)
server/initializers/constants.ts
server/initializers/migrations/0310-drop-unused-video-indexes.ts [new file with mode: 0644]
server/models/video/video.ts

index 1c27a9f6b9710c2e2dc1b0ee8687087d68961f14..c3df2383adeea17e7f29a60a0b8fdb3b5ae602e6 100644 (file)
@@ -16,7 +16,7 @@ let config: IConfig = require('config')
 
 // ---------------------------------------------------------------------------
 
-const LAST_MIGRATION_VERSION = 305
+const LAST_MIGRATION_VERSION = 310
 
 // ---------------------------------------------------------------------------
 
diff --git a/server/initializers/migrations/0310-drop-unused-video-indexes.ts b/server/initializers/migrations/0310-drop-unused-video-indexes.ts
new file mode 100644 (file)
index 0000000..d51f430
--- /dev/null
@@ -0,0 +1,32 @@
+import * as Sequelize from 'sequelize'
+
+async function up (utils: {
+  transaction: Sequelize.Transaction,
+  queryInterface: Sequelize.QueryInterface,
+  sequelize: Sequelize.Sequelize,
+  db: any
+}): Promise<void> {
+  const indexNames = [
+    'video_category',
+    'video_licence',
+    'video_nsfw',
+    'video_language',
+    'video_wait_transcoding',
+    'video_state',
+    'video_remote',
+    'video_likes'
+  ]
+
+  for (const indexName of indexNames) {
+    await utils.sequelize.query('DROP INDEX IF EXISTS "' + indexName + '";')
+  }
+}
+
+function down (options) {
+  throw new Error('Not implemented.')
+}
+
+export {
+  up,
+  down
+}
index 3f282580c5b11b060ae96bcabe64cd9602c66c0d..bcf327f32a215dd7eca8e642f64681b082ece2ab 100644 (file)
@@ -102,16 +102,44 @@ const indexes: Sequelize.DefineIndexesOptions[] = [
   { fields: [ 'createdAt' ] },
   { fields: [ 'publishedAt' ] },
   { fields: [ 'duration' ] },
-  { fields: [ 'category' ] },
-  { fields: [ 'licence' ] },
-  { fields: [ 'nsfw' ] },
-  { fields: [ 'language' ] },
-  { fields: [ 'waitTranscoding' ] },
-  { fields: [ 'state' ] },
-  { fields: [ 'remote' ] },
   { fields: [ 'views' ] },
-  { fields: [ 'likes' ] },
   { fields: [ 'channelId' ] },
+  {
+    fields: [ 'category' ], // We don't care videos with an unknown category
+    where: {
+      category: {
+        [Sequelize.Op.ne]: null
+      }
+    }
+  },
+  {
+    fields: [ 'licence' ], // We don't care videos with an unknown licence
+    where: {
+      licence: {
+        [Sequelize.Op.ne]: null
+      }
+    }
+  },
+  {
+    fields: [ 'language' ], // We don't care videos with an unknown language
+    where: {
+      language: {
+        [Sequelize.Op.ne]: null
+      }
+    }
+  },
+  {
+    fields: [ 'nsfw' ], // Most of the videos are not NSFW
+    where: {
+      nsfw: true
+    }
+  },
+  {
+    fields: [ 'remote' ], // Only index local videos
+    where: {
+      remote: false
+    }
+  },
   {
     fields: [ 'uuid' ],
     unique: true