[SERVER-78453] Running an aggregate command with a $project stage with {$meta: "indexKey"} and a $limit stage involves a FETCH Created: 26/Jun/23  Updated: 25/Jul/23  Resolved: 25/Jul/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Cheahuychou Mao Assignee: Backlog - Query Optimization
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Optimization
Participants:

 Description   

Here is the explain output for this aggregate command. That is there is FETCH although no fetching is necessary.

const cmdObj = {
        "aggregate": "testColl",
        "pipeline": [
            {"$project": {"_id": 0, "key": {"$meta": "indexKey"}}},
            {"$limit": 10000},
 
        ],
        "allowDiskUse": true,
        "cursor": {"batchSize": 101},
        "collation": {"locale": "simple"},
        "hint": {"a": 1}
    };

{
 	"explainVersion" : "1",
 	"stages" : [
 		{
 			"$cursor" : {
 				"queryPlanner" : {
 					"namespace" : "testDb.testColl",
 					"indexFilterSet" : false,
 					"parsedQuery" : {
 
 					},
 					"queryHash" : "FCBE9F38",
 					"planCacheKey" : "FCBE9F38",
 					"maxIndexedOrSolutionsReached" : false,
 					"maxIndexedAndSolutionsReached" : false,
 					"maxScansToExplodeReached" : false,
 					"winningPlan" : {
 						"stage" : "LIMIT",
 						"limitAmount" : 10000,
 						"inputStage" : {
 							"stage" : "FETCH",
 							"inputStage" : {
 								"stage" : "IXSCAN",
 								"keyPattern" : {
 									"a" : 1
 								},
 								"indexName" : "a_1",
 								"isMultiKey" : false,
 								"multiKeyPaths" : {
 									"a" : [ ]
 								},
 								"isUnique" : false,
 								"isSparse" : false,
 								"isPartial" : false,
 								"indexVersion" : 2,
 								"direction" : "forward",
 								"indexBounds" : {
 									"a" : [
 										"[MinKey, MaxKey]"
 									]
 								}
 							}
 						}
 					},
 					"rejectedPlans" : [ ]
 				},
 				"executionStats" : {
 					"executionSuccess" : true,
 					"nReturned" : 10000,
 					"executionTimeMillis" : 23,
 					"totalKeysExamined" : 10000,
 					"totalDocsExamined" : 10000,
 					"executionStages" : {
 						"stage" : "LIMIT",
 						"nReturned" : 10000,
 						"executionTimeMillisEstimate" : 3,
 						"works" : 10001,
 						"advanced" : 10000,
 						"needTime" : 0,
 						"needYield" : 0,
 						"saveState" : 11,
 						"restoreState" : 11,
 						"isEOF" : 1,
 						"limitAmount" : 10000,
 						"inputStage" : {
 							"stage" : "FETCH",
 							"nReturned" : 10000,
 							"executionTimeMillisEstimate" : 3,
 							"works" : 10000,
 							"advanced" : 10000,
 							"needTime" : 0,
 							"needYield" : 0,
 							"saveState" : 11,
 							"restoreState" : 11,
 							"isEOF" : 0,
 							"docsExamined" : 10000,
 							"alreadyHasObj" : 0,
 							"inputStage" : {
 								"stage" : "IXSCAN",
 								"nReturned" : 10000,
 								"executionTimeMillisEstimate" : 2,
 								"works" : 10000,
 								"advanced" : 10000,
 								"needTime" : 0,
 								"needYield" : 0,
 								"saveState" : 11,
 								"restoreState" : 11,
 								"isEOF" : 0,
 								"keyPattern" : {
 									"a" : 1
 								},
 								"indexName" : "a_1",
 								"isMultiKey" : false,
 								"multiKeyPaths" : {
 									"a" : [ ]
 								},
 								"isUnique" : false,
 								"isSparse" : false,
 								"isPartial" : false,
 								"indexVersion" : 2,
 								"direction" : "forward",
 								"indexBounds" : {
 									"a" : [
 										"[MinKey, MaxKey]"
 									]
 								},
 								"keysExamined" : 10000,
 								"seeks" : 1,
 								"dupsTested" : 0,
 								"dupsDropped" : 0
 							}
 						}
 					},
 					"allPlansExecution" : [ ]
 				}
 			},
 			"nReturned" : NumberLong(10000),
 			"executionTimeMillisEstimate" : NumberLong(17)
 		},
 		{
 			"$project" : {
 				"key" : {
 					"$meta" : "indexKey"
 				},
 				"_id" : false
 			},
 			"nReturned" : NumberLong(10000),
 			"executionTimeMillisEstimate" : NumberLong(17)
 		}
 	],
 	"serverInfo" : {
 		"host" : "ip-",
 		"port" : 20040,
 		"version" : "7.1.0-alpha-1667-g39b59fc",
 		"gitVersion" : "39b59fc567cb4e69a948ebcf119a70bb98fa682d"
 	},
 	"serverParameters" : {
 		"internalQueryFacetBufferSizeBytes" : 104857600,
 		"internalQueryFacetMaxOutputDocSizeBytes" : 104857600,
 		"internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600,
 		"internalDocumentSourceGroupMaxMemoryBytes" : 104857600,
 		"internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600,
 		"internalQueryProhibitBlockingMergeOnMongoS" : 0,
 		"internalQueryMaxAddToSetBytes" : 104857600,
 		"internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600,
 		"internalQueryFrameworkControl" : "trySbeEngine"
 	},
 	"command" : {
 		"aggregate" : "testColl",
 		"pipeline" : [
 			{
 				"$project" : {
 					"_id" : 0,
 					"key" : {
 						"$meta" : "indexKey"
 					}
 				}
 			},
 			{
 				"$limit" : 10000
 			}
 		],
 		"allowDiskUse" : true,
 		"cursor" : {
 			"batchSize" : 101
 		},
 		"collation" : {
 			"locale" : "simple"
 		},
 		"hint" : {
 			"a" : 1
 		},
 		"$db" : "testDb"
 	},
 	"ok" : 1,
 	"$clusterTime" : {
 		"clusterTime" : Timestamp(1687800460, 19000),
 		"signature" : {
 			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
 			"keyId" : NumberLong(0)
 		}
 	},
 	"operationTime" : Timestamp(1687800460, 19000)
 }


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