Planner sort analysis should not add FETCH stage if sort key included in index key pattern

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Done
    • Priority: Major - P3
    • 4.1.10
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • Fully Compatible
    • Query 2019-01-28, Query 2019-03-11, Query 2019-03-25
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Queries that require a blocking sort always fetch the full document from the collection to retrieve the sort key, even if the query plan includes an index scan over an index that has the sort key as a part of its key pattern. These queries should be covered instead.

      To reproduce:

      > db.foo.drop()
      true
      > db.foo.ensureIndex({a: 1, b: 1})
      > db.foo.find({a: {$gt: 0}}, {a: 1, b: 1, _id: 0}).sort({b: 1}).explain(true).stats
      {
      	"stage" : "PROJECTION",
      	"nReturned" : 0,
      	"executionTimeMillis" : 2,
      	"works" : 4,
      	"advanced" : 0,
      	"needTime" : 0,
      	"isEOF" : 1,
      	"invalidates" : 0,
      	"transformBy" : {
      		"_id" : 0,
      		"a" : 1,
      		"b" : 1
      	},
      	"inputStage" : {
      		"stage" : "SORT",
      		"nReturned" : 0,
      		"executionTimeMillis" : 2,
      		"works" : 4,
      		"advanced" : 0,
      		"needTime" : 2,
      		"isEOF" : 1,
      		"invalidates" : 0,
      		"sortPattern" : {
      			"b" : 1
      		},
      		"memUsage" : 0,
      		"memLimit" : 33554432,
      		"inputStage" : {
      			"stage" : "KEEP_MUTATIONS",
      			"nReturned" : 0,
      			"executionTimeMillis" : 0,
      			"works" : 2,
      			"advanced" : 0,
      			"needTime" : 1,
      			"isEOF" : 1,
      			"invalidates" : 0,
      			"inputStage" : {
      				"stage" : "FETCH",
      				"nReturned" : 0,
      				"executionTimeMillis" : 0,
      				"works" : 2,
      				"advanced" : 0,
      				"needTime" : 1,
      				"isEOF" : 1,
      				"invalidates" : 0,
      				"docsExamined" : 0,
      				"alreadyHasObj" : 0,
      				"inputStage" : {
      					"stage" : "IXSCAN",
      					"nReturned" : 0,
      					"executionTimeMillis" : 0,
      					"works" : 1,
      					"advanced" : 0,
      					"needTime" : 1,
      					"isEOF" : 1,
      					"invalidates" : 0,
      					"keysExamined" : 0,
      					"keyPattern" : {
      						"a" : 1,
      						"b" : 1
      					},
      					"isMultiKey" : false,
      					"indexBounds" : "field #0['a']: (0.0, inf.0], field #1['b']: [MinKey, MaxKey]",
      					"dupsTested" : 0,
      					"dupsDropped" : 0,
      					"seenInvalidated" : 0,
      					"matchTested" : 0
      				}
      			}
      		}
      	}
      }
      

              Assignee:
              James Wahlin
              Reporter:
              J Rassi (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              9 Start watching this issue

                Created:
                Updated:
                Resolved: