MongoDB的索引(四)

創建索引的好處是可以加快查詢速度,但是但來的負面影響就是磁盤的開銷和降低寫入性嫩。

查看評判當前索引構建情況方法:


1. 使用mongostat工具: 查看mongodb運行狀態的程序

使用格式:mongostat -h 127.0.0.1:12345


2. profile集合使用

db.getProfilingStatus()  查看當前的profile的設置

> db.getProfilingStatus()
{ "was" : 0, "slowms" : 100 }
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 100, "ok" : 1 }

有3個級別可設置,0,1,2

設置爲2MongoDB會記錄所有操作

> db.system.profile.find().sort({$natural:-1}).limit(1)
{ "op" : "query", "ns" : "test.system.profile", "query" : { "query" : {  }, "orderby" : { "$natural" : -1 } }, "ntoreturn" : 10, "ntoskip" : 0, "nscan
ned" : 0, "nscannedObjects" : 0, "keyUpdates" : 0, "writeConflicts" : 0, "numYield" : 0, "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(
2) } }, "MMAPV1Journal" : { "acquireCount" : { "r" : NumberLong(1) } }, "Database" : { "acquireCount" : { "r" : NumberLong(1) } }, "Collection" : { "a
cquireCount" : { "R" : NumberLong(1) } } }, "nreturned" : 0, "responseLength" : 20, "millis" : 0, "execStats" : { "stage" : "COLLSCAN", "filter" : { "
$and" : [ ] }, "nReturned" : 0, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 0, "needTime" : 1, "needFetch" : 0, "saveState" : 0, "res
toreState" : 0, "isEOF" : 1, "invalidates" : 0, "direction" : "backward", "docsExamined" : 0 }, "ts" : ISODate("2015-08-27T15:20:51.434Z"), "client" :
 "127.0.0.1", "allUsers" : [ ], "user" : "" }


3. 日誌介紹

日誌會記錄MongoDB運行狀態,連接等等信息。


4. explain分析

使用該參數可以顯示某次查詢的詳細結果

db.location.find({x:1}).explain()

> db.bochao.find({x:10}).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "bochao.bochao",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "x" : {
                                "$eq" : 10
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "x" : {
                                        "$eq" : 10
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "dcz1001-PC",
                "port" : 27017,
                "version" : "3.0.5",
                "gitVersion" : "8bc4ae20708dbb493cb09338d9e7be6698e4a3a3"
        },
        "ok" : 1





發佈了53 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章