mongodb 索引使用
作用
- 索引通常能夠極大的提高查詢辙芍。
- 索引是一種數(shù)據(jù)結(jié)構(gòu),他搜集一個集合中文檔特定字段的值撮珠。
- B-Tree索引來實(shí)現(xiàn)琉兜。
創(chuàng)建索引
db.collection.createIndex(keys, options)
keys
keys由文檔字段和索引類型組成。如{"name":1}
key 表示字段 value 1,-1 1表示升序院仿,-1降序
options
options 創(chuàng)建索引的選項(xiàng)秸抚。
參數(shù) | 類型 | 描述 |
---|---|---|
background | boolean | 創(chuàng)建索引在后臺運(yùn)行速和,不會阻止其他對數(shù)據(jù)庫操作 |
unique | boolean | 創(chuàng)建唯一索引,文檔的值不會重復(fù) |
name | string | 索引名稱剥汤,默認(rèn)是:字段名_排序類型 開始排序 |
sparse | boolean | 過濾掉null颠放,不存在的字段 |
查看索引
db.collection.getIndexes()
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "leyue.userdatas"
},
{
"v" : 1,
"key" : {
"name" : 1 //索引字段
},
"name" : "name_1", //索引名稱
"ns" : "leyue.userdatas"
}
刪除索引
db.collection.dropIndex(index) 刪除指定的索引。
db.collection.dropIndexes() 刪除除了_id 以外的所有索引吭敢。
- index 是字符串 表示按照索引名稱 name 刪除字段碰凶。
- index 是{字段名稱:1} 表示按照key 刪除索引。
創(chuàng)建/查看/刪除 示例
查看數(shù)據(jù)
db.userdatas.find()
{ "_id" : ObjectId("597f357a09c84cf58880e412"), "name" : "u3", "age" : 32 }
{ "_id" : ObjectId("597f357a09c84cf58880e411"), "name" : "u4", "age" : 30, "score" : [ 7, 4, 2, 0 ] }
{ "_id" : ObjectId("597fcc0f411f2b2fd30d0b3f"), "age" : 20, "score" : [ 7, 4, 2, 0, 10, 9, 8, 7 ], "name" : "lihao" }
{ "_id" : ObjectId("597f357a09c84cf58880e413"), "name" : "u2", "age" : 33, "wendang" : { "yw" : 80, "xw" : 90 } }
{ "_id" : ObjectId("5983f5c88eec53fbcd56a7ca"), "date" : ISODate("2017-08-04T04:19:20.693Z") }
{ "_id" : ObjectId("597f357a09c84cf58880e40e"), "name" : "u1", "age" : 26, "address" : "中國碭山" }
{ "_id" : ObjectId("597f357a09c84cf58880e40f"), "name" : "u1", "age" : 37, "score" : [ 10, 203, 12, 43, 56, 22 ] }
{ "_id" : ObjectId("597f357a09c84cf58880e410"), "name" : "u5", "age" : 78, "address" : "china beijing chaoyang" }
給字段name 創(chuàng)建索引
// 創(chuàng)建索引
db.userdatas.createIndex({"name":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
// 查看索引
db.userdatas.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "leyue.userdatas"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "leyue.userdatas"
}
]
給字段name 創(chuàng)建索引并命名為myindex
db.userdatas.createIndex({"name":1})
db.userdatas.createIndex({"name":1},{"name":"myindex"})
db.userdatas.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "leyue.userdatas"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "myindex",
"ns" : "leyue.userdatas"
}
]
給字段name 創(chuàng)建索引 創(chuàng)建的過程在后臺執(zhí)行
當(dāng)mongodb 集合里面的數(shù)據(jù)過大時 創(chuàng)建索引很耗時省有,可以在放在后臺運(yùn)行痒留。
db.userdatas.dropIndex("myindex")
db.userdatas.createIndex({"name":1},{"name":"myindex","background":true})
給age 字段創(chuàng)建唯一索引
db.userdatas.createIndex({"age":-1},{"name":"ageIndex","unique":true,"sparse":true})
db.userdatas.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "leyue.userdatas"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "myindex",
"ns" : "leyue.userdatas",
"background" : true
},
{
"v" : 1,
"unique" : true,
"key" : {
"age" : -1
},
"name" : "ageIndex",
"ns" : "leyue.userdatas",
"sparse" : true
}
]
// 插入一個已存在的age
db.userdatas.insert({ "name" : "u8", "age" : 32})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: leyue.userdatas.$ageIndex dup key: { : 32.0 }"
}
})
創(chuàng)建復(fù)合索引
db.userdatas.createIndex({"name":1,"age":-1})
db.userdatas.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "leyue.userdatas"
},
{
"v" : 1,
"key" : {
"name" : 1,
"age" : -1
},
"name" : "name_1_age_-1",
"ns" : "leyue.userdatas"
}
]
所有的字段都存在集合 system.indexes 中
db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "leyue.userdatas" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "leyue.scores" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "leyue.test" }
{ "v" : 1, "key" : { "user" : 1, "name" : 1 }, "name" : "myindex", "ns" : "leyue.test" }
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "leyue.mycapped" }
{ "v" : 1, "key" : { "user" : 1 }, "name" : "user_1", "ns" : "leyue.test" }
{ "v" : 1, "key" : { "name" : 1 }, "name" : "myindex", "ns" : "leyue.userdatas" }
索引總結(jié)
1:創(chuàng)建索引時,1表示按升序存儲,-1表示按降序存儲。
2:可以創(chuàng)建復(fù)合索引,如果想用到復(fù)合索引,必須在查詢條件中包含復(fù)合索引中的前N個索引列
3: 如果查詢條件中的鍵值順序和復(fù)合索引中的創(chuàng)建順序不一致的話,
MongoDB可以智能的幫助我們調(diào)整該順序,以便使復(fù)合索引可以為查詢所用蠢沿。4: 可以為內(nèi)嵌文檔創(chuàng)建索引,其規(guī)則和普通文檔創(chuàng)建索引是一樣的伸头。
5: 一次查詢中只能使用一個索引,$or特殊,可以在每個分支條件上使用一個索引。
6: $where,$exists不能使用索引,還有一些低效率的操作符,比如:$ne,$not,$nin等舷蟀。
7: 設(shè)計(jì)多個字段的索引時,應(yīng)該盡量將用于精確匹配的字段放在索引的前面恤磷。
explain 使用
語法
db.collection.explain().<method(...)>
explain() 可以設(shè)置參數(shù) :
queryPlanner。
executionStats野宜。
allPlansExecution扫步。
示例
for(var i=0;i<100000;i++) {
db.test.insert({"user":"user"+i});
}
沒有使用索引
db.test.explain("executionStats").find({"user":"user200000"})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "leyue.test",
"indexFilterSet" : false,
"parsedQuery" : {
"user" : {
"$eq" : "user200000"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"user" : {
"$eq" : "user200000"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2,
"executionTimeMillis" : 326,
"totalKeysExamined" : 0,
"totalDocsExamined" : 1006497,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"user" : {
"$eq" : "user200000"
}
},
"nReturned" : 2,
"executionTimeMillisEstimate" : 270,
"works" : 1006499,
"advanced" : 2,
"needTime" : 1006496,
"needYield" : 0,
"saveState" : 7863,
"restoreState" : 7863,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 1006497
}
},
"serverInfo" : {
"host" : "lihaodeMacBook-Pro.local",
"port" : 27017,
"version" : "3.2.1",
"gitVersion" : "a14d55980c2cdc565d4704a7e3ad37e4e535c1b2"
},
"ok" : 1
}
executionStats.executionTimeMillis: query的整體查詢時間。
executionStats.nReturned: 查詢返回的條目匈子。
executionStats.totalKeysExamined: 索引掃描條目河胎。
executionStats.totalDocsExamined: 文檔掃描條目。
executionTimeMillis = 326 query 執(zhí)行時間
nReturned=2 返回兩條數(shù)據(jù)
totalKeysExamined=0 沒有用到索引
totalDocsExamined 全文檔掃描
理想狀態(tài):
nReturned=totalKeysExamined & totalDocsExamined=0
Stage狀態(tài)分析
stage | 描述 |
---|---|
COLLSCAN | 全表掃描 |
IXSCAN | 掃描索引 |
FETCH | 根據(jù)索引去檢索指定document |
SHARD_MERGE | 將各個分片返回?cái)?shù)據(jù)進(jìn)行merge |
SORT | 表明在內(nèi)存中進(jìn)行了排序 |
LIMIT | 使用limit限制返回?cái)?shù) |
SKIP | 使用skip進(jìn)行跳過 |
IDHACK | 針對_id進(jìn)行查詢 |
SHARDING_FILTER | 通過mongos對分片數(shù)據(jù)進(jìn)行查詢 |
COUNT | 利用db.coll.explain().count()之類進(jìn)行count運(yùn)算 |
COUNTSCAN | count不使用Index進(jìn)行count時的stage返回 |
COUNT_SCAN | count使用了Index進(jìn)行count時的stage返回 |
SUBPLA | 未使用到索引的$or查詢的stage返回 |
TEXT | 使用全文索引進(jìn)行查詢時候的stage返回 |
PROJECTION | 限定返回字段時候stage的返回 |
-
對于普通查詢虎敦,我希望看到stage的組合(查詢的時候盡可能用上索引):
Fetch+IDHACK
Fetch+ixscan
Limit+(Fetch+ixscan)
PROJECTION+ixscan
SHARDING_FITER+ixscan
COUNT_SCAN
不希望看到包含如下的stage:
COLLSCAN(全表掃描),SORT(使用sort但是無index),不合理的SKIP,SUBPLA(未用到index的$or),COUNTSCAN(不使用index進(jìn)行count)
使用索引
db.test.createIndex({"user":1},{"name":"myindex","background":true})
db.test.explain("executionStats").find({"user":"user200000"})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "leyue.test",
"indexFilterSet" : false,
"parsedQuery" : {
"user" : {
"$eq" : "user200000"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"user" : 1
},
"indexName" : "myindex",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"user" : [
"[\"user200000\", \"user200000\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2,
"executionTimeMillis" : 0,
"totalKeysExamined" : 2,
"totalDocsExamined" : 2,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 2,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"user" : 1
},
"indexName" : "myindex",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"user" : [
"[\"user200000\", \"user200000\"]"
]
},
"keysExamined" : 2,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "lihaodeMacBook-Pro.local",
"port" : 27017,
"version" : "3.2.1",
"gitVersion" : "a14d55980c2cdc565d4704a7e3ad37e4e535c1b2"
},
"ok" : 1
}
- executionTimeMillis: 0
- totalKeysExamined: 2
- totalDocsExamined:2
- nReturned:2
- stage:IXSCAN
- 使用索引和不使用差距很大游岳,合理使用索引,一個集合適合做 4-5 個索引其徙。
相關(guān)文章
http://www.mongoing.com/eshu_explain3
https://docs.mongodb.com/v3.2/reference/explain-results/#queryplanner