mongodb 索引建立问题
生活随笔
收集整理的這篇文章主要介紹了
mongodb 索引建立问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
mongodb? 索引建立問題
1.主從庫索引建立不是同步建立:
?? ?MONGODB 2.6.6 版本,索引建立并不是同步的,而是主庫建立完成后,從庫接著建立索引。
?? ?不知是3.0版本是否也一樣,以前使用3.0版本也沒去監控主從日志情況。這坑太大了。
?? ?5:00開始建立,以為主庫完成即OK.
?? ??? ?官方說明:
?? ??? ?Background index build (page 491) allowed on secondaries. If you initiate a background index build on a
?? ??? ?primary, the secondaries will replicate the index build in the background.
?? ??? ?Automatic rebuild of interrupted index builds after a restart.
?? ??? ?– If a standalone or a primary instance terminates during an index build without a clean shutdown, mongod
?? ??? ?now restarts the index build when the instance restarts. If the instance shuts down cleanly or if a user kills
?? ??? ?the index build, the interrupted index builds do not automatically restart upon the restart of the server.
?? ??? ?– If a secondary instance terminates during an index build, the mongod instance will now restart the interrupted index build when the instance restarts.
?? ??? ?To disable this behavior, use the --noIndexBuildRetry command-line option.
?? ?從庫建立索引太長時間,如果中斷從庫建立索引,可以使用 --noIndexBuildRetry? 參數重啟。
?? ?開始不知還有這個參數,直接在主庫中刪除前面建立的索引了。
?? ?下面是日志
?? ?primary:
?? ?2015-12-17T07:46:53.074+0800 [conn5530345]????????????? Index Build(background): 27309700/27314911????? 99%
?? ?。。。
?? ?slave:
?? ??? ?2015-12-17T07:46:54.985+0800 [repl index builder 14]???? building index in background
?? ??? ?2015-12-17T07:46:55.529+0800 [conn2154798] killcursors? keyUpdates:0 numYields:0 locks(micros) r:73 167ms
?? ??? ?2015-12-17T07:46:58.226+0800 [repl index builder 14]??????????? Index Build(background): 4700/27314911? 0%
2.TTL 索引刪除數據,不是一次性刪除超出日期的數據,而是一點點來的,
?? ?也是一樣,以前一直以為是索引建立后,TTL 刪除數據是一次性操作,那里知道幾個小時也沒刪除完成,導致數據庫
?? ?壓力一直很高。
?? ?后來只好先改名,再建立好新表后把數據插入進去。
?? ?db.WebLog.renameCollection("WebLog_bak");
?? ?db.WebLog.ensureIndex({MallID:1,Mac:1});
?? ?db.WebLog.ensureIndex({CreateTime:-1},{expireAfterSeconds:3600*24*3*30});
?? ?var cursor=db.WebLog_bak.find({_id:{$gte:24600000}});
?? ?while (cursor.hasNext()){
?? ?? nc=cursor.next();
?? ?? db.WebLog.insert(nc);
?? ?};
1.主從庫索引建立不是同步建立:
?? ?MONGODB 2.6.6 版本,索引建立并不是同步的,而是主庫建立完成后,從庫接著建立索引。
?? ?不知是3.0版本是否也一樣,以前使用3.0版本也沒去監控主從日志情況。這坑太大了。
?? ?5:00開始建立,以為主庫完成即OK.
?? ??? ?官方說明:
?? ??? ?Background index build (page 491) allowed on secondaries. If you initiate a background index build on a
?? ??? ?primary, the secondaries will replicate the index build in the background.
?? ??? ?Automatic rebuild of interrupted index builds after a restart.
?? ??? ?– If a standalone or a primary instance terminates during an index build without a clean shutdown, mongod
?? ??? ?now restarts the index build when the instance restarts. If the instance shuts down cleanly or if a user kills
?? ??? ?the index build, the interrupted index builds do not automatically restart upon the restart of the server.
?? ??? ?– If a secondary instance terminates during an index build, the mongod instance will now restart the interrupted index build when the instance restarts.
?? ??? ?To disable this behavior, use the --noIndexBuildRetry command-line option.
?? ?從庫建立索引太長時間,如果中斷從庫建立索引,可以使用 --noIndexBuildRetry? 參數重啟。
?? ?開始不知還有這個參數,直接在主庫中刪除前面建立的索引了。
????
??? 網上看到別人是這樣處理的,感覺也不錯:
?? ??? ?1.首先把 secondary server 停止,在取消 --replSet 參數,并且更改 MongoDB port 之后重新啟動 MongoDB,這時候 MongoDB 將進入 standalone 模式;
?? ??? ?2.在 standalone 模式下運行命令 ensureIndex 建立索引,建議使用 foreground 方式運行;
?? ??? ?3.建立索引完畢之后關閉 secondary server 按正常方式啟動;
?? ?下面是日志
?? ?primary:
?? ?2015-12-17T07:46:53.074+0800 [conn5530345]????????????? Index Build(background): 27309700/27314911????? 99%
?? ?。。。
?? ?slave:
?? ??? ?2015-12-17T07:46:54.985+0800 [repl index builder 14]???? building index in background
?? ??? ?2015-12-17T07:46:55.529+0800 [conn2154798] killcursors? keyUpdates:0 numYields:0 locks(micros) r:73 167ms
?? ??? ?2015-12-17T07:46:58.226+0800 [repl index builder 14]??????????? Index Build(background): 4700/27314911? 0%
2.TTL 索引刪除數據,不是一次性刪除超出日期的數據,而是一點點來的,
?? ?也是一樣,以前一直以為是索引建立后,TTL 刪除數據是一次性操作,那里知道幾個小時也沒刪除完成,導致數據庫
?? ?壓力一直很高。
?? ?后來只好先改名,再建立好新表后把數據插入進去。
?? ?db.WebLog.renameCollection("WebLog_bak");
?? ?db.WebLog.ensureIndex({MallID:1,Mac:1});
?? ?db.WebLog.ensureIndex({CreateTime:-1},{expireAfterSeconds:3600*24*3*30});
?? ?var cursor=db.WebLog_bak.find({_id:{$gte:24600000}});
?? ?while (cursor.hasNext()){
?? ?? nc=cursor.next();
?? ?? db.WebLog.insert(nc);
?? ?};
總結
以上是生活随笔為你收集整理的mongodb 索引建立问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL\MariaDB 多线程复制初
- 下一篇: 使用NOSQL的MongoDB时建立索引