mongodb数据库模糊查询:一文带你洞悉MongoDB数据库
mongodb数据库模糊查询:一文带你洞悉MongoDB数据库这里为了方便测试,将慢查询的级别设置为2> db.setProfilingLevel(1 { slowms: 500 }) { "was" : 0 "slowms" : 100 "sampleRate" : 1 "ok" : 1 } > db.getProfilingStatus() { "was" : 1 "slowms" : 500 "sampleRate" : 1 }这里设置的Profiling级别为1,慢查询阈值为500毫秒。开启Mongo数据库的慢查询功能在这里需要注意,Mongo数据库的慢查询数据是存放在一个数据库集合中(system.profile),这个和Mysql数据库是有区别的,如果你不主动创建system.profile这个集合,那这
简介使用Mysql数据库的朋友,肯定知道,可以开启慢查询日志,就能捕获Mysql数据库中的慢SQL语句了,那么在Mongo数据库怎么捕获慢查询呢,下面就一步步带大家了解Mongo数据库的慢查询功能的开启和使用的。

Mongo数据库的慢查询功能(Profiling)有三个级别
0:代表关闭,不收集任何慢查询
1:收集慢查询数据,默认收集超过100毫秒的慢查询
2:收集任何操作记录数据
默认情况下,Mongo数据库的慢查询功能是关闭的,如果想要知道Mongo数据库是否已经开启慢查询功能,可以通过下面命令
> db.getProfilingLevel()
0
    
可以看到返回结果是0,Mongo数据库没有开启慢查询功能。
开启Mongo数据库的慢查询功能
在这里需要注意,Mongo数据库的慢查询数据是存放在一个数据库集合中(system.profile),这个和Mysql数据库是有区别的,如果你不主动创建system.profile这个集合,那这个集合就固定1M大小,当慢查询记录超过1M,就会将历史数据覆盖,循环使用,所以在这里需要根据业务实际情况设置集合大小。
在这里需要大家注意的是,system.profile在哪个数据库下创建,就只会收集这个数据库下的慢查询,在这里,我在test数据库下创建system.profile集合,操作过程如下所示
> use test
switched to db admin
> db.createCollection( "system.profile"  { capped: true  size: 1024*1024*10 } )
{ "ok" : 1 }
> show tables;
customers
system.profile
user
    
开启慢查询功能
> db.setProfilingLevel(1  { slowms: 500 })
{ "was" : 0  "slowms" : 100  "sampleRate" : 1  "ok" : 1 }
> db.getProfilingStatus()
{ "was" : 1  "slowms" : 500  "sampleRate" : 1 }
    
这里设置的Profiling级别为1,慢查询阈值为500毫秒。
慢查询功能测试这里为了方便测试,将慢查询的级别设置为2
> db.setProfilingLevel(2)
{ "was" : 1  "slowms" : 500  "sampleRate" : 1  "ok" : 1 }
> db.getProfilingStatus()
{ "was" : 2  "slowms" : 500 "sampleRate" : 1 }
    
模拟集合的查询
> db.user.find()
{ "_id" : ObjectId("5fbf52c182be1e071c0db8ef")  "username" : "jim"  "age" : 18  "status" : 0 }
{ "_id" : ObjectId("5fbf530082be1e071c0db8f0")  "username" : "tony"  "age" : 28  "status" : 0 }
{ "_id" : ObjectId("5fbf530082be1e071c0db8f1")  "username" : "trace" }
{ "_id" : ObjectId("5fbf53b382be1e071c0db8f2")  "username" : "tony1" }
{ "_id" : ObjectId("5fbf53b382be1e071c0db8f3")  "username" : "trace1" }
> db.system.profile.find()
{ "op" : "query"  "ns" : "test.user"  "command" : { "find" : "user"  "filter" : {  }  "lsid" : { "id" : UUID("7424ffd1-52d9-48b1-b658-9adfd3189a89") }  "$db" : "test" }  "keysExamined" : 0  "docsExamined" : 5  "cursorExhausted" : true  "numYield" : 0  "nreturned" : 5  "locks" : { "ReplicationStateTransition" : { "acquireCount" : { "w" : NumberLong(1) } }  "Global" : { "acquireCount" : { "r" : NumberLong(1) } }  "Database" : { "acquireCount" : { "r" : NumberLong(1) } }  "Collection" : { "acquireCount" : { "r" : NumberLong(1) } }  "Mutex" : { "acquireCount" : { "r" : NumberLong(1) } } }  "flowControl" : {  }  "responseLength" : 379  "protocol" : "op_msg"  "millis" : 0  "planSummary" : "COLLSCAN"  "execStats" : { "stage" : "COLLSCAN"  "nReturned" : 5  "executionTimeMillisEstimate" : 0  "works" : 7  "advanced" : 5  "needTime" : 1  "needYield" : 0  "saveState" : 0  "restoreState" : 0  "isEOF" : 1  "direction" : "forward"  "docsExamined" : 5 }  "ts" : ISODate("2020-12-03T09:49:45.211Z")  "client" : "127.0.0.1"  "appName" : "MongoDB Shell"  "allUsers" : [ ]  "user" : "" }
    
从上面结果可以看到,已经捕获到慢查询了。
关注1.如果您喜欢这篇文章,请点赞 转发。
2.如果您特别喜欢,请加关注。




