http:///database/mongo/E1tWQz4_e.html 索引是提高查询查询效率最有效的手段。索引是一种特殊的数据结构,索引以易于遍历的形式存储了数据的部分内容(如:一个特定的字段或一组字段值),索引会按一定规则对存储值进行排序,而且索引的存储位置在内存中,所在从索引中检索数据会非常快。如果没有索引,MongoDB必须扫描集合中的每一个文档,这种扫描的效率非常低,尤其是在数据量较大时。 创建/重建索引 查看索引 删除索引
1. 创建/重建索引MongoDB全新创建索引使用ensureIndex() 方法,对于已存在的索引可以使用reIndex() 进行重建。 1.1 创建索引ensureIndex() MongoDB创建索引使用ensureIndex() 方法。 语法结构 db.COLLECTION_NAME.ensureIndex(keys[,options]) 如,为集合sites 建立索引: > db.sites.ensureIndex({name: 1, domain: -1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
} 注意:1.8 版本之前创建索引使用createIndex() ,1.8 版本之后已移除该方法 1.2 重建索引reIndex() db.COLLECTION_NAME.reIndex() 如,重建集合sites 的所有索引: > db.sites.reIndex()
{
"nIndexesWas" : 2,
"nIndexes" : 2,
"indexes" : [
{
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "newDB.sites"
},
{
"key" : {
"name" : 1,
"domain" : -1
},
"name" : "name_1_domain_-1",
"ns" : "newDB.sites"
}
],
"ok" : 1
} 2. 查看索引MongoDB提供了查看索引信息的方法:getIndexes() 方法可以用来查看集合的所有索引,totalIndexSize() 查看集合索引的总大小,db.system.indexes.find() 查看数据库中所有索引信息。 2.1 查看集合中的索引getIndexes() db.COLLECTION_NAME.getIndexes() 如,查看集合sites 中的索引: >db.sites.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "newDB.sites"
},
{
"v" : 1,
"key" : {
"name" : 1,
"domain" : -1
},
"name" : "name_1_domain_-1",
"ns" : "newDB.sites"
}
] 2.2 查看集合中的索引大小totalIndexSize() db.COLLECTION_NAME.totalIndexSize() 如,查看集合sites 索引大小: > db.sites.totalIndexSize()
16352 2.3 查看数据库中所有索引db.system.indexes.find() db.system.indexes.find() 如,当前数据库的所有索引: > db.system.indexes.find() 3. 删除索引不在需要的索引,我们可以将其删除。删除索引时,可以删除集合中的某一索引,可以删除全部索引。 3.1 删除指定的索引dropIndex() db.COLLECTION_NAME.dropIndex("INDEX-NAME") 如,删除集合sites 中名为"name_1_domain_-1"的索引: > db.sites.dropIndex("name_1_domain_-1")
{ "nIndexesWas" : 2, "ok" : 1 } 3.3 删除所有索引dropIndexes() db.COLLECTION_NAME.dropIndexes() 如,删除集合sites 中所有的索引: > db.sites.dropIndexes()
{
"nIndexesWas" : 1,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
|