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

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Minor - P4
    • Resolution: Duplicate
    • None
    • None
    • Aggregation Framework
    • None
    • Fully Compatible

    Description

      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.

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: