liveBook · Manning

Query targeting score

As explained in MongoDB 8.0 in Action, Third Edition

Query targeting is a metric of query efficiency based on how many objects a query scans relative to the objects it returns.

Query targeting score measures query efficiency by comparing documents read with documents returned. A score of 1 means every document read matched and appeared in the results; higher scores indicate more wasted scanning. section 21.2.1

Use the score to prioritize query-performance work:

  • Treat high scores as signals to inspect index use and query structure, because unnecessary reads can increase CPU, I/O, and response time. section 21.3.1
  • Use suggested-index results to find slow-query patterns; each recommendation includes an average targeting score for queries using that index. section 21.2.1
  • A score of 1 represents a highly efficient pattern, but implementing an index remains an optimization opportunity rather than a guarantee. section 21.2.1

Alert on excessive scanning

Configure an Atlas alert for QUERY_TARGETING_SCANNED_OBJECTS_PER_RETURNED with GREATER_THAN and a raw threshold of 1000. The alert fires when a query scans more than 1,000 objects for each object returned, enabling proactive investigation. section 21.3.1

atlas alerts settings create --event OUTSIDE_METRIC_THRESHOLD --enabled \
--metricName QUERY_TARGETING_SCANNED_OBJECTS_PER_RETURNED \
 --metricOperator GREATER_THAN --metricThreshold 1000 \
--metricUnits RAW --notificationType EMAIL --notificationEmailEnabled \
--notificationEmailAddress your-email@example.com \
--notificationIntervalMin 15

Create the metric alert with the Atlas CLI, enabling email notification and a 15-minute notification interval. section 21.3.1

atlas performanceAdvisor suggestedIndexes list \
    --processName atlas-rr5vne-shard-00-00.a7niyd4.mongodb.net:27017

Retrieve suggested indexes for a selected process, then compare their average query targeting scores when investigating slow queries. section 21.2.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines the scanned-to-returned ratio, interprets score 1, and connects thresholds to indexes and alerts without explaining query execution internals.
Examples
Uses a 1,000-object alert threshold and Atlas CLI commands for suggested indexes and slow-query investigation.
What it assumes
Assumes familiarity with queries and returned documents while introducing score interpretation, Atlas monitoring, and suggested-index workflows.
What you can do after
Diagnose inefficient queries, inspect index recommendations, and configure Atlas alerts for excessive scanning.
How it connects
Connects query targeting with indexes, slow queries, Performance Advisor, Query Profiler, and Atlas alerts.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.