Projections that could utilize a covered index do not

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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" : "..."
      }
      */
      

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

              Created:
              Updated:
              Resolved: