Mongo错过了索引

时间:2021-05-03 07:05:54

标签: mongodb indexing mongodb-indexes

我正在尝试更新大约 200GB mongo 集合中的 255k 份文档。如果我对查询运行解释计划,则获胜计划会命中正确的索引。但是当我运行执行相同查询的 node js 脚本时,索引丢失了。 我发现始终使用索引的唯一方法是添加提示。

因此,如果我添加了 255k 条记录的提示更新,则会在几秒钟内以小时/天为单位进行更新...:)

我真的很想了解为什么 mongo 会错过索引并解释简单的分数。

还有一些事情:

  • 索引的顺序是否重要。有哪些好的做法
  • 过滤器中的字段是否需要与索引中的顺序相同
  • 索引在使用 neq(不等于)过滤器时的表现
  • 当 mongo 发现多个性能相同的响应时,它会做什么。是否执行limit(1)
  • 即使适当的索引非常快,他怎么会错过索引

下面是数据模式、现有索引和节点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 });

0 个答案:

没有答案