分析慢查询
1、db.setProfilingLevel(级别)
0 – 不开启
1 – 记录慢命令 (默认为>100ms)
2 – 记录所有命令
2、
{
"op" : "command",
"ns" : "wjdipw.car",
"command" : {
"aggregate" : "car",
"pipeline" : [
{
"$lookup" : {
"from" : "inetuser",
"localField" : "applicant",
"foreignField" : "_id",
"as" : "inetuser"
}
},
{
"$addFields" : {
"inetuser" : {
"$arrayElemAt" : [
"$inetuser",
0
]
}
}
},
{
"$count" : "count"
}
],
"cursor" : {
"batchSize" : 32
},
"allowDiskUse" : false,
"$db" : "wjdipw",
"$clusterTime" : {
"clusterTime" : Timestamp(1619064272, 1),
"signature" : {
"hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" },
"keyId" : NumberLong(0)
}
},
"lsid" : {
"id" : UUID("a48fcac9-5438-4748-9265-57e80250a3fe")
},
"$readPreference" : {
"mode" : "secondaryPreferred"
}
},
"keysExamined" : 0,
"docsExamined" : 10,
"cursorExhausted" : true,
"numYield" : 0,
"nreturned" : 1,
"queryHash" : "A300CFDE",
"planCacheKey" : "A2B33459",
"locks" : {
"ReplicationStateTransition" : {
"acquireCount" : {
"w" : NumberLong(23)
}
},
"Global" : {
"acquireCount" : {
"r" : NumberLong(23)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(23)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(24)
}
},
"Mutex" : {
"acquireCount" : {
"r" : NumberLong(23)
}
}
},
"flowControl" : {},
"responseLength" : 243,
"protocol" : "op_msg",
"millis" : 2,
"planSummary" : "COLLSCAN",
"ts" : ISODate("2021-04-22T04:04:32.985Z"),
"client" : "192.168.8.31",
"allUsers" : [],
"user" : ""
}
3、解读
system.profile.op
这一项主要包含如下几类
insert(插入)
query(查询)
update(更新)
remove(删除)
getmore (从游标中取数据)
command(比以上插入、查找、更新、删除的综合还多,还统计了别的命令)
代表了该慢日志的种类是什么,是查询、插入、更新、删除还是其他。
system.profile.ns
该项表明该慢日志是哪个库下的哪个集合所对应的慢日志。
system.profile.command
该项详细输出了慢日志的具体语句和行为
system.profile.keysExamined
该项表明为了找出最终结果MongoDB搜索了多少个key
system.profile.docsExamined
该项表明为了找出最终结果MongoDB搜索了多少个文档
system.profile.keyUpdates
该项表名有多少个index key在该操作中被更改,更改索引键也会有少量的性能消耗,因为数据库不单单要删除旧Key,还要插入新的Key到B-Tree索引中
system.profile.writeConflicts
写冲突发生的数量,例如update一个正在被别的update操作的文档
system.profile.numYield
为了让别的操作完成而屈服的次数,一般发生在需要访问的数据尚未被完全读取到内存中,MongoDB会优先完成在内存中的操作
system.profile.nreturned
该操作最终返回文档的数量
system.profile.responseLength
结果返回的大小,单位为bytes,该值如果过大,则需考虑limit()等方式减少输出结果
system.profile.millis
该操作从开始到结束耗时多少,单位为毫秒
system.profile.execStats
包含了一些该操作的统计信息,只有query类型的才会显示
system.profile.execStats.stage
包含了该操作的详细信息,例如是否用到索引
system.profile.ts
该操作执行时的时间
system.profile.client
哪个客户端发起的该操作,并显示出该客户端的ip或hostname
system.profile.allUsers
哪个认证用户执行的该操作
system.profile.user
是否认证用户执行该操作,如认证后使用其他用户操作,该项为空