Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-16388

Aggregation operation not using any fields should use an appropriate index instead of collection scan

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            anil.kumar Anil Kumar
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: