Time Taken to Execute a query

569 views
Skip to first unread message

sdwivedi

unread,
Nov 16, 2016, 5:22:14 AM11/16/16
to mongodb-user
Hello
I want to check time taken in Milliseconds to execute a query. I am using the command db.collectionname.find().limit(2000).explain(). But each time I run this command it gives different values for Millis for example first time it gives "millis" : 29, second time if I run the same query  "millis" : 0 , third time  "millis" : 1 and so on. So My question is how to identify how much time it took to execute a mongoDB query and that time should be constant whenever I am executing.
Any help is Welcome. Thanks in advance.
Regards

Kevin Adistambha

unread,
Dec 1, 2016, 9:16:29 PM12/1/16
to mongodb-user

Hi

how to identify how much time it took to execute a mongoDB query and that time should be constant whenever I am executing.

What MongoDB version are you using? In the latest version (currently 3.4.0), you need to pass explain('executionStats') to the collection object, e.g. db.collection.explain('executionStats').find().limit(2000). The total time taken for the query is shown in the executionStats.executionTimeMillis value.

Typically, the different value for execution time is due to the requested data being loaded to RAM from disk for the first time the query is run. Subsequent execution of the same query should give lower timing, since the data is already loaded into RAM, and RAM access is always faster than disk access.

However, execution time can vary due to the interplay of many factors, including but not limited to:

In general, if your query is efficiently indexed, the RAM is “warmed up”, and the Production Notes observed, queries should take a relatively consistent time from one execution to the next.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages