mongodb 索引日常维护操作

创建索引:

db.t_order_detail.createIndex({"order_id":1})

复合索引:

db.t_order_detail.createIndex({"order_id":1,"detail_id":1,"batch_id":1})

在后台创建索引:

db.t_order_detail.createIndex({order_id:1},{background:1})

查看索引:

db.t_order_detail.getIndexes()

查看索引键:

db.t_order_detail.getIndexKeys()

查看集合索引总大小:

db.t_order_detail.totalIndexSize()

查看集合各索引的详细信息:

db.t_order_detail.getIndexSpecs()

删除索引:

db.t_order_detail.dropIndex("index_name")

删除所有索引

db.t_order_detail.dropIndexes()方法用于删除全部的索引

索引重建:

db.t_order_detail.reIndex({"order_id":1})

备注:
在前台创建索引期间会锁定集合,会导致其它操作无法进行数据读写,在后台创建索引,会定期释放写锁,从而保证其它操作的运行,但是后台操作会在耗时更长,尤其是在频繁进行写入的集合上。

相关文章