我正在尝试更新大约 200GB mongo 集合中的 255k 份文档。如果我对查询运行解释计划,则获胜计划会命中正确的索引。但是当我运行执行相同查询的 node js 脚本时,索引丢失了。 我发现始终使用索引的唯一方法是添加提示。
因此,如果我添加了 255k 条记录的提示更新,则会在几秒钟内以小时/天为单位进行更新...:)
我真的很想了解为什么 mongo 会错过索引并解释简单的分数。
还有一些事情:
下面是数据模式、现有索引和节点js脚本的一部分。
//Existing indexes
schema.index({ executed: -1, account: 1, status: 1, type: 1, retry: 1, _id: 1 });
schema.index({ id: 1 });
schema.index({ status: 1, deleted: 1, account: 1, updatedAt: -1 });
schema.index({ account: 1, deleted: 1, updatedAt: -1 });
schema.index({ type: 1, deleted: 1, account: 1 });
schema.index({ deleted: 1, account: 1, status: 1 });
schema.index({ willRun: 1, type: 1, deleted: 1 });
schema.index({ cases: 1, deleted: 1 });
schema.index({ procedure: 1, deleted: 1, type: 1, scheduled: -1, cases: 1 });
schema.index({ account: 1, status: 1, executed: 1, type: 1, retry: 1, _id: 1 });
//part of script
let tasksList = await tasks
.find(
{
account: mongoose.Types.ObjectId(accountId),
status: 0,
executed: {
$lte: '2020-10-16T06:01:17.171Z' // new Date(deleteDate)
},
type: {
$ne: 'taskForDelete'
},
retry: {
$ne: 5
}
}, { _id: 1 }).hint({ account: 1, status: 1, executed: 1, type: 1, retry: 1, _id: 1 });