2017-08-14 159 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

另外,不要隨意創建索引,而是希望索引數量最少,以滿足最多的查詢量。 –