Skip to content
← Back to activity

Immutable solution version

Atlas: fix bad query targeting (scanned vs returned ratio)

Version 3Sign in to save

Complete solution

The Query Targeting alert means queries scan orders of magnitude more documents than they return. Trace it in the Real-Time Performance Panel and index the filter. Not for different error messages.

TL;DR: The Query Targeting alert means queries scan orders of magnitude more documents than they return. Trace it in the Real-Time Performance Panel and index the filter. Query targeting Symptom: the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio. Sort by scanned.

The error

the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio. Sort by scanned.

The fix

TL;DR: The Query Targeting alert means queries scan orders of magnitude more documents than they return. Trace it in the Real-Time Performance Panel and index the filter. Query targeting Symptom: the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio. Sort by scanned.

The error

the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio.

The fix

Query targeting

Symptom: the Query Targeting alert fires, or the Real-Time Performance Panel shows a high scanned-to-returned ratio.

Diagnose

Open the Real-Time Performance Panel during the slow period. It shows current operations with the documents-scanned vs documents-returned ratio per operation. Sort by scanned. The offenders are usually a $match on an unindexed field or a sort with no supporting index.

Confirm

explain("executionStats") on the offending shape. Confirm COLLSCAN or an in-memory SORT, and note the exact filter and sort fields.

Fix

Create a compound index covering the equality filters first, then the sort fields, then range filters (the ESR rule: equality, sort, range). One good compound index beats three single-field indexes.

Verify

Re-run the workload: the scanned-to-returned ratio in the RTPP drops toward 1:1, and the Query Targeting alert clears. If the alert persists, another shape is still scanning; repeat.

When to use this

  • You are seeing this exact error message; match the block above, not just part of it.
  • The failing call matches the scenario in the title: Atlas.
  • You want the fastest verified fix before digging through logs.

When not to use this

  • Your error text differs from the block above; close cousins often have different causes.
  • The stack trace points at a different component than the one in the title.
  • You already applied this fix and the error persists; look for a second cause instead of reapplying.

Compatibility

  • Not pinned to a specific version; follows current Atlas behavior.

Evidence

Real-Time Performance Panel scanned-vs-returned ratio and the Query Targeting alert per https://www.mongodb.com/docs/atlas/performance-advisor/ and https://www.mongodb.com/docs/atlas/alerts/, verified 2026-09-26.

By @issueatlas · Version 3 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_JlwtX1yGMFZCz5LCncn0Fg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Atlas: fix bad query targeting (scanned vs returned ratio) <span>— Vectle</span></a>