文档章节

mongodb 后台创建索引避免阻塞

易野
 易野
发布于 2017/07/27 15:54
字数 704
阅读 4
收藏 0

By default, creating an index blocks all other operations on a database. When building an index on a collection, the
database that holds the collection is unavailable for read or write operations until the index build completes. Any
operation that requires a read or write lock on all databases (e.g. listDatabases) will wait for the foreground index
build to complete.
For potentially long running index building operations, consider the background operation so that the MongoDB
database remains available during the index building operation. For example, to create an index in the background of
the zipcode field of the people collection, issue the following:


db.people.createIndex( { zipcode: 1}, {background: true} )

 


By default, background is false for building MongoDB indexes.
You can combine the background option with other options, as in the following:
 

db.people.createIndex( { zipcode: 1}, {background: true, sparse: true } )

 

Behavior
As of MongoDB version 2.4, a mongod instance can build more than one index in the background concurrently.
Changed in version 2.4: Before 2.4, a mongod instance could only build one background index per database at a time.
Changed in version 2.2: Before 2.2, a single mongod instance could only build one index at a time.
Background indexing operations run in the background so that other database operations can run while creating the
index. However, the mongo shell session or connection where you are creating the index will block until the index
build is complete. To continue issuing commands to the database, open another connection or mongo instance.
Queries will not use partially-built indexes: the index will only be usable once the index build is complete.

Note: If MongoDB is building an index in the background, you cannot perform other administrative
operations involving that collection, including running repairDatabase, dropping the collection (i.e.
db.collection.drop()), and running compact. These operations will return an error during background
index builds.

Performance
The background index operation uses an incremental approach that is slower than the normal “foreground” index
builds. If the index is larger than the available RAM, then the incremental process can take much longer than the
foreground build.
If your application includes createIndex() operations, and an index doesn’t exist for other operational concerns,
building the index can have a severe impact on the performance of the database.
To avoid performance issues, make sure that your application checks for the indexes at start up using the
getIndexes() method or the equivalent method for your driver7 and terminates if the proper indexes do not exist.
Always build indexes in production instances using separate application code, during designated maintenance
windows.
Interrupted Index Builds
If a background index build is in progress when the mongod process terminates, when the instance restarts the index
build will restart as foreground index build. If the index build encounters any errors, such as a duplicate key error, the
mongod will exit with an error.
To start the mongod after a failed index build, use the storage.indexBuildRetry or
--noIndexBuildRetry to skip the index build on start up.

 

Building Indexes on Secondaries
Changed in version 2.6: Secondary members can now build indexes in the background. Previously all index builds on
secondaries were in the foreground.
Background index operations on a replica set secondaries begin after the primary completes building the index. If
MongoDB builds an index in the background on the primary, the secondaries will then build that index in the background.
To build large indexes on secondaries the best approach is to restart one secondary at a time in standalone mode and
build the index. After building the index, restart as a member of the replica set, allow it to catch up with the other
members of the set, and then build the index on the next secondary. When all the secondaries have the new index, step
down the primary, restart it as a standalone, and build the index on the former primary.
The amount of time required to build the index on a secondary must be within the window of the oplog, so that the
secondary can catch up with the primary.
Indexes on secondary members in “recovering” mode are always built in the foreground to allow them to catch up as
soon as possible.

 

 

 

© 著作权归作者所有

共有 人打赏支持
易野
粉丝 4
博文 171
码字总数 126481
作品 0
深圳
私信 提问
浅尝辄止MongoDB:操作(3)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzy0623/article/details/82882703 目录 5. 批处理数据 6. 重命名集合 7. 删除数据 8. 引用数据库 9. 使用与索...

wzy0623
09/28
0
0
十个 MongoDB 使用要点

从 mongodb 阶段性技术总结 中抽取并整理了对大家有帮助的十个要点: 1.mongodb 表名和字段名统一用小写字母 mongodb 是默认区分大小写的,为了避免以前在 mysql 下遇到的大小写敏感导致程序...

旁观者-郑昀
2013/02/08
0
0
浅尝辄止MongoDB:管理(2)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzy0623/article/details/83012634 目录 四、验证与修复 1. 修复服务器 2. 验证单个集合 3. 修复集合验证错误...

wzy0623
10/11
0
0
mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、等等)

最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网:https://www.mongodb.com/ MongoDB 是一个基于分布式文件存储的...

Airship
06/01
0
0
Adhesive问答之Mongodb数据服务

Q:Mongodb数据服务有什么用? A:首先,Mongodb适合保存大量的非业务数据,因此,Adhesive框架提倡把不是非常重要的非业务数据(比如应用程序信息中心的日志、异常、状态数据,又比如WCF扩展...

随缘主人
2011/12/16
0
0

没有更多内容

加载失败,请刷新页面

加载更多

控制台打印图片

function dev(){ if (window.console){ console.log("%c\n ", "font-size:100px;background:url('http://gmcyzs.com/resources/images/logo.png') no-repeat"); console.log('%c 深务平台,\......

羊皮卷
6分钟前
0
0
MyBaties的二级缓存

二级缓存介绍 在上文中提到的一级缓存中,其最大的共享范围就是一个SqlSession内部,那么如何让多个SqlSession之间也可以共享缓存呢,答案是二级缓存。 当开启二级缓存后,会使用CachingExec...

嘴角轻扬30
7分钟前
0
0
10.新增博客功能-结束语---《Beetl视频课程》

本期视频实现发布新博客功能 一起学beetl目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598 作者:GK 教程进入了尾声,该讲的知识点基本讲到了,本节课不会讲新的知识点。...

Gavin-King
11分钟前
1
0
SpringBoot项目热部署

IntelliJ IDEA开发工具 1.需要在pom.xml文件中加入以下依赖。 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> ......

llsydn
13分钟前
0
0
JVM问题排查也不是很难--工具使用

目录 概述 环境准备 工具介绍 远程连接方式 开启JMX 工具远程连接 参考文献 概述 线上环境中,程序越来越慢,一头雾水?遇到程序经常宕机,但找不到原因?排查问题却经常记不住命令? 那是没找到好...

java_龙
17分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部