perf: convert expireAt index to partial

no need to store null in index for all documents
This commit is contained in:
Barış Soner Uşaklı
2026-04-03 20:17:06 -04:00
parent 0e79029b7f
commit e145330c37
2 changed files with 32 additions and 2 deletions

View File

@@ -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!');
};

View File

@@ -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 } },
},
);
},
};