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

Projections that could utilize a covered index do not

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • None
    • None
    • Querying
    • None

    Description

      When a query's projection includes fields that are covered by an index, the covering index is not used.

      Notice in this example that the first query is indexOnly: false, but the second (hinted) query is indexOnly: true.

      c = db.c;
      c.drop();
       
      c.ensureIndex({_id: 1, a: 1});
       
      c.save({_id: "i", a: "x"});
       
      c.find({_id: "i"}, {a: 1}).explain();
      /*
      {
      	"cursor" : "BtreeCursor _id_",
      	"isMultiKey" : false,
      	"n" : 1,
      	"nscannedObjects" : 1,
      	"nscanned" : 1,
      	"nscannedObjectsAllPlans" : 1,
      	"nscannedAllPlans" : 1,
      	"scanAndOrder" : false,
      	"indexOnly" : false,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"millis" : 0,
      	"indexBounds" : {
      		"_id" : [
      			[
      				"i",
      				"i"
      			]
      		]
      	},
      	"server" : "..."
      }
      */
       
      c.find({_id: "i"}, {a: 1}).hint({_id: 1, a: 1}).explain();
      /*
      {
      	"cursor" : "BtreeCursor _id_1_a_1",
      	"isMultiKey" : false,
      	"n" : 1,
      	"nscannedObjects" : 1,
      	"nscanned" : 1,
      	"nscannedObjectsAllPlans" : 1,
      	"nscannedAllPlans" : 1,
      	"scanAndOrder" : false,
      	"indexOnly" : true,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"millis" : 0,
      	"indexBounds" : {
      		"_id" : [
      			[
      				"i",
      				"i"
      			]
      		],
      		"a" : [
      			[
      				{
      					"$minElement" : 1
      				},
      				{
      					"$maxElement" : 1
      				}
      			]
      		]
      	},
      	"server" : "..."
      }
      */

      Attachments

        Activity

          People

            Unassigned Unassigned
            vincetg2 Vince Garcia
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: