From 754c1211683b1587e3a9296e5778a193176d4f53 Mon Sep 17 00:00:00 2001 From: mikiher Date: Wed, 1 Jan 2025 07:34:29 +0200 Subject: [PATCH 1/3] Add libraryItem size index --- server/migrations/v2.17.7-add-indices.js | 81 ++++++++++++++++++++++++ server/models/LibraryItem.js | 3 + 2 files changed, 84 insertions(+) create mode 100644 server/migrations/v2.17.7-add-indices.js diff --git a/server/migrations/v2.17.7-add-indices.js b/server/migrations/v2.17.7-add-indices.js new file mode 100644 index 00000000..3d70ba20 --- /dev/null +++ b/server/migrations/v2.17.7-add-indices.js @@ -0,0 +1,81 @@ +/** + * @typedef MigrationContext + * @property {import('sequelize').QueryInterface} queryInterface - a suquelize QueryInterface object. + * @property {import('../Logger')} logger - a Logger object. + * + * @typedef MigrationOptions + * @property {MigrationContext} context - an object containing the migration context. + */ + +const migrationVersion = '2.17.7' +const migrationName = `${migrationVersion}-add-indices` +const loggerPrefix = `[${migrationVersion} migration]` + +/** + * This upward migration adds some indices to the libraryItems and books tables to improve query performance + * + * @param {MigrationOptions} options - an object containing the migration context. + * @returns {Promise} - A promise that resolves when the migration is complete. + */ +async function up({ context: { queryInterface, logger } }) { + // Upwards migration script + logger.info(`${loggerPrefix} UPGRADE BEGIN: ${migrationName}`) + + await addIndex(queryInterface, logger, 'libraryItems', ['libraryId', 'mediaType', 'size']) + + logger.info(`${loggerPrefix} UPGRADE END: ${migrationName}`) +} + +/** + * This downward migration script removes the indices added in the upward migration script + * + * @param {MigrationOptions} options - an object containing the migration context. + * @returns {Promise} - A promise that resolves when the migration is complete. + */ +async function down({ context: { queryInterface, logger } }) { + // Downward migration script + logger.info(`${loggerPrefix} DOWNGRADE BEGIN: ${migrationName}`) + + await removeIndex(queryInterface, logger, 'libraryItems', ['libraryId', 'mediaType', 'size']) + + logger.info(`${loggerPrefix} DOWNGRADE END: ${migrationName}`) +} + +/** + * Utility function to add an index to a table. If the index already exists, it logs a message and continues. + * + * @param {import('sequelize').QueryInterface} queryInterface + * @param {import ('../Logger')} logger + * @param {string} tableName + * @param {string[]} columns + */ +async function addIndex(queryInterface, logger, tableName, columns) { + try { + logger.info(`${loggerPrefix} adding index [${columns.join(', ')}] to table "${tableName}"`) + await queryInterface.addIndex(tableName, columns) + logger.info(`${loggerPrefix} added index [${columns.join(', ')}] to table "${tableName}"`) + } catch (error) { + if (error.name === 'SequelizeDatabaseError' && error.message.includes('already exists')) { + logger.info(`${loggerPrefix} index [${columns.join(', ')}] for table "${tableName}" already exists`) + } else { + throw error + } + } +} + +/** + * Utility function to remove an index from a table. + * Sequelize implemets it using DROP INDEX IF EXISTS, so it won't throw an error if the index doesn't exist. + * + * @param {import('sequelize').QueryInterface} queryInterface + * @param {import ('../Logger')} logger + * @param {string} tableName + * @param {string[]} columns + */ +async function removeIndex(queryInterface, logger, tableName, columns) { + logger.info(`${loggerPrefix} removing index [${columns.join(', ')}] from table "${tableName}"`) + await queryInterface.removeIndex(tableName, columns) + logger.info(`${loggerPrefix} removed index [${columns.join(', ')}] from table "${tableName}"`) +} + +module.exports = { up, down } diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index bed96631..2aa41b70 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -1061,6 +1061,9 @@ class LibraryItem extends Model { { fields: ['libraryId', 'mediaType'] }, + { + fields: ['libraryId', 'mediaType', 'size'] + }, { fields: ['libraryId', 'mediaId', 'mediaType'] }, From 0444829a9f99b1d42ec00ce228f8c22eaa116e05 Mon Sep 17 00:00:00 2001 From: mikiher Date: Wed, 1 Jan 2025 08:37:57 +0200 Subject: [PATCH 2/3] Add index on duration --- server/migrations/v2.17.7-add-indices.js | 2 ++ server/models/Book.js | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/server/migrations/v2.17.7-add-indices.js b/server/migrations/v2.17.7-add-indices.js index 3d70ba20..b3821de8 100644 --- a/server/migrations/v2.17.7-add-indices.js +++ b/server/migrations/v2.17.7-add-indices.js @@ -22,6 +22,7 @@ async function up({ context: { queryInterface, logger } }) { logger.info(`${loggerPrefix} UPGRADE BEGIN: ${migrationName}`) await addIndex(queryInterface, logger, 'libraryItems', ['libraryId', 'mediaType', 'size']) + await addIndex(queryInterface, logger, 'books', ['duration']) logger.info(`${loggerPrefix} UPGRADE END: ${migrationName}`) } @@ -37,6 +38,7 @@ async function down({ context: { queryInterface, logger } }) { logger.info(`${loggerPrefix} DOWNGRADE BEGIN: ${migrationName}`) await removeIndex(queryInterface, logger, 'libraryItems', ['libraryId', 'mediaType', 'size']) + await removeIndex(queryInterface, logger, 'books', ['duration']) logger.info(`${loggerPrefix} DOWNGRADE END: ${migrationName}`) } diff --git a/server/models/Book.js b/server/models/Book.js index f7341db9..a904f536 100644 --- a/server/models/Book.js +++ b/server/models/Book.js @@ -321,10 +321,10 @@ class Book extends Model { // }, { fields: ['publishedYear'] + }, + { + fields: ['duration'] } - // { - // fields: ['duration'] - // } ] } ) From 46247ecf7897af954c965d58ca25b37bda72f4da Mon Sep 17 00:00:00 2001 From: mikiher Date: Wed, 1 Jan 2025 08:41:27 +0200 Subject: [PATCH 3/3] Update migrations changelog --- server/migrations/changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/server/migrations/changelog.md b/server/migrations/changelog.md index c2de4693..3b5a5626 100644 --- a/server/migrations/changelog.md +++ b/server/migrations/changelog.md @@ -12,3 +12,4 @@ Please add a record of every database migration that you create to this file. Th | v2.17.4 | v2.17.4-use-subfolder-for-oidc-redirect-uris | Save subfolder to OIDC redirect URIs to support existing installations | | v2.17.5 | v2.17.5-remove-host-from-feed-urls | removes the host (serverAddress) from URL columns in the feeds and feedEpisodes tables | | v2.17.6 | v2.17.6-share-add-isdownloadable | Adds the isDownloadable column to the mediaItemShares table | +| v2.17.7 | v2.17.7-add-indices | Adds indices to the libraryItems and books tables to reduce query times |