Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[Mongo] MongoDB index management-creation, viewing and deletion of indexes

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

http://itbilu.com/database/mongo/E1tWQz4_e.html

索引是提高查询查询效率最有效的手段。索引是一种特殊的数据结构,索引以易于遍历的形式存储了数据的部分内容(如:一个特定的字段或一组字段值),索引会按一定规则对存储值进行排序,而且索引的存储位置在内存中,所在从索引中检索数据会非常快。如果没有索引,MongoDB必须扫描集合中的每一个文档,这种扫描的效率非常低,尤其是在数据量较大时。

创建/重建索引查看索引删除索引

1. 创建/重建索引

MongoDB全新创建索引使用ensureIndex()方法,对于已存在的索引可以使用reIndex()进行重建。

1.1 创建索引ensureIndex()

MongoDB创建索引使用ensureIndex()方法。

语法结构

db.COLLECTION_NAME.ensureIndex(keys[,options])我们可以为内嵌文档创建索引,其规则和普通文档没有任何差别,如: db.test.ensureIndex({"comments.date":1}) keys,要建立索引的参数列表。如:{KEY:1},其中key表示字段名,1表示升序排序,也可使用使用数字-1降序。options,可选参数,表示建立索引的设置。可选值如下:background,Boolean,在后台建立索引,以便建立索引时不阻止其他数据库活动。默认值 false。unique,Boolean,创建唯一索引。默认值 false。name,String,指定索引的名称。如果未指定,MongoDB会生成一个索引字段的名称和排序顺序串联。dropDups,Boolean,创建唯一索引时,如果出现重复删除后续出现的相同索引,只保留第一个。sparse,Boolean,对文档中不存在的字段数据不启用索引。默认值是 false。v,index version,索引的版本号。weights,document,索引权重值,数值在 1 到 99,999 之间,表示该索引相对于其他索引字段的得分权重。

如,为集合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}

MongoDB 创建索引导致锁库的解决方案

背景描述

300G 的数据创建索引,执行 db.collection.ensureIndex({key:1}) 之后,打开另一个终端,任何操作都不能执行。

根本原因

在数据库建立索引时,默认时 "foreground" 也就是前台建立索引,但是,当你的数据库数据量很大时,在建立索引的时会读取数据文件,大量的文件读写会阻止其他的操作,命令没有显性指定 background,所以命令会锁库。

解决方案

执行 db.collection.ensureIndex({key:1},{background: true}),这样就不会锁库了,建立索引就会在后台处理了。(注:"{key:1}" 中,1 表示升序 - asc,-1 表示降序 - desc )

在后台建立索引的时候,不能对建立索引的 collection 进行一些坏灭型的操作,如:运行 repairDatabase,drop,compat,当你在建立索引的时候运行这些操作的会报错。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report