Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menganalisis eksekusi kueri dengan Query Explain
Halaman ini menjelaskan cara mengambil informasi eksekusi kueri saat Anda menjalankan kueri.
Menggunakan Query Explain
Anda dapat menggunakan Query Explain untuk memahami cara kueri Anda dijalankan.
Hal ini memberikan detail yang dapat Anda gunakan untuk mengoptimalkan kueri.
Anda dapat menggunakan Query Explain melalui konsol Google Cloud atau perintah explain.
Bagian atas output yang dijelaskan berisi ringkasan statistik eksekusi.
Gunakan statistik ini untuk menentukan apakah kueri memiliki latensi atau biaya yang tinggi. Selain itu, tab ini juga berisi statistik memori yang memberi tahu Anda seberapa dekat kueri Anda dengan batas memori.
Hierarki eksekusi menjelaskan eksekusi kueri sebagai serangkaian node. Node
bawah (node daun) mengambil data dari lapisan penyimpanan yang melintasi
pohon untuk menghasilkan respons kueri.
Untuk mengetahui detail tentang setiap node eksekusi,
lihat Referensi eksekusi.
Untuk mengetahui detail cara menggunakan informasi ini guna mengoptimalkan kueri Anda, lihat Mengoptimalkan eksekusi kueri.
Berikut adalah contoh pohon eksekusi:
• Drop
| fields to drop: [__$3__]
| records returned: 0
|
└── • MajorSort
| fields: [__$3__ ASC]
| peak memory usage: 4.00 KiB (4,096 B)
| records returned: 0
|
└── • Extend
| expressions: [date_placed AS __$3__]
| records returned: 0
|
└── • Drop
| fields to drop: [__key__]
| records returned: 0
|
└── • Extend
| expressions: [_id(__key__) AS __id__]
| records returned: 0
|
└── • Filter
| expression: $eq(user, 1,234)
| records returned: 0
|
└── • TableScan
order: UNDEFINED
properties: * - { __create_time__, __update_time__ }
records returned: 0
records scanned: 0
source: (default)#/**/collection
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-18 UTC."],[],[],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)."]]