2017-08-14 188 views
0

我是Mongo的新手,并试图获得不同数量的用户。字段Id和状态不是单独编入索引的列,但是在这两个字段上都存在复合索引。我目前的查询是这样的,其中匹配条件根据需求而改变。调整Mongo查询

DBQuery.shellBatchSize = 1000000; 
db.getCollection('username').aggregate([ 
    {$match: 
    { Status: "A" 
    } }, 
{"$group" : {_id:"$Id", count:{$sum:1}}} 
]); 

反正我们可以更优化这个查询或集合添加并行运行,使我们能够更快地取得成果?

问候

+0

如果你想要,你可以使用'distinct'查询。这里是链接https://docs.mongodb.com/manual/reference/method/db.collection.distinct/ –

+0

直接不同将需要更长的时间在非索引字段 – user2854333

+0

那么按照mongo文档调整最好的方法是创建index .. https://docs.mongodb.com/manual/tutorial/optimize-query-performance-with-indexes-and-projections/ –

回答

0

你可以通过在聚合方法中的explain=true选项调整您的聚集管道。

db.getCollection('username').aggregate([ 
    {$match: { Status: "A" } }, 
    {"$group" : {_id:"$Id", count:{$sum:1}}}], 
{ explain: true }); 

这一操作将输出以下与

{ 
     "stages" : [ 
       { 
         "$cursor" : { 
           "query" : { 
             "Status" : "A" 
           }, 
           "fields" : { 
             "Id" : 1, 
             "_id" : 0 
           }, 
           "queryPlanner" : { 
             "plannerVersion" : 1, 
             "namespace" : "test.usernames", 
             "indexFilterSet" : false, 
             "parsedQuery" : { 
               "Status" : { 
                 "$eq" : "A" 
               } 
             }, 
             "winningPlan" : { 
               "stage" : "EOF" 
             }, 
             "rejectedPlans" : [ ] 
           } 
         } 
       }, 
       { 
         "$group" : { 
           "_id" : "$Id", 
           "count" : { 
             "$sum" : { 
               "$const" : 1 
             } 
           } 
         } 
       } 
     ], 
     "ok" : 1 
} 

如此工作,加快我们的查询,我们需要一个指数,以帮助管道的比赛的一部分,所以让我们Status

创建索引
> db.usernames.createIndex({Status:1}) 
{ 
    "createdCollectionAutomatically" : true, 
    "numIndexesBefore" : 1, 
    "numIndexesAfter" : 2, 
    "ok" : 1 
} 

如果我们现在再次运行解释,我们会得到下面的结果

{ 
     "stages" : [ 
       { 
         "$cursor" : { 
           "query" : { 
             "Status" : "A" 
           }, 
           "fields" : { 
             "Id" : 1, 
             "_id" : 0 
           }, 
           "queryPlanner" : { 
             "plannerVersion" : 1, 
             "namespace" : "test.usernames", 
             "indexFilterSet" : false, 
             "parsedQuery" : { 
               "Status" : { 
                 "$eq" : "A" 
               } 
             }, 
             "winningPlan" : { 
               "stage" : "FETCH", 
               "inputStage" : { 
                 "stage" : "IXSCAN", 
                 "keyPattern" : { 
                   "Status" : 1 
                 }, 
                 "indexName" : "Status_1", 
                 "isMultiKey" : false, 
                 "multiKeyPaths" : { 
                   "Status" : [ ] 
                 }, 
                 "isUnique" : false, 
                 "isSparse" : false, 
                 "isPartial" : false, 
                 "indexVersion" : 2, 
                 "direction" : "forward", 
                 "indexBounds" : { 
                   "Status" : [ 
                     "[\"A\", \"A\"]" 
                   ] 
                 } 
               } 
             }, 
             "rejectedPlans" : [ ] 
           } 
         } 
       }, 
       { 
         "$group" : { 
           "_id" : "$Id", 
           "count" : { 
             "$sum" : { 
               "$const" : 1 
             } 
           } 
         } 
       } 
     ], 
     "ok" : 1 
} 

我们现在可以直接看到这是使用索引。

https://docs.mongodb.com/manual/reference/explain-results/

+0

谢谢凯文。问题是我不能去创建索引,因为我只是DB的消费者。所以想要检查是否有一种方法可以不创建新的索引,或者是否有利用现有的组合索引。 – user2854333

+0

运行解释并查看它说了什么,如果可以,应该使用索引集。 (你可以通过当前配置的索引发送?'db.usernames.getIndexes()')。 –

+0

另外,不要随意创建索引,而是希望索引数量最少,以满足最多的查询量。 –