How to find queries not using indexes or slow in mongodb How to find queries not using indexes or slow in mongodb mongodb mongodb

How to find queries not using indexes or slow in mongodb


The equivalent approach in MongoDB would be to use the query profiler to track and diagnose slow queries.

With profiling enabled for a database, slow operations are written to the system.profile capped collection (which by default is 1Mb in size). You can adjust the threshold for slow operations (by default 100ms) using the slowms parameter.


First, you must set up your profiling, specifying what the log level that you want. The 3 options are:

  • 0 - logger off
  • 1 - log slow queries
  • 2 - log all queries

You do this by running your mongod deamon with the --profile options:

mongod --profile 2 --slowms 20

With this, the logs will be written to the system.profile collection, on which you can perform queries as follows:

  • find all logs in some collection, ordering by ascending timestamp:

db.system.profile.find( { ns:/<db>.<collection>/ } ).sort( { ts: 1 } );

  • looking for logs of queries with more than 5 milliseconds:

db.system.profile.find( {millis : { $gt : 5 } } ).sort( { ts: 1} );


You can use the following two mongod options. The first option fails queries not using index (V 2.4 only), the second records queries slower than some ms threshold (default is 100ms)

--notablescanForbids operations that require a table scan.--slowms <value>Defines the value of “slow,” for the --profile option. The database logs all slow queries to the log, even when the profiler is not turned on. When the database profiler is on, mongod the profiler writes to the system.profile collection. See the profile command for more information on the database profiler.