1、explain执行计划
例如
>db.order.ensureIndex({"user.uid":1})
>db.order.find({ "status": 1.0, "user.uid": { $gt: 2663199.0 } }).explain()
{
"cursor" : "BtreeCursor user.uid_1",
"nscanned" : 337800,
"nscannedObjects" : 337800,
"n" : 337800,
"millis" : 1371,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"user.uid" : [
[
2663199,
1.7976931348623157e+308
]
]
}
}
2、优化器profile
mongodb把要输出的慢语句,存在于db.system.profile。与mysql的slowlog配置相似,需要进行参数设置,mongo才会输出慢语句到profile。有两个参数来控制profile的输出
db.setProfilingLevel(level,slowms);
默认为0 不输出 1按第二个参数时间阀值(单位为毫秒)输出 2全部输出。通常我们在调优的时候都在测试环境打开参数。在生产环境下一般不输出profile。
比如
> db.system.profile.find({millis:{$gt:1000}})
就可以输出,查询时间大于1秒的慢语句。
profile输出的各项值的含义是
ts:命令执行时间
info:命令的内容
query:代表查询
order.order: 代表查询的库与集合
reslen:返回的结果集大小,byte数
nscanned:扫描记录数量
nquery:后面是查询条件
nreturned:返回记录数及用时
millis:所花时间
文章转载自: mongodb索引的优化 http://www.studyofnet.com/news/456.html