安裝最新版的mongodb.
安裝免費的圖形客戶端robo 3t
在資料庫local下建立乙個名為test的collection
建立索引db.test.createindex( , )
準備一些資料
db.test.find().explain();
執行結果
關鍵引數說明
},"winningplan" : ,
"indexname" : "ab",
"ismultikey" : false,
"multikeypaths" : ,
"isunique" : false,
"issparse" : false,
"ispartial" : false,
"indexversion" : 2,
"direction" : "forward",
"indexbounds" : }},
"rejectedplans" :
},"serverinfo" : {},
"ok" : 1.0
}
字段說明
queryplanner.indexfilterset
是否啟用了index filter
, mongodb使用index filter
來選擇索引
queryplanner.winningplan
優化器最終選擇的plan
queryplanner.winningplan.inputstage
查詢輸入的引數和索引
queryplanner.winningplan.inputstage.stage
所處的階段
collscan
: 掃瞄collection
ixscan
掃瞄索引
fetch
讀取文件
queryplanner.winningplan.inputstage.keypattern
索引的格式, 與建立索引時的格式一致
queryplanner.winningplan.inputstage.indexbounds
查詢條件的約束值.
queryplanner.rejectedplans
被拒絕掉的候選plan
db.test.find().explain("executionstats");
關鍵字段說明,
"executionstats" : ,
"indexname" : "ab",
"ismultikey" : false,
"multikeypaths" : ,
"isunique" : false,
"issparse" : false,
"ispartial" : false,
"indexversion" : 2,
"direction" : "forward",
"indexbounds" : ,
"key***amined" : 4,
"seeks" : 1,
"dupstested" : 0,
"dupsdropped" : 0,
"seeninvalidated" : 0}}
},"serverinfo" : {},
"ok" : 1.0
}
字段說明
executionstats.totalkey***amined
遍歷索引的次數
executionstats.totaldoc***amined
遍歷文件的次數
executiontimemillisestimate
**需要執行的時間
遍歷的次數越多, 遍歷肯定越慢. 本例中使用了索引, 有4條滿足條件的文件, 所有遍歷次數都是4.
db.test.find().explain("executionstats");
db.test.find()無法使用索引,
}
, 所以沒有遍歷索引, 然後遍歷了整個collection
db.test.find(, ).explain("executionstats");
本例要mongodb只返回
},}
a
,b
兩次字段, 而這兩個字段剛好建立了索引, 不需要再讀取document, 所以沒有遍歷文件.
mongodb中的索引與mysql的索引的行為非常相似
mongodb查詢語句
1.基本查詢 構造查詢資料。db.test.findone 多條件查詢。下面的示例等同於sql語句的where name stephen and age 35 db.test.find 返回指定的文件鍵值對。下面的示例將只是返回name和age鍵值對。db.test.find 指定不返回的文件鍵值對...
mongodb基本查詢語句
1.基本查詢 構造查詢資料。db.test.findone 多條件查詢。下面的示例等同於sql語句的where name stephen and age 35 db.test.find 返回指定的文件鍵值對。下面的示例將只是返回name和age鍵值對。db.test.find 指定不返回的文件鍵值對...
MongoDB查詢效能分析
explain 方法能夠提供大量與查詢相關的資訊。對於速度比較慢的查詢來說,它是最重要的效能分析工具之一。通過檢視乙個查詢的explain 輸出資訊,可以知道查詢使用了哪個索引,以及是如何使用的。對於任意查詢,都可以在最後新增乙個explain 呼叫 與呼叫sort 或者limit 一樣,不過exp...