mongodb: how can I see the execution time for the aggregate command?

前端 未结 6 1170
梦如初夏
梦如初夏 2020-12-09 02:45

I execute the follow mongodb command in mongo shell

db.coll.aggregate(...)

and i see the list of result. but is it possible to see the quer

相关标签:
6条回答
  • 2020-12-09 02:56
    var before = new Date()
    #aggregation query
    var after = new Date()
    execution_mills = after - before
    
    0 讨论(0)
  • 2020-12-09 02:58

    Try .explain("executionStats"):

    db.inventory.find(
       { quantity: { $gte: 100, $lte: 200 } }
    ).explain("executionStats")
    

    Which returns a very detailed JSON with the time in "executionTimeMillis" like:

    {
       "queryPlanner" : {
             ...
       },
       "executionStats" : {
          "executionSuccess" : true,
          "nReturned" : 3,
          "executionTimeMillis" : 0,
          ...
    

    More details: https://docs.mongodb.com/manual/tutorial/analyze-query-plan/#evaluate-the-performance-of-a-query

    0 讨论(0)
  • 2020-12-09 03:09

    Or you can install the excellent mongo-hacker, which automatically times every query, pretty()fies it, colorizes the output, sorts the keys, and more:

    enter image description here

    0 讨论(0)
  • 2020-12-09 03:13

    I will write an answer to explain this better.

    Basically there is no explain() functionality for the aggregation framework yet: https://jira.mongodb.org/browse/SERVER-4504

    However there is a way to measure client side but not without its downsides:

    • You are not measuring the database
    • You are measuring the application
    • There are too many unknowns about the in between parts to be able to get an accurate reading, i.e. you can't say that it took 0.04ms for the document result to be formulated by the MongoDB server, serialised, sent over the wire, de-serialised by the app and then stored into a hash allowing you subtract that sum from the total to get a aggregation benchmark.

    However that being said, you might be able to get a slightly accurate result by doing it in MongoDB console on the same server as the mongos / mongod. This will create very little in betweens, still too many but enough to maybe get a reading you could roughly trust. As such you could use @Zagorulkin's answer in that position.

    0 讨论(0)
  • 2020-12-09 03:15

    You can add a time function to your .mongorc.js file (in your home directory):

    function time(command) {
        const t1 = new Date();
        const result = command();
        const t2 = new Date();
        print("time: " + (t2 - t1) + "ms");
        return result; 
    }
    

    and then you can use it like so:

    time(() => db.coll.aggregate(...))
    

    Caution

    This method doesn't give relevant results for db.collection.find()

    0 讨论(0)
  • 2020-12-09 03:17

    i see that in mongodb there is a possibility to use this two command:

    • db.setProfilingLevel(2)

    • and so after the query you can use db.system.profile.find() to see the query execution time and other

    0 讨论(0)
提交回复
热议问题