现已推出具有 MongoDB 兼容性的 Firestore 企业版!
了解详情。
使用查询解释分析查询执行
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
仅与 Cloud Firestore 企业版相关。
|
本页面介绍了如何在执行查询时检索查询执行信息。
使用查询解释
您可以使用查询解释功能来了解查询的执行方式。这会提供可用于优化查询的详细信息。
您可以通过 Google Cloud 控制台或 explain
命令来使用查询解释。
控制台
在查询编辑器中执行查询,然后打开说明标签页:
-
在 Google Cloud 控制台中,转到数据库页面。
前往“数据库”
-
从数据库列表中,选择一个与 MongoDB 兼容的 Cloud Firestore 数据库。Google Cloud 控制台会为该数据库打开 Firestore Explorer。
-
在查询编辑器中输入查询,然后点击运行。
-
点击说明标签页以查看查询分析输出。
MongoDB API
通过 explain
命令为 MongoDB API 中的查询解释提供支持,您可以在 Mongo Shell 和 Compass 等工具中使用该命令。
aggregate
、find
、distinct
和 count
命令支持 explain
命令,例如:
db.collection.explain.find(...)
您还可以使用 explain()
方法,例如:
db.collection.find({QUERY}).explain()
限制
请注意以下限制和差异:
-
查询解释不支持返回游标的命令。例如,不支持通过直接调用以下命令来调用解释:
db.collection.aggregate(..., explain: true)
只有 find
、aggregate
、count
和 distinct
命令支持查询解释。
-
通过 MongoDB API 不支持查询解释的 Verbosity
和 Comment
选项。此行为与 executionStats
选项匹配。如果提供了 allPlansExecution
和 queryPlanner
选项,则系统会忽略这些选项。
分析
查询解释的输出包含两个主要组成部分:摘要统计信息和执行树。请考虑以下查询示例:
db.order.aggregate(
[
{ "$match": { "user_id": 1234 } },
{ "$sort": { "date_placed": 1 } }
]
)
摘要统计信息
解释性输出的顶部包含执行统计信息的摘要。使用这些统计信息可确定查询是否具有高延迟或高费用。它还包含内存统计信息,可让您了解查询与内存限制的接近程度。
Billing Metrics:
read units: 1
Execution Metrics:
request peak memory usage: 4.00 KiB (4,096 B)
results returned: 1
执行树
执行树将查询执行描述为一系列节点。底部节点(叶节点)会从存储层检索数据,然后向上遍历树以生成查询响应。
如需详细了解每个执行节点,请参阅执行参考文档。
如需详细了解如何使用这些信息来优化查询,请参阅优化查询执行。
以下是执行树示例:
• Compute
| $out_1: map_set($record_1, "__id__", $__id___1, "__key__", $__key___1, "__row_id__", $__row_id___1, "__$0__", $__$0___2)
| is query result: true
|
| Execution:
| records returned: 1
|
└── • Compute
| $__$0___2: UNSET
|
| Execution:
| records returned: 1
|
└── • Compute
| $__key___1: UNSET
| $__row_id___1: UNSET
|
| Execution:
| records returned: 1
|
└── • Compute
| $__id___1: _id($record_1.__key__)
|
| Execution:
| records returned: 1
|
└── • MajorSort
| fields: [$v_5 ASC]
| output: [$record_1]
|
| Execution:
| records returned: 1
| peak memory usage: 4.00 KiB (4,096 B)
|
└── • Compute
| $v_5: array_get($v_4, 0L)
|
| Execution:
| records returned: 1
|
└── • Compute
| $v_4: sortPaths(array($record_1.date_placed), [date_placed ASC])
|
| Execution:
| records returned: 1
|
└── • Filter
| expression: $eq($user_id_1, 1,234)
|
| Execution:
| records returned: 1
|
└── • TableScan
source: **/my_collection
order: STABLE
properties: * - { __create_time__, __update_time__ }
output record: $record_1
output bindings: {$user_id_1=user_id}
variables: [$record_1, $user_id_1]
Execution:
records returned: 1
records scanned: 1
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["\u003cbr /\u003e\n\n\n|--------------------------------------------------------|\n| *Relevant to Cloud Firestore Enterprise edition only.* |\n\n\u003cbr /\u003e\n\nThis page describes how to retrieve query execution information when you execute a query.\n\nUse Query Explain\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](/docs/firestore/enterprise/optimize-query-performance).\n\nYou can use Query Explain through the Google Cloud console or the `explain` command. \n\nConsole\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 Cloud 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\nMongoDB 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\nLimitations Note 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\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\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](/docs/firestore/enterprise/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\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](/docs/firestore/enterprise/query-explain-reference).\n\nFor details on how to use this information to optimize your queries,\nsee [Optimize query execution](/docs/firestore/enterprise/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- To learn about the execution tree nodes, see the [Query execution reference](/docs/firestore/enterprise/query-explain-reference).\n- To learn how to optimize your queries, see [Optimize query execution](/docs/firestore/enterprise/optimize-query-performance)."]]