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:
mongod, or there are multiple resource-intensive process running on the same hardware.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