search

Home  >  Q&A  >  body text

使用MongoDB如何改善数据空洞

数据空洞指的是,一个表经历了频繁的update和delete后,其数据文件的尺寸大于其数据实际尺寸。比如show dbs看到一个库为400G,导出再导入后就只有250G。

PHPzPHPz2802 days ago876

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-22 08:58:10

    There is no need to export and then import, using repairDatabase 或者 compact will compress the data. But they have an impact on performance during operation.

    The repairDatabase command compacts all collections in the database. It is identical to running the compact command on each collection inpidually.

    It should be noted that compact不会像repairDatabasethis will release the disk space that has been applied for, but new disk requests will use the organized space. Another thing to note is that both commands require additional space, see the dick usage section. This means that if the disk space is full, these two commands will not be used. However, at this time, a slave node can be resynchronized and the disk data can be reconstructed.

    If you use repairDatabase或者compact in a replica set, it is best to use the rolling method. The method of rolling is to first take out a secondary node and defragment the disk. Then repeat the above steps on all slave nodes. Finally, step down primary and defragment the disk on it. This can avoid the impact of these operations on online services.

    Recommended to use usePowerOf2Sizes allows MongoDB to use the disk more efficiently under frequent updates and deletions, because each application and release is a power of 2 in size.

    reply
    0
  • Cancelreply