-
Type: Improvement
-
Resolution: Duplicate
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
None
-
Fully Compatible
This is not an issue but some basic optimization for aggregation on the collection when no fields are required from the document to complete the aggregation.
The following operation is performing aggregation without using any field from the document, but still results in the full collection scan:
db.places.aggregate([{$group: {_id: null, count: {$sum: 1} } }], {explain: true})
{ "stages" : [ { "$cursor" : { "query" : { }, "fields" : { "_id" : 0, "$noFieldsNeeded" : 1 }, "plan" : { "cursor" : "BasicCursor", "isMultiKey" : false, "scanAndOrder" : false, "allPlans" : [ { "cursor" : "BasicCursor", "isMultiKey" : false, "scanAndOrder" : false } ] } } }, { "$group" : { "_id" : { "$const" : null }, "count" : { "$sum" : { "$const" : 1 } } } } ], "ok" : 1 }
Ideally, in non-sharded environment, this should be traversing the _id index and on sharded environment it should be traversing the shard-key index.
- duplicates
-
SERVER-12015 Reenable use of covering indexes with aggregation
- Closed