1.通过查找Stack Overflow找到相关问题的解答
2.跟索引相关
3.使用db.currenyOp();命令得到正在执行的命令(增删改查)以及通过ns来查看是否正在使用索引’ns’:’collections.system.indexes’;
4.同时,查看MongoDB log日志 MongoDB日志器会打印索引查询时间超过100ms的操作,发现当前数据库打印的数据查询超过400m
5.2018-06-28T16:51:08.275+0800 I COMMAND [conn30] command bws.block command: find { find: “block”, filter: { blockhash: “378bc0ec06bc22534f2967db5b2b1855dc9776596a7e6b46ad76805aad71d6e3” }, sort: { blockheight: -1 }, limit: 1 } planSummary: COLLSCAN keysExamined:0 docsExamined:376638 hasSortStage:1 cursorExhausted:1 numYields:2944 nreturned:1 reslen:4594 locks:{ Global: { acquireCount: { r: 5890 } }, Database: { acquireCount: { r: 2945 } }, Collection: { acquireCount: { r: 2945 } } } protocol:op_query 396ms
6.从日志得出查询时间较长的语句,单独执行db.collections.find({条件}).sort({value:-1}).limit(1).explain(‘executionStatus’);得出查询详细信息。其中totalDocsExamined指的是MongoDB扫描的文档数量。
{
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “bws.block”,
“indexFilterSet” : false,
“parsedQuery” : {
“blockhash” : {
“$eq” : “378bc0ec06bc22534f2967db5b2b1855dc9776596a7e6b46ad76805aad71d6e3”
}
},
“winningPlan” : {
“stage” : “SORT”,
“sortPattern” : {
“blockheight” : -1
},
“limitAmount” : 1,
“inputStage” : {
“stage” : “SORT_KEY_GENERATOR”,
“inputStage” : {
“stage” : “COLLSCAN”,
“filter” : {
“blockhash” : {
“$eq” : “378bc0ec06bc22534f2967db5b2b1855dc9776596a7e6b46ad76805aad71d6e3”
}
},
“direction” : “forward”
}
}
},
“rejectedPlans” : [ ]
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 1,
“executionTimeMillis” : 205,
“totalKeysExamined” : 0,
“totalDocsExamined” : 403964,
“executionStages” : {
“stage” : “SORT”,
“nReturned” : 1,
“executionTimeMillisEstimate” : 171,
“works” : 403969,
“advanced” : 1,
“needTime” : 403967,
“needYield” : 0,
“saveState” : 3156,
“restoreState” : 3156,
“isEOF” : 1,
“invalidates” : 0,
“sortPattern” : {
“blockheight” : -1
},
“memUsage” : 4502,
“memLimit” : 33554432,
“limitAmount” : 1,
“inputStage” : {
“stage” : “SORT_KEY_GENERATOR”,
“nReturned” : 1,
“executionTimeMillisEstimate” : 161,
“works” : 403967,
“advanced” : 1,
“needTime” : 403965,
“needYield” : 0,
“saveState” : 3156,
“restoreState” : 3156,
“isEOF” : 1,
“invalidates” : 0,
“inputStage” : {
“stage” : “COLLSCAN”,
“filter” : {
“blockhash” : {
“$eq” : “378bc0ec06bc22534f2967db5b2b1855dc9776596a7e6b46ad76805aad71d6e3”
}
},
“nReturned” : 1,
“executionTimeMillisEstimate” : 161,
“works” : 403966,
“advanced” : 1,
“needTime” : 403964,
“needYield” : 0,
“saveState” : 3156,
“restoreState” : 3156,
“isEOF” : 1,
“invalidates” : 0,
“direction” : “forward”,
“docsExamined” : 403964
}
}
}
},
“serverInfo” : {
“host” : “MwalletDB”,
“port” : 27017,
“version” : “3.4.2”,
“gitVersion” : “3f76e40c105fc223b3e5aac3e20dcd026b83b38b”
},
“ok” : 1
}
7.由于是生产环境无法停止数据库的访问,所以准备后台构建索引。命令为:db.collections.createIndex({value:1},{background:true})