From e145330c3701cc3764e549d84df04ee231a6d0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 3 Apr 2026 20:17:06 -0400 Subject: [PATCH] perf: convert expireAt index to partial no need to store null in index for all documents --- src/database/mongo.js | 8 ++++-- src/upgrades/4.11.0/expireAt-partial-index.js | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/upgrades/4.11.0/expireAt-partial-index.js diff --git a/src/database/mongo.js b/src/database/mongo.js index 4e2ba46a66..4179111812 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -87,9 +87,13 @@ mongoModule.createIndices = async function () { await collection.createIndex({ _key: 1, score: -1 }, { background: true }); await collection.createIndex({ _key: 1, value: -1 }, { background: true, unique: true, sparse: true }); await collection.createIndex( - { members: 1, _key: 1}, { background: true, partialFilterExpression: { members: { $exists: true } } } + { members: 1, _key: 1}, + { background: true, partialFilterExpression: { members: { $exists: true } } } + ); + await collection.createIndex( + { expireAt: 1 }, + { expireAfterSeconds: 0, background: true, partialFilterExpression: { expireAt: { $exists: true } } }, ); - await collection.createIndex({ expireAt: 1 }, { expireAfterSeconds: 0, background: true }); winston.info('[database] Checking database indices done!'); }; diff --git a/src/upgrades/4.11.0/expireAt-partial-index.js b/src/upgrades/4.11.0/expireAt-partial-index.js new file mode 100644 index 0000000000..6c47911c06 --- /dev/null +++ b/src/upgrades/4.11.0/expireAt-partial-index.js @@ -0,0 +1,26 @@ +'use strict'; + + +const db = module.parent.require('./database'); + +module.exports = { + name: 'Change expireAt index to partial index', + timestamp: Date.UTC(2026, 3, 3), + method: async function () { + const nconf = require.main.require('nconf'); + const isMongo = nconf.get('database') === 'mongo'; + if (!isMongo) { + return; + } + await db.client.collection('objects').dropIndex('expireAt_1'); + await db.client.collection('objects').createIndex( + { expireAt: 1 }, + { + expireAfterSeconds: 0, + background: true, + partialFilterExpression: { expireAt: { $exists: true } }, + }, + ); + }, +}; +