MongoDB的索引(四)

时间:2023-03-10 03:45:41
MongoDB的索引(四)
创建索引的好处是可以加快查询速度,但是但来的负面影响就是磁盘的开销和降低写入性嫩。

查看评判当前索引构建情况方法:

1. 使用mongostat工具: 查看mongodb运行状态的程序

使用格式:mongostat -h 127.0.0.1:12345

2. profile集合使用

db.getProfilingStatus()  查看当前的profile的设置

> db.getProfilingStatus()
{ "was" : 0, "slowms" : 100 }
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 100, "ok" : 1 }

有3个级别可设置,0,1,2

设置为2MongoDB会记录所有操作

> db.system.profile.find().sort({$natural:-1}).limit(1)
{ "op" : "query", "ns" : "test.system.profile", "query" : { "query" : { }, "orderby" : { "$natural" : -1 } }, "ntoreturn" : 10, "ntoskip" : 0, "nscan
ned" : 0, "nscannedObjects" : 0, "keyUpdates" : 0, "writeConflicts" : 0, "numYield" : 0, "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(
2) } }, "MMAPV1Journal" : { "acquireCount" : { "r" : NumberLong(1) } }, "Database" : { "acquireCount" : { "r" : NumberLong(1) } }, "Collection" : { "a
cquireCount" : { "R" : NumberLong(1) } } }, "nreturned" : 0, "responseLength" : 20, "millis" : 0, "execStats" : { "stage" : "COLLSCAN", "filter" : { "
$and" : [ ] }, "nReturned" : 0, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 0, "needTime" : 1, "needFetch" : 0, "saveState" : 0, "res
toreState" : 0, "isEOF" : 1, "invalidates" : 0, "direction" : "backward", "docsExamined" : 0 }, "ts" : ISODate("2015-08-27T15:20:51.434Z"), "client" :
"127.0.0.1", "allUsers" : [ ], "user" : "" }

3. 日志介绍

日志会记录MongoDB运行状态,连接等等信息。

4. explain分析

使用该参数可以显示某次查询的详细结果

db.location.find({x:1}).explain()

> db.bochao.find({x:10}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "bochao.bochao",
"indexFilterSet" : false,
"parsedQuery" : {
"x" : {
"$eq" : 10
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"x" : {
"$eq" : 10
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "dcz1001-PC",
"port" : 27017,
"version" : "3.0.5",
"gitVersion" : "8bc4ae20708dbb493cb09338d9e7be6698e4a3a3"
},
"ok" : 1