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

Running an aggregate command with a $project stage with {$meta: "indexKey"} and a $limit stage involves a FETCH

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Optimization

      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)
       }
      

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            cheahuychou.mao@mongodb.com Cheahuychou Mao
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: