[SERVER-35973] Explain execution stats for $sample are not reported correctly Created: 05/Jul/18  Updated: 29/Oct/23  Resolved: 16/Jul/18

Status: Closed
Project: Core Server
Component/s: Aggregation Framework, Diagnostics
Affects Version/s: 4.0.0, 4.1.1
Fix Version/s: 4.1.1

Type: Bug Priority: Major - P3
Reporter: David Storch Assignee: David Storch
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-35967 $sample with explain(true) hangs Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Query 2018-07-16, Query 2018-07-30
Participants:

 Description   

The [MULTI_ITERATOR] stage used as part of $sample execution reports all zeros in place of real execution stats:

MongoDB Enterprise > db.c.drop()
false
MongoDB Enterprise > for (let i = 0; i < 1000; i++) { db.c.insert({_id: i}); }
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.c.explain(true).aggregate([{$sample: {size: 10}}])
{
	"stages" : [
		{
			"$cursor" : {
				"query" : {
 
				},
				"queryPlanner" : {
					"plannerVersion" : 1,
					"namespace" : "test.c",
					"indexFilterSet" : false,
					"winningPlan" : {
						"stage" : "MULTI_ITERATOR"
					},
					"rejectedPlans" : [ ]
				},
				"executionStats" : {
					"executionSuccess" : true,
					"nReturned" : 0,
					"executionTimeMillis" : 43,
					"totalKeysExamined" : 0,
					"totalDocsExamined" : 0,
					"executionStages" : {
						"stage" : "MULTI_ITERATOR",
						"nReturned" : 0,
						"executionTimeMillisEstimate" : 0,
						"works" : 0,
						"advanced" : 0,
						"needTime" : 0,
						"needYield" : 0,
						"saveState" : 0,
						"restoreState" : 0,
						"isEOF" : 0,
						"invalidates" : 0
					},
					"allPlansExecution" : [ ]
				}
			}
		},
		{
			"$sampleFromRandomCursor" : {
				"size" : NumberLong(10)
			}
		}
	],
	"ok" : 1
}

Notice that the counts ("works", "advanced", "nReturned", etc.) associated with MULTI_ITERATOR in the "executionStats" section are all zeros. This is a reporting issue in the server; the query clearly returns results when run without explain:

MongoDB Enterprise > db.c.aggregate([{$sample: {size: 10}}])
{ "_id" : 266 }
{ "_id" : 789 }
{ "_id" : 999 }
{ "_id" : 987 }
{ "_id" : 685 }
{ "_id" : 979 }
{ "_id" : 633 }
{ "_id" : 484 }
{ "_id" : 847 }
{ "_id" : 22 }

It looks like the issue is that MultiIterator::getStats() is not implemented correctly.



 Comments   
Comment by Githook User [ 16/Jul/18 ]

Author:

{'username': 'dstorch', 'name': 'David Storch', 'email': 'david.storch@10gen.com'}

Message: SERVER-35973 Report MULTI_ITERATOR common exec stats correctly.
Branch: master
https://github.com/mongodb/mongo/commit/4021ba116f22e5c2010f367ab6c038864d8bceb7

Generated at Thu Feb 08 04:41:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.