mongodb建立索引遇到的坑

       最近为线上的mongodb建立索引。当时mongodb单collection存有大约1亿条记录,在建立索引过程中发现程序报警,mongodb写入超时,不能及时消费MQ中的消息。

       当时心里想肯定是建索引的过程较慢,阻塞mongodb的读写请求。直接查了一下资料,mongodb 文档 如下:

       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.

     mongodb 建立索引默认是阻塞该集合上的读写操作,只有指定background 才会在后台建立索引,恰好与个人的直觉相反,感觉挺坑。如果想在后台建立索引可以 如下面这样

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


    另外mongodb 出现问题时可以借助 mongostat 来查看mongodb的状态,例如每秒的读写次数等等。

    当时用mongostat连接不上去mongodb。只好通过mongodb的log来查看mongodb的状态。通过mongodb的log可以看到mongodb建立索引的过程进度。当初多亏看到这个进度才没有重新启动mongodb,所否可能中途直接杀死mongodb,mongodb会画更多时间来恢复,也有可能起不来(只是猜想)。


转载于:https://my.oschina.net/robinyao/blog/425014

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值