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  参数重启。
    开始不知还有这个参数,直接在主库中删除前面建立的索引了。

   

    网上看到别人是这样处理的,感觉也不错:

        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);
    };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值