二十四、mongodb的索引
阿新 • • 發佈:2018-12-22
1、為什麼mongdb需要建立索引
- 加快查詢速度
- 進行資料的去重
2、mongodb建立簡單的索引方法
- db.集合.ensureIndex({屬性:1}),1表示升序, -1表示降序
- db.集合.createIndex({屬性:1})
註釋:上面兩個命令效果等價
具體操作:db.db_name.ensureIndex({name:1})
3、建索引前後查詢速度對比
- 測試:插入10萬條資料到資料庫中 插入資料:
for(i=0;i<100000;i++){db.t255.insert({name:'test'+i,age:i})}
- 建立索引前:
db.t1.find({name:‘test10000’})
db.t1.find({name:‘test10000’}).explain(‘executionStats’)
- 建立索引後:
db.t255.ensureIndex({name:1})
db.t1.find({name:‘test10000’}).explain(‘executionStats’)
- 前後速度對比
4、索引的檢視
- 預設情況下_id是集合的索引
db.collection_name.getIndexes()
新增索引前:
> db.test2000.insert({"name":"hello",age:20}) WriteResult({ "nInserted" : 1 }) > db.test2000.find() { "_id" : ObjectId("5ae0232f625b9ddd91a0e7ae"), "name" : "hello", "age" : 20 } > db.test2000.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test2000.test2000" } ]
新增name為索引後:
> db.test2000.ensureIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.test2000.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test2000.test2000"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test2000.test2000"
}
]
5、mongodb建立唯一索引
在預設情況下mongdb的索引域的值是可以相同的,建立唯一索引之後,資料庫會在插入資料的時候檢查建立索引域的值是否存在,如果存在則不會插入該條資料,但是建立索引僅僅能夠提高查詢速度,同時降低資料庫的插入速度。
- 新增唯一索引的語法:
db.collection_name.ensureIndex({"name":1},{"unique":true})
- 使用普通索引的效果如下:
db.test2000.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test2000.test2000"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test2000.test2000"
}
]
db.test2000.insert({name:"hello",age:40})
db.test2000.find()
{ "_id" : ObjectId("5ae0232f625b9ddd91a0e7ae"), "name" : "hello", "age" : 20 }
{ "_id" : ObjectId("5ae02421625b9ddd91a0e7af"), "name" : "hello", "age" : 30 }
{ "_id" : ObjectId("5ae02432625b9ddd91a0e7b0"), "name" : "hello", "age" : 40 }
- 新增age為唯一索引之後:
db.test2000.createIndex({age:1},{unique:true})
顯示結果:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
檢視索引:
db.test2000.getIndexes()
顯示結果:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test2000.test2000"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test2000.test2000"
},
{
"v" : 2,
"unique" : true,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test2000.test2000"
}
]
插入資料:
db.test2000.insert({"name":"world",age:20})
顯示結果
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: test2000.test2000 index: age_1 dup key: { : 20.0 }"
}
})
6、刪除索引
db.t1.dropIndex({'索引名稱':1})
7、建立複合索引
在進行資料去重的時候,可能用一個域來保證資料的唯一性,這個時候可以考慮建立複合索引來實現。
例如:抓全貼吧資訊,如果把帖子的名字作為唯一索引對資料進行去重是不可取的,因為可能有很多帖子名字相同
建立複合索引的語法:
db.collection_name.ensureIndex({欄位1:1,欄位2:1})
檢視索引:
db.test2000.getIndexes()
顯示結果:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test2000.test2000"
}
]
8、建立索引注意點
- 根據需要選擇是否需要建立唯一索引
- 索引欄位是升序還是降序在單個索引的情況下不影響查詢效率,但是帶複合索引的條件下會有影響
資料量巨大並且資料庫的讀出操作非常頻繁的時候才需要建立索引,如果寫入操作非常頻繁,建立索引會影響寫入速度,通過實現==“讀寫分離”==,解決該問題。