A poorly optimized query is a query that is either not using an index, or using an index that is not specific enough. You may have been alerted to the existence of poorly optimized queries by the Query Targeting alert.
Finding Queries in Real Time
If the poorly optimized queries are executing consistently, or if you know they are being executed right now, then the Real Time metrics dashboard is the place to go. To find this, start from the Clusters dashboard and click the
Metrics button. Then click on the
Real Time tab.
Next, look at the
Slow Operations table on the bottom right. This table shows all queries that take more than 100ms to execute, and it shows them as they are executing. Poorly optimized queries typically show up here - when you see a suspiciously slow query, click on the row in the table to view the specifics of the operation.
Finding Slow Queries from the MongoDB Logs
By default, MongoDB also logs all queries that take more than 100 ms to execute. The mongod logs can be found via the following steps:
Navigate to the relevant host.
Fixing Your Slow Queries
Once you've downloaded the logs or found your slow query in the Real Time metrics dashboard, the first thing to do is to look for the string
COLLSCAN . This string indicates the MongoDB is performing a "collection scan" to satisfy the query. This means that there is no suitable index to satisfy the query, so MongoDB is instead looking through every single document in the collection. If your logs contain collection scans, you should typically create an index to satisfy the query.
If you've resolved all collection scans, you may need to look deeper. For further information, please refer to the following sections of our documentation: