MongoDB 3.0的Explain

//插入1000000条记录
for (val=0; val < 1000000; val++) {
    db.destination.save({distance:val});
}

//显示记录数
db.destination.count();
//1000000

//查找
db.destinations.find({distance:555500});
{ "_id" : ObjectId("537e3ccdf252b602fb92a9dc"), "distance" : 555500 }

没有建立index的花费

db.destinations.find({distance:555500}).explain();
{
“cursor” : “BasicCursor”,
“isMultiKey” : false,
“n” : 99899,
“nscannedObjects” : 1000000,
“nscanned” : 1000000,
“nscannedObjectsAllPlans” : 1000000,
“nscannedAllPlans” : 1000000,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 781,
“nChunkSkips” : 0,
“millis” : 40,
“server” : “gaurav-Aspire-E1-572:27017”,
“filterSet” : false }

// 建立distance的索引
> db.destination.ensureIndex({distance:1});   

确认关于distance的索引已经存在
db.destination.getIndexes();
[
{
“v” : 1,
“key” : {
“_id” : 1
},
“name” : “id“,
“ns” : “tutorial.dd”
},
{
“v” : 1,
“key” : {
“distance” : 1
},
“name” : “distance_1”,
“ns” : “tutorial.destination”
} ]

建立索引后的花费

db.destinations.find({distance:555500}).explain();
{
“cursor” : “BtreeCursor distance_1”,
“isMultiKey” : false,
“n” : 1,
“nscannedObjects” : 1,
“nscanned” : 1,
“nscannedObjectsAllPlans” : 1,
“nscannedAllPlans” : 1,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 0,
“nChunkSkips” : 0,
“millis” : 0,
“indexBounds” : {
“distance” : [
[
555500,
555500
]
]
},
“server” : “gaurav-Aspire-E1-572:27017”,
“filterSet” : false }

字段说明:
cursor:返回游标类型,有BasicCursor和BtreeCursor,后者意味着使用了索引。
nscanned:扫描document的行数。
n:返回的文档行数。
millis:耗时(毫秒)。
indexBounds:所用的索引

以上代码原文出处:
http://www.jellyfishtechnologies.com/how-to-create-mongodb-indexes/

MongoDB 3.0以上对 explain有些变动
如果还是执行db.destinations.find({x:4444}).explain();
结果仅仅如下

{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "test.destinations",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "x" : {
                                "$eq" : 4444
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "x" : {
                                        "$eq" : 4444 }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "XiaHj",
                "port" : 12345,
                "version" : "3.0.2",
                "gitVersion" : "6201872043ecbbc0a4cc169b5482dcf385fc464f"
        },
        "ok" : 1
}
修改后的explain()需要填写参数。”queryPlanner”, “executionStats”, “allPlansExecution”.
若不填写时,执行db.destinations.find({x:4444}).explain(); 等同于db.destinations.find({x:4444}).explain(“queryPlanner”)
想要继续查看查找花费的时间,则执行
db.destinations.find({x:4444}).explain("executionStats")  

官方语法说明

cursor.explain(verbosity)


ParameterDescription
verbose

Optional. Specifies the verbosity mode for the explain output. The mode affects the behavior of explain() and determines the amount of information to return. The possible modes are: “queryPlanner”, “executionStats”, and “allPlansExecution”.

Default mode is “queryPlanner”.

For backwards compatibility with earlier versions of cursor.explain(), MongoDB interprets true as “allPlansExecution” and false as “queryPlanner”.

For more information on the modes, see Verbosity Modes.

Changed in version 3.0.

官方文档
http://docs.mongodb.org/manual/reference/method/cursor.explain/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值