# 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.