[SERVER-13128] Explain field 'nscannedObjects' incorrect for indexed $or queries Created: 11/Mar/14  Updated: 11/Jul/16  Resolved: 13/Mar/14

Status: Closed
Project: Core Server
Component/s: Diagnostics, Querying
Affects Version/s: 2.6.0-rc1
Fix Version/s: 2.6.0-rc2

Type: Bug Priority: Major - P3
Reporter: J Rassi Assignee: J Rassi
Resolution: Done Votes: 0
Labels: 26qa
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-12529 incorrect explain output for covered ... Closed
Operating System: ALL
Participants:

 Description   

Explain for indexed $or queries always incorrectly sets 'nscannedObjects' to zero, even when the query is not covered.

To reproduce:

> db.foo.ensureIndex({a:1})
> db.foo.ensureIndex({b:1})
> for(i=0;i<10;i++){db.foo.insert({a:1,b:1})}
> db.foo.find({$or:[{a:1},{b:1}]}).explain().nscannedObjects
0 // should be 20 (well, arguably should be 10, but at least in 2.4.9 this is 20)

Full explain output:

> db.foo.find({$or:[{a:1},{b:1}]}).explain(true)
{
	"clauses" : [
		{
			"cursor" : "BtreeCursor a_1",
			"isMultiKey" : false,
			"n" : 10,
			"nscannedObjects" : 0,
			"nscanned" : 10,
			"scanAndOrder" : false,
			"indexOnly" : false,
			"nChunkSkips" : 0,
			"indexBounds" : {
				"a" : [
					[
						1,
						1
					]
				]
			}
		},
		{
			"cursor" : "BtreeCursor b_1",
			"isMultiKey" : false,
			"n" : 10,
			"nscannedObjects" : 0,
			"nscanned" : 10,
			"scanAndOrder" : false,
			"indexOnly" : false,
			"nChunkSkips" : 0,
			"indexBounds" : {
				"b" : [
					[
						1,
						1
					]
				]
			}
		}
	],
	"cursor" : "QueryOptimizerCursor",
	"n" : 10,
	"nscannedObjects" : 0,
	"nscanned" : 20,
	"nscannedObjectsAllPlans" : 0,
	"nscannedAllPlans" : 20,
	"scanAndOrder" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 1,
	"allPlans" : [
		{
			"clauses" : [
				{
					"cursor" : "BtreeCursor a_1",
					"isMultiKey" : false,
					"n" : 10,
					"nscannedObjects" : 0,
					"nscanned" : 10,
					"scanAndOrder" : false,
					"indexOnly" : false,
					"nChunkSkips" : 0,
					"indexBounds" : {
						"a" : [
							[
								1,
								1
							]
						]
					}
				},
				{
					"cursor" : "BtreeCursor b_1",
					"isMultiKey" : false,
					"n" : 10,
					"nscannedObjects" : 0,
					"nscanned" : 10,
					"scanAndOrder" : false,
					"indexOnly" : false,
					"nChunkSkips" : 0,
					"indexBounds" : {
						"b" : [
							[
								1,
								1
							]
						]
					}
				}
			],
			"cursor" : "QueryOptimizerCursor",
			"n" : 10,
			"nscannedObjects" : 0,
			"nscanned" : 20,
			"scanAndOrder" : false,
			"nChunkSkips" : 0
		}
	],
	"server" : "Rassi-MacBook-Pro.local:27017",
	"filterSet" : false,
	"stats" : {
		"type" : "FETCH",
		"works" : 22,
		"yields" : 0,
		"unyields" : 0,
		"invalidates" : 0,
		"advanced" : 10,
		"needTime" : 11,
		"needFetch" : 0,
		"isEOF" : 1,
		"alreadyHasObj" : 0,
		"forcedFetches" : 0,
		"matchTested" : 0,
		"children" : [
			{
				"type" : "OR",
				"works" : 22,
				"yields" : 0,
				"unyields" : 0,
				"invalidates" : 0,
				"advanced" : 10,
				"needTime" : 11,
				"needFetch" : 0,
				"isEOF" : 1,
				"dupsTested" : 20,
				"dupsDropped" : 10,
				"locsForgotten" : 0,
				"matchTested_0" : 0,
				"matchTested_1" : 0,
				"children" : [
					{
						"type" : "IXSCAN",
						"works" : 11,
						"yields" : 0,
						"unyields" : 0,
						"invalidates" : 0,
						"advanced" : 10,
						"needTime" : 0,
						"needFetch" : 0,
						"isEOF" : 1,
						"keyPattern" : "{ a: 1.0 }",
						"boundsVerbose" : "field #0['a']: [1.0, 1.0]",
						"isMultiKey" : 0,
						"yieldMovedCursor" : 0,
						"dupsTested" : 0,
						"dupsDropped" : 0,
						"seenInvalidated" : 0,
						"matchTested" : 0,
						"keysExamined" : 10,
						"children" : [ ]
					},
					{
						"type" : "IXSCAN",
						"works" : 11,
						"yields" : 0,
						"unyields" : 0,
						"invalidates" : 0,
						"advanced" : 10,
						"needTime" : 0,
						"needFetch" : 0,
						"isEOF" : 1,
						"keyPattern" : "{ b: 1.0 }",
						"boundsVerbose" : "field #0['b']: [1.0, 1.0]",
						"isMultiKey" : 0,
						"yieldMovedCursor" : 0,
						"dupsTested" : 0,
						"dupsDropped" : 0,
						"seenInvalidated" : 0,
						"matchTested" : 0,
						"keysExamined" : 10,
						"children" : [ ]
					}
				]
			}
		]
	}
}
>



 Comments   
Comment by Githook User [ 13/Mar/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-13128 Fix nscannedObjects calculation for $or subtree explain
Branch: v2.6
https://github.com/mongodb/mongo/commit/3956365fccb2ba7e4a622b848ce69a9aa4c8b273

Comment by Githook User [ 13/Mar/14 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-13128 Fix nscannedObjects calculation for $or subtree explain
Branch: master
https://github.com/mongodb/mongo/commit/dc28d4a2d9336904ce302c9c3c8099cb27f402eb

Comment by J Rassi [ 11/Mar/14 ]

I suspect the cause of the issue is that the value of "covered" is not propagated to the explainPlan() calls for the OR children (that is, "covered" is set to the default of true in the explain performed on the index scan subtree):

https://github.com/mongodb/mongo/blob/25596dbf48b18a76d3d2cdfdf1fcf23a43e46316/src/mongo/db/query/explain_plan.cpp#L300

Generated at Thu Feb 08 03:30:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.