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)


Parameter Description
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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章