Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB warn about query not using index

I have a vague memory that MongoDB will output to its log a message if a query uses fields that are not indexed, however I am unable to reproduce this locally (with an admittedly small dataset) nor have I found any documentation for it.

Am I imagining things or is there such a feature? If yes, can anyone link to documentation?

like image 653
Rich Avatar asked Dec 16 '22 04:12

Rich


1 Answers

You need to turn on profiling to get a meaningful log from mongodb - setting profiling to 1 is sufficient.

You can then find unindexed queries using any number of searches in the log, e.g.:

grep nscanned /path/to/mongodb.log | grep -v "nscanned:1 " | grep -v "nscanned:0 "

Original tweet where I found out about this: https://twitter.com/#!/eonwhite/status/21498320559

You can see more about what the log output means in mongo's docs

If nscanned is much higher than nreturned, the database is scanning many objects to find the target objects. Consider creating an index to improve this.

like image 79
AD7six Avatar answered Jan 02 '23 20:01

AD7six