查看mongodb执行命令耗时

 

1.开启日志,超过10毫秒的都记录
db.setProfilingLevel( 1 , 10)

 

2.执行命令
db.metric.aggregate([{$group: {_id: '$month', totalcnt: {$sum: 1}}}])

 

3.查看执行情况

> db.system.profile.find().pretty()
{
        "op" : "command",
        "ns" : "db_test.metric",
        "command" : {
                "aggregate" : "metric",
                "pipeline" : [
                        {
                                "$group" : {
                                        "_id" : "$month",
                                        "totalcnt" : {
                                                "$sum" : 1
                                        }
                                }
                        }
                ],
                "cursor" : {

                },
                "lsid" : {
                        "id" : UUID("fda589d9-c981-4f93-9341-fca9980f77b3")
                },
                "$readPreference" : {
                        "mode" : "secondaryPreferred"
                },
                "$db" : "db_test"
        },
        "keysExamined" : 0,
        "docsExamined" : 1000000,
        "cursorExhausted" : true,
        "numYield" : 1026,
        "nreturned" : 1,
        "locks" : {
                "ReplicationStateTransition" : {
                        "acquireCount" : {
                                "w" : NumberLong(1058)
                        }
                },
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(1058)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "r" : NumberLong(1057)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "r" : NumberLong(1057)
                        }
                },
                "Mutex" : {
                        "acquireCount" : {
                                "r" : NumberLong(31)
                        }
                }
        },
        "flowControl" : {

        },
        "storage" : {

        },
        "responseLength" : 149,
        "protocol" : "op_msg",
        "millis" : 1732,
        "planSummary" : "COLLSCAN",
        "ts" : ISODate("2022-03-24T03:29:11.433Z"),
        "client" : "192.168.1.135",
        "appName" : "MongoDB Shell",
        "allUsers" : [ ],
        "user" : ""
}

 

这里执行1732毫秒

 

4.关闭日志
db.setProfilingLevel(0)

 

posted @ 2022-03-24 11:34  slnngk  阅读(1132)  评论(0编辑  收藏  举报