先在数据库上增加一些数据,输入下面的命令:
for(var i=1;i<10;i++) db.customers.insert({name:"jordan"+i,country:"American"})
for(var i=1;i<10;i++) db.customers.insert({name:"gaga"+i,country:"American"})
for(var i=1;i<10;i++) db.customers.insert({name:"ham"+i,country:"UK"})
for(var i=1;i<10;i++) db.customers.insert({name:"brown"+i,country:"UK"})
for(var i=1;i<10;i++) db.customers.insert({name:"ramda"+i,country:"Malaysia"})
使用下面的命令查看当前数据库并不存在索引(_id除外)
db.system.indexes.find()
现在在name字段增加一列索引,索引语法:
db.collection.ensureIndex(keys,options)
keys是一个document,包含要增加索引的字段和索引的排序方向;option是可选参数,控制索引的创建排序方式。具体命令如下:
db.customers.ensureIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
利用indexes.find()可以查询到刚刚建立好的索引。如果建立唯一索引可以使用如下命令:
db.customers.ensureIndex({name:1},{unique:true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
(此文使用唯一索引)
查看数据是否使用索引:
db.customers.find({name:"ramda9"}).explain()
{
"cursor" : "BtreeCursor name_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"name" : [
[
"ramda9",
"ramda9"
]
]
},
"server" : "localhost.localdomain:27017",
"filterSet" : false
}
从粗体字的"nscannedObjects"看出查询过程中扫描的总文档数是使用了索引。现在再次查询country字段:
db.customers.find().count()
45
db.customers.find({country:"Malaysia"}).explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 9,
"nscannedObjects" : 45,
"nscanned" : 45,
"nscannedObjectsAllPlans" : 45,
"nscannedAllPlans" : 45,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"server" : "localhost.localdomain:27017",
"filterSet" : false
}
可以看出这是使用了全表扫描,并没有使用到索引。
建立复合索引:
现在为country建立一个索引:
for(var i=1;i<10;i++) db.customers.insert({name:"lanbo"+i,country:"Malaysia"})
查询索引情况:
db.customers.find({country:"Malaysia"}).explain()
结果是全表扫描。现在尝试在country上建立一个普通索引:
db.customers.ensureIndex({country:1})
重新再次执行explain语句:
db.customers.find({country:"Malaysia"}).explain()
{
"cursor" : "BtreeCursor country_1",
"isMultiKey" : false,
"n" : 18,
"nscannedObjects" : 18,
"nscanned" : 18,
"nscannedObjectsAllPlans" : 18,
"nscannedAllPlans" : 18,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"country" : [
[
"Malaysia",
"Malaysia"
]
]
},
"server" : "localhost.localdomain:27017",
"filterSet" : false
}
使用了索引并且查询到18条记录。现在创建一个复合索引:
db.customers.ensureIndex({name:1,coutry:1})
db.customers.find({name:"lanbo2",country:"Malaysia"}).explain()
{
"cursor" : "BtreeCursor name_1_coutry_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 3,
"nscannedAllPlans" : 3,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"name" : [
[
"lanbo2",
"lanbo2"
]
],
"coutry" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
},
"server" : "localhost.localdomain:27017",
"filterSet" : false
}
此处使用了name与country的复合索引。(要删除了name的唯一索引才可以,db.customers.dropIndex("name_1"))
转载于:https://blog.51cto.com/raytech/1692774