Analyze query execution with Query Explain

This page describes how to retrieve query execution information when you execute a query.

Use Query Explain

You can use Query Explain to understand how your queries are being executed. This provides details that you can use to optimize your queriegoogledata/devsite/site-cloud/en/firestore/mongodb-compatibility/quotas.mds. Support for Query Explain is currently limited to the Google Cloud console.

To use Query Explain, execute a query in the Query Editor and open the Explanation tab:

Console
  1. In the Google Cloud console, go to the Databases page.

    Go to Databases

  2. From the list of databases, select a Firestore with MongoDB compatibility database. The Google Cloud console opens the Firestore Explorer for that database.
  3. Enter a query in the query editor and click Run.
  4. Click the Explanation tab to view the query analysis output.

    Query Explain tab in the console

Analysis

The output of Query Explain contains two main components-the Summary Statistics and Execution Tree. Consider this query as an example:

db.order.aggregate(
 [
   { "$match": { "user_id": 1234 } },
   { "$sort": { "date_placed": 1 } }
 ]
)

Summary Statistics

The top of the explained output contains a summary of the execution statistics. Use these statistics to determine if a query has high latency or cost. It also contains memory statistics which let you know how close your query is to memory limits.

Billing Metrics:
read units: 1

Execution Metrics:
request peak memory usage: 4.00 KiB (4,096 B)
results returned: 1

Execution Tree

The execution tree describes the query execution as a series of nodes. The bottom nodes (leaf nodes) retrieve data from the storage layer which traverses up the tree to generate a query response.

For details about each execution node, refer to the Execution reference.

For details on how to use this information to optimize your queries, see Optimize query execution.

The following is an example of an execution tree:

• 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

What's next