2013-10-25 107 views
12

我在mongo 2.4.4中有一个match-unwind-group-sort聚合管道,我需要加快聚合速度。mongodb聚合框架的索引优化

匹配操作由16个字段的范围查询组成。我已使用.explain()方法来优化范围查询(即创建复合索引)。是否有类似的功能来优化聚合?我正在寻找类似的东西:

db.col.aggregate([]).explain() 

另外,我是否正确地专注于索引优化?

+1

有在最新的不稳定:https://jira.mongodb.org/browse/SERVER-4504但在那之前没有,没有,没有指标是用过去的mathc因此指数optimsation不一个好的路径 – Sammaye

+0

@Sammaye这是错误的,匹配肯定会使用索引,就像排序。 –

+0

@AsyaKamsky这就是我刚才所说的,我实际上说过了匹配,即在$ group – Sammaye

回答

14

对于第一个问题,是的,你可以解释聚合。

db.collection.runCommand("aggregate", {pipeline: YOUR_PIPELINE, explain: true}) 

对于第二个,创建优化的范围查询也将适用于聚集管道$比赛阶段,如果它们出现在管道开始的索引。所以你正确地关注索引优化。

请参阅Pipeline Operators and Indexes

更新2

更多解释:2.4版本,它是不可靠的;在2.6+上它不提供查询执行数据。 https://groups.google.com/forum/#!topic/mongodb-user/2LzAkyaNqe0

更新1

聚合的文稿上的MongoDB 2.4.5解释。

$ mongo so 
MongoDB shell version: 2.4.5 
connecting to: so 
> db.q19329239.runCommand("aggregate", {pipeline: [{$group: {_id: '$user.id', hits: {$sum: 1}}}, {$match: {hits: {$gt: 10}}}], explain: true}) 
{ 
    "serverPipeline" : [ 
     { 
      "query" : { 

      }, 
      "projection" : { 
       "user.id" : 1, 
       "_id" : 0 
      }, 
      "cursor" : { 
       "cursor" : "BasicCursor", 
       "isMultiKey" : false, 
       "n" : 1031, 
       "nscannedObjects" : 1031, 
       "nscanned" : 1031, 
       "nscannedObjectsAllPlans" : 1031, 
       "nscannedAllPlans" : 1031, 
       "scanAndOrder" : false, 
       "indexOnly" : false, 
       "nYields" : 0, 
       "nChunkSkips" : 0, 
       "millis" : 0, 
       "indexBounds" : { 

       }, 
       "allPlans" : [ 
        { 
         "cursor" : "BasicCursor", 
         "n" : 1031, 
         "nscannedObjects" : 1031, 
         "nscanned" : 1031, 
         "indexBounds" : { 

         } 
        } 
       ], 
       "server" : "ficrm-rafa.local:27017" 
      } 
     }, 
     { 
      "$group" : { 
       "_id" : "$user.id", 
       "hits" : { 
        "$sum" : { 
         "$const" : 1 
        } 
       } 
      } 
     }, 
     { 
      "$match" : { 
       "hits" : { 
        "$gt" : 10 
       } 
      } 
     } 
    ], 
    "ok" : 1 
} 

服务器版本。

$ mongo so 
MongoDB shell version: 2.4.5 
connecting to: so 
> db.version() 
2.4.5 
+0

中所说的那样,你只能在unstable中解释汇总查询,因此不应该认为你可以,你必须让这个人知道这不是生产准备好的并且版本不稳定并且很可能会更改 – Sammaye

+0

不,您可以在MongoDB 2.4中解释汇总。我一直这样做。 – Rafa

+0

真的吗?你能证明这一点吗?也许显示解释的输出? – Sammaye