準備階段
插入10W條數(shù)據(jù)測試準備:
插入10W條數(shù)據(jù)
MongoDB性能分析函數(shù)(explain)
性能分析函數(shù)
cursor #查詢方式 BasicCursor:順序查找即“表掃描”
n #最終返回文檔數(shù)量
nscanned #數(shù)據(jù)庫瀏覽了10w個文檔
millis #總共耗時50毫秒
查看索引
db.age.getIndexes()
查看索引
命名索引
db.age.ensureIndex({"name":1},{name:"normal_index"})
刪除索引
db.age.dropIndex("name_1")
刪除索引
創(chuàng)建索引
單鍵索引
db.age.ensureIndex({"name":1})
單鍵索引
創(chuàng)建索引性能對比:
創(chuàng)建索引性能對比
唯一索引
重復的鍵值自然就不能插入
db.age.ensureIndex({"name":1},{unique:true})
稀疏索引
沒有此字段的數(shù)據(jù)不建立索引,節(jié)約磁盤
db.age.ensureIndex({"name":1},{sparse:true/false})
組合索引
db.age.ensureIndex({"name":1,"age":1})
db.age.ensureIndex({"age":1,"name":1})
查看組合索引
過期索引
在一段時間后會過期的索引
在索引過期后,相應的數(shù)據(jù)會被刪除
適合存儲在一段時間之后會失效的數(shù)據(jù),比如用戶的登錄信息、存儲的日志等。
db.age.ensureIndex({time:1},{expireAfterSeconds:30}) #索引30秒后失效
db.age.insert({time:new Date()}) #time必須為ISODate或者ISODate數(shù)組,不能為時間戳
全文索引
創(chuàng)建
db.age.ensureIndex({key:"text"}) #key:字段名,value:固定字符串text
db.age.ensureIndex({key1:"text",key2:"text"}) #在多個字段上創(chuàng)建全文索引
db.age.ensureIndex({"$**":"text"}) #給所有字段建立全文索引
查詢
db.age.find({$text:{$search:"coffee"}})
db.age.find({$text:{$search:"aa bb cc"}}) #空格代表或操作,aa或bb或cc
db.age.find({$text:{$search:"aa bb -cc"}}) #-號為非操作,即不包含cc的
db.age.find({$text:{$search: "\"aa\" \"bb\" \"cc\""}}) #""號為與操作,aa與bb與cc
查詢返回相似度
db.age.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}})
db.age.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}}) #返回相似度并排序
全局索引的限制:
- 每次查詢,只能指定一個$text查詢
- 不能出現(xiàn)在$nor查詢中
- 查詢中如果包含了$text, hint(強制指定索引)不再起作用
- MongoDB全文索引還不支持中文
地理位置索引
2D地理位置索引的取值范圍以及表示方法 經(jīng)緯度[經(jīng)度,緯度]
經(jīng)緯度取值范圍:經(jīng)度[-180,180] 緯度[-90,90]
1. 2D索引
創(chuàng)建
db.collection.ensureIndex({w:"2d"})
插入
db.collection.insert({w:[180,90]})
查詢
(1) $near 查詢距離某個點最近的點 ,默認返回最近的100個點
db.collection.find({w:{$near:[x,y]}})
(2) $geoWithin查詢某個形狀內(nèi)的點
#查詢矩形中的點
db.collection.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}})
#查詢圓中的點
db.collection.find({w:{$geoWithin:{$center:[[0,0],5]}}})
#查詢多邊形中的點
db.collection.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1]]}}})
(3)geoNear查詢
#2d索引不支持minDistance
#num:返回數(shù)量
db.runCommand({geoNear:"collection名稱",near:[x, y],minDistance:10,maxDistance:10,num:1...})
goNear查詢
2. 2Dsphere索引
創(chuàng)建
db.collection.ensureIndex({w:"2dsphere"})
插入
#位置格式
{type:"Point/LineString/Polygon",coordinates:[x,y]}
db.sphere.insert({name:"A",sp:{type:"Point",coordinates:[105.754484701156,41.689607057699]}})
db.sphere.insert({name:"B",sp:{type:"Point",coordinates:[105.304045248031,41.783456183240]}})
db.sphere.insert({name:"C",sp:{type:"Point",coordinates:[105.084318685531,41.389027478812]}})
db.sphere.insert({name:"D",sp:{type:"Point",coordinates:[105.831388998031,41.285916385493]}})
db.sphere.insert({name:"E",sp:{type:"Point",coordinates:[106.128706502914,42.086868474465]}})
db.sphere.insert({name:"F",sp:{type:"Point",coordinates:[105.431074666976,42.009365053841]}})
db.sphere.insert({name:"G",sp:{type:"Point",coordinates:[104.705977010726,41.921549795110]}})
查詢
#2dsphere索引支持minDistance
#num:返回數(shù)量
db.runCommand({
geoNear:"collection名稱",
near:{type:"Point/LineString/Polygon",coordinates:[x,y]},
minDistance:10,
maxDistance:10,
num:1,
...})