【问题标题】:Mongo Aggregate not using IndexMongo 聚合不使用索引
【发布时间】:2014-08-05 08:59:16
【问题描述】:

我的 mongo find 查询正在使用索引,但如果我使用聚合实现相同的功能,它不使用索引。

db.collection1.find({Attribute8: "s1000",Attribute9: "s1000"}).sort({Attribute10: 1})

“用于查找的光标”:“BtreeCursor Attribute8_1_Attribute9_1_Attribute10_1”

 db.collection1.aggregate([
      {
        $match: {
          Attribute8: "s1000",
          Attribute9: "s1000"
        }
      },
      {
        $sort: {
         Attribute10: 1
        }
      }
    ])

“聚合使用的光标”:“BtreeCursor”。

谁能告诉我哪里出错了。我的目标是在聚合方法中使用索引。 提前致谢。

【问题讨论】:

  • 如何发布您的完整解释输出和您正在调用的命令语法。这应该有助于更好地看待事情。

标签: mongodb aggregation-framework


【解决方案1】:

经过一番挖掘,问题是以下类型的使用限制:

Symbol、MinKey、MaxKey、DBRef、Code 和 CodeWScope

在这种情况下,符号用于包含字符串值,因此索引不起作用。

请尝试在聚合选项中使用 Number en set explain 为 true。


[编辑] 我之前的回答是错误的。

聚合管道正在使用“BtreeCursor”(仅当定义的字段具有索引时)运行 $match 查询并且确实使用了保证索引,请检查“indexBound”进行验证。

确保整个集合在“Attribute08”上有一个索引

db.temps.ensureIndex({Attribute08:1})

$match 与索引的字段:

db.temps.aggregate([{$match:{Attribute08:"s1000"}}],{explain:true}) “所有计划”:[ { “光标”:“BtreeCursor”, “isMultiKey”:假, “scanAndOrder”:假, “索引边界”:{ “属性08”:[ [ "s1000", “s1000” ] ] } } ]

在没有索引的字段的 $match 下方:

db.temps.aggregate([{$match:{Attribute09:"s1000"}}],{explain:true}) “所有计划”:[ { “光标”:“基本光标”, “isMultiKey”:假, “scanAndOrder”:假 } ]

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多