All Collections
Atlas Frequently Asked Questions
Understanding the Query Targeting Alert
Understanding the Query Targeting Alert

What is the Query Targeting alert, and what triggers it?

Joao Boesso avatar
Written by Joao Boesso
Updated over a week ago

The Query Targeting alert is based on the "scanned objects / returned" metric.  The formal definition of this metric is "The ratio of the number of documents scanned to the number of documents returned by queries".  That is, when your application executes a query, how many documents does MongoDB need to look through before it finds the ones you need.  This is best illustrated via a simple example.  Suppose I create a brand new collection called students  and then I insert a few documents:

db.students.insert({name: 'Albert'})
db.students.insert({name: 'Bob'})
db.students.insert({name: 'Carl'})

Note that I have not created any indexes on this collection. Now I execute a query:

db.students.find({name: 'Carl'})

The most likely outcome is that MongoDB will need to inspect the Albert document, then the Bob document, and then finally the Carl document, before it can return the Carl document.  This means that the "scanned objects / returned" value for this particular query is 3.

The Query Targeting metric is the average of all the "scanned objects / returned" values for all queries run in a particular window of time.

To make your Atlas cluster run as efficiently as possible, you want the vast majority of your queries to be well targeted - that is, have a value of "scanned objects / returned" that is close to 1. 

To achieve this, you'll need to create an index of the name  field.

db.students.createIndex({name:1})

Now, instead of inspecting every single document when we query for name: 'Carl' , MongoDB can use the index to skip straight to the Carl document.

More info: Viewing Query Targeting metrics over time

You can view the historical performance of the Query Targeting metrics by clicking on the METRICS  button on your Clusters dashboard.  On the Metrics page, look for the Add Chart  button, and then select Query Targeting .  The Query Targeting alert is based on the green line in this chart.  Viewing historical metrics can help you understand if you are making progress on improving this statistic - or might also call out that this statistic is particularly poor at certain times of day.

Did this answer your question?