Mongodb Profiler Output

如下是System.profile中的一条记录

    {
    "op" : "query",
    "ns" : "omnisocials_admin_dev.GcLog",
    "query" : { "$orderby" : { "createTime" : -1 }, "$query" : {} },
    "ntoreturn" : 1,
    "ntoskip" : 0,
    "nscanned" : 0,
    "nscannedObjects" : 164404,
    "scanAndOrder" : true,
    "keyUpdates" : 0,
    "writeConflicts" : 0,
    "numYield" : 1356,
    "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(1357) } }, "MMAPV1Journal" : { "acquireCount" : { "r" : NumberLong(1357) } }, "Database" : { "acquireCount" : { "r" : NumberLong(1357) } }, "Collection" : { "acquireCount" : { "R" : NumberLong(1357) } } },
    "nreturned" : 1,
    "responseLength" : 421,
    "millis" : 2348,
    "execStats" : { "stage" : "OR", "nReturned" : 1, "executionTimeMillisEstimate" : 1860, "works" : 164482, "advanced" : 1, "needTime" : 164407, "needFetch" : 74, "saveState" : 1356, "restoreState" : 1356, "isEOF" : 0, "invalidates" : 0, "dupsTested" : 1, "dupsDropped" : 0, "locsForgotten" : 0, "matchTested_0" : 0, "matchTested_1" : 0, "inputStages" : [ { "stage" : "SORT", "nReturned" : 1, "executionTimeMillisEstimate" : 1850, "works" : 164482, "advanced" : 1, "needTime" : 164406, "needFetch" : 74, "saveState" : 1356, "restoreState" : 1356, "isEOF" : 1, "invalidates" : 0, "sortPattern" : { "createTime" : -1 }, "memUsage" : 409, "memLimit" : 3.35544e+07, "limitAmount" : 1, "inputStage" : { "stage" : "COLLSCAN", "filter" : { "$and" : [] }, "nReturned" : 164404, "executionTimeMillisEstimate" : 1130, "works" : 164480, "advanced" : 164404, "needTime" : 1, "needFetch" : 74, "saveState" : 1356, "restoreState" : 1356, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 164404 } }, { "stage" : "SORT", "nReturned" : 0, "executionTimeMillisEstimate" : 0, "works" : 0, "advanced" : 0, "needTime" : 0, "needFetch" : 0, "saveState" : 1356, "restoreState" : 1356, "isEOF" : 0, "invalidates" : 0, "sortPattern" : { "createTime" : -1 }, "memUsage" : 0, "memLimit" : 3.35544e+07, "inputStage" : { "stage" : "COLLSCAN", "filter" : { "$and" : [] }, "nReturned" : 0, "executionTimeMillisEstimate" : 0, "works" : 0, "advanced" : 0, "needTime" : 0, "needFetch" : 0, "saveState" : 1356, "restoreState" : 1356, "isEOF" : 0, "invalidates" : 0, "direction" : "forward", "docsExamined" : 0 } } ] },
    "ts" : ISODate("2016-01-18T10:19:55.840Z"),
    "client" : "127.0.0.1",
    "allUsers" : [],
    "user" : "" }

如何生成system profile collection?

MongoDB Database Profiling 是一个捕获数据库执行活动的系统,它可以帮助识别慢查询和操作。

设置 profiling level

  • 级别
    • 级别0, 禁用;
    • 级别1, 启用;只记录慢操作
    • 级别2, 启用;记录所有操作
  • 级别设置
    • db.setProfilingLevel(level, slowms)
      • level:级别
      • slowms:慢操作阈值
    • slowms默认100ms,所以使用db.setProfilingLevel(1)就表示设置记录慢操作,阈值100ms

查询 profiling level & status

>db.getProfilingLevel()
1
>db.getProfilingStatus()
{ "was" : 1, "slowms" : 20 }
>db.system.profile.find().sort({$natural:-1})
{ "op" : "query", "ns" : "omnisocials_admin_dev.GcLog", "query" : { "$orderby" : { "createTime" : -1 }, "$query" : {  } }, "ntoreturn" : 1, "ntoskip" : 0, "nscanned" : 0, "nscannedObjects" : 172989, "scanAndOrder" : true, "keyUpdates" : 0, "writeConflicts" : 0, "numYield" : 1351, "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(1352) } }, "MMAPV1Journal" : { "acquireCount" : { "r" : NumberLong(1352) } }, "Database" : { "acquireCount" : { "r" : NumberLong(1352) } }, "Collection" : { "acquireCount" : { "R" : NumberLong(1352) } } }, "nreturned" : 1, "responseLength" : 421, "millis" : 636, "execStats" : { "stage" : "OR", "nReturned" : 1, "executionTimeMillisEstimate" : 630, "works" : 172993, "advanced" : 1, "needTime" : 172992, "needFetch" : 0, "saveState" : 1351, "restoreState" : 1351, "isEOF" : 0, "invalidates" : 0, "dupsTested" : 1, "dupsDropped" : 0, "locsForgotten" : 0, "matchTested_0" : 0, "matchTested_1" : 0, "inputStages" : [ { "stage" : "SORT", "nReturned" : 1, "executionTimeMillisEstimate" : 630, "works" : 172993, "advanced" : 1, "needTime" : 172991, "needFetch" : 0, "saveState" : 1351, "restoreState" : 1351, "isEOF" : 1, "invalidates" : 0, "sortPattern" : { "createTime" : -1 }, "memUsage" : 409, "memLimit" : 33554432, "limitAmount" : 1, "inputStage" : { "stage" : "COLLSCAN", "filter" : { "$and" : [ ] }, "nReturned" : 172989, "executionTimeMillisEstimate" : 110, "works" : 172991, "advanced" : 172989, "needTime" : 1, "needFetch" : 0, "saveState" : 1351, "restoreState" : 1351, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 172989 } }, { "stage" : "SORT", "nReturned" : 0, "executionTimeMillisEstimate" : 0, "works" : 0, "advanced" : 0, "needTime" : 0, "needFetch" : 0, "saveState" : 1351, "restoreState" : 1351, "isEOF" : 0, "invalidates" : 0, "sortPattern" : { "createTime" : -1 }, "memUsage" : 0, "memLimit" : 33554432, "inputStage" : { "stage" : "COLLSCAN", "filter" : { "$and" : [ ] }, "nReturned" : 0, "executionTimeMillisEstimate" : 0, "works" : 0, "advanced" : 0, "needTime" : 0, "needFetch" : 0, "saveState" : 1351, "restoreState" : 1351, "isEOF" : 0, "invalidates" : 0, "direction" : "forward", "docsExamined" : 0 } } ] }, "ts" : ISODate("2016-01-19T04:56:49.725Z"), "client" : "127.0.0.1", "allUsers" : [ ], "user" : "" }

关于system.profile

Mongodb system.profile 是一个capped collection,capped collections是性能出色且有着固定大小的集合,对于大小固定,我们可以想象其就像一个环形队列,当集合空间用完后,再插入的元素就会覆盖最初始的头部的元素!

一些字段参考

相关字段 版本 含义
op 2.6+ 操作类型,包括【insert,query,update,remove,getmore,command】
ns 2.6+ 集合全路径,例如:omnisocials_admin_dev.GcLog
query 2.6+ 该操作document,如大于50kb则转对象为字符串摘要,如何摘要大于50kb,则进行截取
command 2.6+ command操作文档,如大于50kb则转对象为字符串摘要,如何摘要大于50kb,则进行截取
updateobj 2.6+ 在进行update操作时传递的update文档
cursorid 2.6+ query和getmore操作时的游标ID
ntoreturn 2.6+ 操作指定返回的文档数量,例如:limit(5)
ntoskip 2.6+ skip是多少
nscanned 2.6+ 当前操作执行时扫描的索引数量
nscannedObjects 2.6+ 当前操作执行时扫描的集合数
moved 3.0* 更新操作移动一个或多个文档到分区其他位置时,此值显示true
nmoved 3.0* 分区上移动的文档数
scanAndOrder 2.6+ 是否以index的排序去返回请求结果
ndeleted 2.6+ 当期操作删除的文档数量
ninserted 2.6+ 当期操作插入的文档数量
nMatched 2.6+ 仅适用与update操作
nModified 2.6+ 当期操作修改的文档数量
upsert 2.6+ A boolean that indicates the update operation’s upsert option value. Only appears if upsert is true.
keyUpdates 2.6+ 当前操作的执行引发了多少个index的改变,一个index的改变会引发性能消耗
writeConflicts 3.0+ 如果当前操作是写操作,那么writeConfilicts则表示写操作时遇到的冲突数量
numYield 2.6+ 当前操作在读取数据时,为使其他操作的执行二放弃的次数
lockStats 2.6- Mongodb2.6引入的锁的监控(R)global read lock,(W)global write lock,(r)global write lock,(r)database specific read lock|
locks 3.0+ lock types and lock modes:[(Global)global lock,(MMAPV1Journal)MMAPv1 storage engine specific lock to synchronize journal writes,(Database)database lock.,(Collection)collection lock.,(Metadata)metadata lock.,(oplog)lock on the oplog.][(R)Shared lock,(W)Exclusive lock,(r)Intent Shared lock,(w)Intent Exclusive lock]
acquireCount 3.0+ 当前操作获取锁的次数
acquireWaitCount 3.0+ 当前操作获取锁时需等待的次数
timeAcquiringMicros 3.0+ 当前操作等待锁的总时间
deadlockCount 3.0+ 当前操作等待锁释放时遇到死锁的次数
nreturned 2.6+ 当前操作实际返回的文档数量
responseLength 2.6+ 当前操作返回的文档大小
millis 2.6+ 当前操作最终耗时
execStats 3.0+ 包含执行统计操作的文档
ts 2.6+ 当期操作开始时的时间
client 2.6+ 发起操作的机器IP地址
allUsers 2.6+ 从session获取的已认证的用户组
user 2.6+ 执行当期操作的认证用户

参考链接

  • Mongodb2.6:https://docs.mongodb.org/v2.6/reference/database-profiler/
  • Mongodb3.0:https://docs.mongodb.org/v3.0/reference/database-profiler/
To reader: 所述仅供参考,如有不妥之处,望不吝指正!

你可能感兴趣的:(mongodb,profiler,Mongodb慢查询)