> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Fix Query Issues

`Query Targeting` alerts often indicate inefficient queries.

## Alert Conditions

You can configure the following alert conditions in the project-level [alert settings](https://www.mongodb.com/docs/atlas/configure-alerts.md#std-label-configure-alerts) page to trigger alerts.

[`Query Targeting: Scanned Objects / Returned`](https://www.mongodb.com/docs/atlas/reference/alert-conditions.md#mongodb-alert-Query-Targeting--Scanned-Objects---Returned) alerts are triggered when the average number of documents scanned relative to the average number of documents returned server-wide across all operations during a sampling period exceeds a defined threshold. The default alert uses a 1000:1 threshold.

Ideally, the ratio of scanned documents to returned documents should be close to 1. A high ratio negatively impacts query performance.

[`Query Targeting: Scanned / Returned`](https://www.mongodb.com/docs/atlas/reference/alert-conditions.md#mongodb-alert-Query-Targeting--Scanned---Returned) occurs if the number of index keys examined to fulfill a query relative to the actual number of returned documents meets or exceeds a user-defined threshold. This alert is not enabled by default.

**Example:**

The following [mongod log](https://www.mongodb.com/docs/atlas/mongodb-logs.md#std-label-mongodb-logs) entry shows statistics generated from an inefficient query:

```json
<Timestamp> COMMAND  <query>
planSummary: COLLSCAN keysExamined:0
docsExamined: 10000 cursorExhausted:1 numYields:234
nreturned:4  protocol:op_query 358ms
```

This query scanned 10,000 documents and returned only 4 for a ratio of 2500, which is highly inefficient. No index keys were examined, so MongoDB scanned all documents in the collection, known as a [collection scan.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-collection-scan)

## Common Triggers

The query targeting alert typically occurs when there is no index to support a query or queries or when an existing index only partially supports a query or queries.

The [change streams](https://www.mongodb.com/docs/manual/changeStreams/) cursors that the MongoDB Search process (`mongot`) uses to keep MongoDB Search indexes updated can contribute to the query targeting ratio and trigger [query targeting alerts](https://www.mongodb.com/docs/atlas/reference/alert-conditions.md#std-label-query-targeting-alerts) if the ratio is high.

## Fix the Immediate Problem

Add one or more indexes to better serve the inefficient queries.

The [Performance Advisor](https://www.mongodb.com/docs/atlas/performance-advisor.md#std-label-pa-slow-queries) provides the easiest and quickest way to create an index. The Performance Advisor monitors queries that MongoDB considers slow and recommends indexes to improve performance. Atlas dynamically adjusts your slow query threshold based on the execution time of operations across your cluster.

Click Create Index on a slow query for instructions on how to create the recommended index.

**Note:**

It is possible to receive a Query Targeting alert for an inefficient query without receiving index suggestions from the Performance Advisor if the query exceeds the slow query threshold and the ratio of scanned to returned documents is greater than the threshold specified in the alert.

In addition, you can use the following resources to determine which query generated the alert:

- The [Real-Time Performance Panel](https://www.mongodb.com/docs/atlas/real-time-performance-panel.md#std-label-real-time-metrics-status-tab) monitors and displays current network traffic and database operations on machines hosting MongoDB in your Atlas clusters.

- The [MongoDB logs](https://www.mongodb.com/docs/atlas/mongodb-logs.md#std-label-mongodb-logs) maintain an account of activity, including queries, for each `mongod` instance in your Atlas clusters.

- The [cursor.explain()](https://www.mongodb.com/docs/manual/reference/method/cursor.explain/) command for [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) provides performance details for all queries.

- [Namespace Insights](https://www.mongodb.com/docs/atlas/namespace-insights.md#std-label-namespace-insights) monitors collection-level query latency.

- The Atlas [Query Profiler](https://www.mongodb.com/docs/atlas/tutorial/query-profiler.md#std-label-query-profiler) records operations that Atlas considers slow when compared to average execution time for all operations on your cluster.

## Implement a Long-Term Solution

Refer to the following for more information on query performance:

- [MongoDB Indexing Strategies](https://www.mongodb.com/docs/manual/applications/indexes/)

- [Query Optimization](https://www.mongodb.com/docs/manual/core/query-optimization/)

- [Analyze Query Plan](https://www.mongodb.com/docs/manual/tutorial/analyze-query-plan/)

## Monitor Your Progress

Atlas provides the following methods to visualize query targeting:

- [Query Shape Reports](https://www.mongodb.com/docs/atlas/query-shape-insights.md#std-label-query-shape-reports), which send a summary of inefficient query shapes to Project Owners by email.

- Query Targeting [metrics](https://www.mongodb.com/docs/atlas/monitor-cluster-metrics.md#std-label-monitor-cluster-metrics), which highlight high ratios of objects scanned to objects returned.

- [Namespace Insights](https://www.mongodb.com/docs/atlas/namespace-insights.md#std-label-namespace-insights), which monitors collection-level query latency.

- The [Query Profiler](https://www.mongodb.com/docs/atlas/tutorial/query-profiler.md#std-label-query-profiler), which describes specific inefficient queries executed on the cluster.

### Query Targeting Metrics

You can view historical metrics to help you visualize the query performance of your cluster. To view Query Targeting metrics in the Atlas UI:

1. In Atlas, go to the Clusters page for your project.

   If it's not already displayed, select the organization that contains your desired project from the  Organizations menu in the navigation bar.

   If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

   In the sidebar, click Clusters under the Database heading.

   The [Clusters](https://cloud.mongodb.com/go?l=https%3A%2F%2Fcloud.mongodb.com%2Fv2%2F%3Cproject%3E%23%2Fclusters) page displays.

2. View Query Targeting metrics.

   Click View Monitoring on the dashboard for the cluster.

   On the Metrics page, click the Add Chart dropdown menu and select Query Targeting.

The Query Targeting chart displays the following metrics for queries executed on the server:

| Metric | Description |
| --- | --- |
| Scanned Objects / Returned | Indicates the average number of documents examined relative to the average number of returned documents. |
| Scanned / Returned | Indicates the number of index keys examined to fulfill a query relative to the actual number of returned documents. |

The [change streams](https://www.mongodb.com/docs/manual/changeStreams/) cursors that the MongoDB Search process (`mongot`) uses to keep MongoDB Search indexes updated can contribute to the query targeting ratio and trigger [query targeting alerts](https://www.mongodb.com/docs/atlas/reference/alert-conditions.md#std-label-query-targeting-alerts) if the ratio is high.

If either of these metrics exceed the [user-defined threshold](https://www.mongodb.com/docs/atlas/configure-alerts.md#std-label-configure-alerts), Atlas generates the corresponding [`Query Targeting: Scanned Objects / Returned`](https://www.mongodb.com/docs/atlas/reference/alert-conditions.md#mongodb-alert-Query-Targeting--Scanned-Objects---Returned) or [`Query Targeting: Scanned / Returned`](https://www.mongodb.com/docs/atlas/reference/alert-conditions.md#mongodb-alert-Query-Targeting--Scanned---Returned) alert.

**Note:**

You can also view Query Targeting ratios of operations in real-time using the [Real-Time Performance Panel.](https://www.mongodb.com/docs/atlas/real-time-performance-panel.md#std-label-real-time-metrics-status-tab)

### Namespace Insights

[Namespace Insights](https://www.mongodb.com/docs/atlas/namespace-insights.md#std-label-namespace-insights) monitors collection-level [query latency](https://www.mongodb.com/docs/manual/reference/operator/aggregation/collStats/#latencystats-document). You can view query latency metrics and statistics for certain hosts and operation types. Manage pinned namespaces and choose up to five namespaces to show in the corresponding query latency charts.

To access Namespace Insights:

1. In Atlas, go to the Clusters page for your project.

   If it's not already displayed, select the organization that contains your desired project from the  Organizations menu in the navigation bar.

   If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

   In the sidebar, click Clusters under the Database heading.

   The [Clusters](https://cloud.mongodb.com/go?l=https%3A%2F%2Fcloud.mongodb.com%2Fv2%2F%3Cproject%3E%23%2Fclusters) page displays.

2. Go to the Namespace Insights tab.

   Click the name of the cluster to open the Cluster sidebar.

   Click Query Insights in the Cluster sidebar.

   Click the Namespace Insights tab.

### Query Profiler

The [Query Profiler](https://www.mongodb.com/docs/atlas/tutorial/query-profiler.md#std-label-query-profiler) contains several metrics you can use to pinpoint specific inefficient queries. You can visualize up to the past 24 hours of query operations. The Query Profiler can show the Examined : Returned Ratio (index keys examined to documents returned) of logged queries, which might help you identify the queries that triggered a [`Query Targeting: Scanned / Returned`](https://www.mongodb.com/docs/atlas/reference/alert-conditions.md#mongodb-alert-Query-Targeting--Scanned---Returned) alert. The chart shows the number of index keys examined to fulfill a query relative to the actual number of returned documents.

**Note:**

The [default](https://www.mongodb.com/docs/atlas/configure-alerts.md#std-label-default-alert-settings) [`Query Targeting: Scanned Objects / Returned`](https://www.mongodb.com/docs/atlas/reference/alert-conditions.md#mongodb-alert-Query-Targeting--Scanned-Objects---Returned) alert ratio differs slightly. The ratio of the average number of documents scanned to the average number of documents returned during a sampling period triggers this alert.

Atlas might not [log](https://www.mongodb.com/docs/atlas/mongodb-logs.md#std-label-mongodb-logs) the individual operations that contribute to the Query Targeting ratios due to automatically set thresholds. However, you can still use the Query Profiler and Query Targeting metrics to analyze and optimize query performance.

To access the Query Profiler:

1. In Atlas, go to the Clusters page for your project.

   If it's not already displayed, select the organization that contains your desired project from the  Organizations menu in the navigation bar.

   If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

   In the sidebar, click Clusters under the Database heading.

   The [Clusters](https://cloud.mongodb.com/go?l=https%3A%2F%2Fcloud.mongodb.com%2Fv2%2F%3Cproject%3E%23%2Fclusters) page displays.

2. Go to the Query Profiler tab.

   Click the name of the cluster to open the Cluster sidebar.

   Click Query Insights in the Cluster sidebar.

   Click the Query Profiler tab.
