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/

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