现已推出具有 MongoDB 兼容性的 Firestore 企业版!
了解详情。
使用查詢說明功能分析查詢執行作業
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
僅適用於 Cloud Firestore Enterprise 版。
|
本頁說明如何執行查詢時擷取查詢執行資訊。
使用查詢說明
您可以使用「查詢說明」瞭解查詢的執行方式。
這項資訊可協助您最佳化查詢。
您可以透過 Google Cloud 控制台或 explain
指令使用查詢說明。
控制台
在查詢編輯器中執行查詢,然後開啟「說明」分頁:
-
前往 Google Cloud 控制台的「資料庫」頁面。
前往「資料庫」
-
從資料庫清單中,選取與 MongoDB 相容的 Cloud Firestore 資料庫。
Google Cloud 控制台會開啟該資料庫的「Firestore Explorer」(Firestore 探索工具)。
-
在查詢編輯器中輸入查詢,然後點選「Run」(執行)。
-
按一下「說明」分頁標籤,即可查看查詢分析輸出內容。
MongoDB API
MongoDB API 支援 Query Explain,您可以使用 explain
指令,在 Mongo Shell 和 Compass 等工具中執行這項功能。
explain
指令支援 aggregate
、find
、distinct
和 count
指令,例如:
db.collection.explain.find(...)
您也可以使用 explain()
方法,例如:
db.collection.find({QUERY}).explain()
限制
請注意下列限制和差異:
-
查詢說明不支援傳回游標的指令。舉例來說,系統不支援直接呼叫下列指令來叫用說明:
db.collection.aggregate(..., explain: true)
查詢說明僅支援 find
、aggregate
、count
和 distinct
指令。
-
MongoDB API 不支援 Query Explain 的 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
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間: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)."]]