[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[],[],null,["# Analyze query execution with Query Explain\n==========================================\n\nThis page describes how to retrieve query execution information when you execute a query.\n\nUse Query Explain\n-----------------\n\nYou can use Query Explain to understand how your queries are being executed.\nThis provides details that you can use to [optimize your queries](/firestore/mongodb-compatibility/docs/optimize-query-performance).\n\nYou can use Query Explain through the Google Cloud console or the `explain` command. \n\n##### Console\n\nExecute a query in the Query Editor and open the **Explanation** tab:\n\n1. In the Google Cloud console, go to the **Databases** page.\n\n [Go to Databases](https://console.cloud.google.com/firestore/databases)\n2. From the list of databases, select a Firestore with MongoDB compatibility database. The Google Cloud console opens the **Firestore Explorer** for that database.\n3. Enter a query in the query editor and click **Run**.\n4. Click the **Explanation** tab to view the query analysis output.\n\n##### MongoDB API\n\nQuery Explain in the MongoDB API is supported through the\n[`explain`](https://www.mongodb.com/docs/manual/reference/command/explain/)\ncommand which you can use in tools such as Mongo Shell and Compass.\n\n\nThe `explain` command is supported with the `aggregate`,\n`find`, `distinct`, and `count`\ncommands, for example: \n\n```text\ndb.collection.explain.find(...)\n```\n\n\nYou can also use the `explain()` method, for example: \n\n```text\ndb.collection.find({QUERY}).explain()\n```\n\n###### Limitations\n\nNote the following limitations and differences:\n\n- Query Explain does not support commands which return a cursor. For example,\n invoking explain by calling the following command directly is not supported:\n\n ```verilog\n db.collection.aggregate(..., explain: true)\n ```\n- Query Explain is only supported on the\n `find`, `aggregate`, `count`, and `distinct`\n commands.\n\n- The `Verbosity` and `Comment` options of Query Explain\n are not supported through the MongoDB API. The behaviour matches the\n `executionStats` option. The `allPlansExecution` and\n `queryPlanner` options are ignored if provided.\n\nAnalysis\n--------\n\nThe output of Query Explain contains two main components-the Summary Statistics and Execution Tree.\nConsider this query as an example: \n\n db.order.aggregate(\n [\n { \"$match\": { \"user_id\": 1234 } },\n { \"$sort\": { \"date_placed\": 1 } }\n ]\n )\n\nSummary Statistics\n------------------\n\nThe top of the explained output contains a summary of the execution statistics.\nUse these statistics to determine if a query has high latency or cost. It also\ncontains memory statistics which let you know how close your query is\nto [memory limits](/firestore/mongodb-compatibility/quotas). \n\n Billing Metrics:\n read units: 1\n\n Execution Metrics:\n request peak memory usage: 4.00 KiB (4,096 B)\n results returned: 1\n\nExecution Tree\n--------------\n\nThe execution tree describes the query execution as a series of nodes. The\nbottom nodes (leaf nodes) retrieve data from the storage layer which traverses\nup the tree to generate a query response.\n\nFor details about each execution node,\nrefer to the [Execution reference](/firestore/mongodb-compatibility/docs/query-explain-reference).\n\nFor details on how to use this information to optimize your queries,\nsee [Optimize query execution](/firestore/mongodb-compatibility/docs/optimize-query-performance).\n\nThe following is an example of an execution tree: \n\n • Compute\n | $out_1: map_set($record_1, \"__id__\", $__id___1, \"__key__\", $__key___1, \"__row_id__\", $__row_id___1, \"__$0__\", $__$0___2)\n | is query result: true\n |\n | Execution:\n | records returned: 1\n |\n └── • Compute\n | $__$0___2: UNSET\n |\n | Execution:\n | records returned: 1\n |\n └── • Compute\n | $__key___1: UNSET\n | $__row_id___1: UNSET\n |\n | Execution:\n | records returned: 1\n |\n └── • Compute\n | $__id___1: _id($record_1.__key__)\n |\n | Execution:\n | records returned: 1\n |\n └── • MajorSort\n | fields: [$v_5 ASC]\n | output: [$record_1]\n |\n | Execution:\n | records returned: 1\n | peak memory usage: 4.00 KiB (4,096 B)\n |\n └── • Compute\n | $v_5: array_get($v_4, 0L)\n |\n | Execution:\n | records returned: 1\n |\n └── • Compute\n | $v_4: sortPaths(array($record_1.date_placed), [date_placed ASC])\n |\n | Execution:\n | records returned: 1\n |\n └── • Filter\n | expression: $eq($user_id_1, 1,234)\n |\n | Execution:\n | records returned: 1\n |\n └── • TableScan\n source: **/my_collection\n order: STABLE\n properties: * - { __create_time__, __update_time__ }\n output record: $record_1\n output bindings: {$user_id_1=user_id}\n variables: [$record_1, $user_id_1]\n\n Execution:\n records returned: 1\n records scanned: 1\n\nWhat's next\n-----------\n\n- To learn about the execution tree nodes, see the [Query execution reference](/firestore/mongodb-compatibility/docs/query-explain-reference).\n- To learn how to optimize your queries, see [Optimize query execution](/firestore/mongodb-compatibility/docs/optimize-query-performance)."]]