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

Query with SORT_MERGE stage returns same document twice

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.6.0-rc1
    • Querying

    Description

      The example below shows a query that returns "n" larger than the size of the collection.

      Set up:

      > for(i=0;i<10000;i++){db.foo.insert({a:1,b:1,c:1})}

      Issue a long-running series of updates:

      > for(i=0;;i++){eval("db.foo.update({},{$set:{f"+i+":true}},false,true)");}

      Meanwhile, run the following query (and note that itcount() will return values greater than 10000, the number of documents in the collection):

      > db.foo.find({$or:[{a:1}, {b:1}]}).sort({c:1}).itcount()
      7979
      > db.foo.find({$or:[{a:1}, {b:1}]}).sort({c:1}).itcount()
      10000
      > db.foo.find({$or:[{a:1}, {b:1}]}).sort({c:1}).itcount()
      11038

      Full explain:

      > db.foo.find({$or:[{a:1}, {b:1}]}).sort({c:1}).explain(true)
      {
      	"clauses" : [
      		{
      			"cursor" : "BtreeCursor a_1_c_1",
      			"isMultiKey" : false,
      			"n" : 11126,
      			"nscannedObjects" : 0,
      			"nscanned" : 11126,
      			"scanAndOrder" : false,
      			"indexOnly" : false,
      			"nChunkSkips" : 0,
      			"indexBounds" : {
      				"a" : [
      					[
      						1,
      						1
      					]
      				],
      				"c" : [
      					[
      						{
      							"$minElement" : 1
      						},
      						{
      							"$maxElement" : 1
      						}
      					]
      				]
      			}
      		},
      		{
      			"cursor" : "BtreeCursor b_1_c_1",
      			"isMultiKey" : false,
      			"n" : 11126,
      			"nscannedObjects" : 0,
      			"nscanned" : 11126,
      			"scanAndOrder" : false,
      			"indexOnly" : false,
      			"nChunkSkips" : 0,
      			"indexBounds" : {
      				"b" : [
      					[
      						1,
      						1
      					]
      				],
      				"c" : [
      					[
      						{
      							"$minElement" : 1
      						},
      						{
      							"$maxElement" : 1
      						}
      					]
      				]
      			}
      		}
      	],
      	"cursor" : "QueryOptimizerCursor",
      	"n" : 11126,
      	"nscannedObjects" : 0,
      	"nscanned" : 22252,
      	"nscannedObjectsAllPlans" : 0,
      	"nscannedAllPlans" : 22252,
      	"scanAndOrder" : false,
      	"nYields" : 267,
      	"nChunkSkips" : 0,
      	"millis" : 2559,
      	"allPlans" : [
      		{
      			"clauses" : [
      				{
      					"cursor" : "BtreeCursor a_1_c_1",
      					"isMultiKey" : false,
      					"n" : 11126,
      					"nscannedObjects" : 0,
      					"nscanned" : 11126,
      					"scanAndOrder" : false,
      					"indexOnly" : false,
      					"nChunkSkips" : 0,
      					"indexBounds" : {
      						"a" : [
      							[
      								1,
      								1
      							]
      						],
      						"c" : [
      							[
      								{
      									"$minElement" : 1
      								},
      								{
      									"$maxElement" : 1
      								}
      							]
      						]
      					}
      				},
      				{
      					"cursor" : "BtreeCursor b_1_c_1",
      					"isMultiKey" : false,
      					"n" : 11126,
      					"nscannedObjects" : 0,
      					"nscanned" : 11126,
      					"scanAndOrder" : false,
      					"indexOnly" : false,
      					"nChunkSkips" : 0,
      					"indexBounds" : {
      						"b" : [
      							[
      								1,
      								1
      							]
      						],
      						"c" : [
      							[
      								{
      									"$minElement" : 1
      								},
      								{
      									"$maxElement" : 1
      								}
      							]
      						]
      					}
      				}
      			],
      			"cursor" : "QueryOptimizerCursor",
      			"n" : 11126,
      			"nscannedObjects" : 0,
      			"nscanned" : 22252,
      			"scanAndOrder" : false,
      			"nChunkSkips" : 0
      		}
      	],
      	"server" : "Rassi-MacBook-Pro.local:27017",
      	"filterSet" : false,
      	"stats" : {
      		"type" : "FETCH",
      		"works" : 33394,
      		"yields" : 267,
      		"unyields" : 267,
      		"invalidates" : 3365,
      		"advanced" : 11126,
      		"needTime" : 22254,
      		"needFetch" : 13,
      		"isEOF" : 1,
      		"alreadyHasObj" : 0,
      		"forcedFetches" : 0,
      		"matchTested" : 0,
      		"children" : [
      			{
      				"type" : "SORT_MERGE",
      				"works" : 33380,
      				"yields" : 267,
      				"unyields" : 267,
      				"invalidates" : 3365,
      				"advanced" : 11126,
      				"needTime" : 22254,
      				"needFetch" : 0,
      				"isEOF" : 1,
      				"dupsTested" : 22252,
      				"dupsDropped" : 11126,
      				"forcedFetches" : 0,
      				"children" : [
      					{
      						"type" : "IXSCAN",
      						"works" : 11127,
      						"yields" : 267,
      						"unyields" : 267,
      						"invalidates" : 3365,
      						"advanced" : 11126,
      						"needTime" : 0,
      						"needFetch" : 0,
      						"isEOF" : 1,
      						"keyPattern" : "{ a: 1.0, c: 1.0 }",
      						"boundsVerbose" : "field #0['a']: [1.0, 1.0], field #1['c']: [MinKey, MaxKey]",
      						"isMultiKey" : 0,
      						"yieldMovedCursor" : 0,
      						"dupsTested" : 0,
      						"dupsDropped" : 0,
      						"seenInvalidated" : 0,
      						"matchTested" : 0,
      						"keysExamined" : 11126,
      						"children" : [ ]
      					},
      					{
      						"type" : "IXSCAN",
      						"works" : 11127,
      						"yields" : 267,
      						"unyields" : 267,
      						"invalidates" : 3365,
      						"advanced" : 11126,
      						"needTime" : 0,
      						"needFetch" : 0,
      						"isEOF" : 1,
      						"keyPattern" : "{ b: 1.0, c: 1.0 }",
      						"boundsVerbose" : "field #0['b']: [1.0, 1.0], field #1['c']: [MinKey, MaxKey]",
      						"isMultiKey" : 0,
      						"yieldMovedCursor" : 0,
      						"dupsTested" : 0,
      						"dupsDropped" : 0,
      						"seenInvalidated" : 0,
      						"matchTested" : 0,
      						"keysExamined" : 11126,
      						"children" : [ ]
      					}
      				]
      			}
      		]
      	}
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            rassi J Rassi
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: