How to Improve Data Holes with MongoDB

  mongodb, question

Data hole refers to the fact that the size of a table’s data file is larger than its actual data size after frequent update and delete. such asshow dbsSee a library is 400G, only 250G after export and import.

Do not need to export and import, userepairDatabaseOr ..compactWill compress the data. But their operation has an impact on performance.

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

It should be noted thatcompactNot likerepairDatabaseThat frees up the disk space that has already been requested, but new disk requests will use the sorted space. Another thing to note is that both commands require extra space, seeDicksusage section. This means that if the disk space is full, the two commands will not be used. However, at this time, you can start againSynchronizing a Slave Node, rebuild the disk data.

If used in replica setrepairDatabaseOr ..compactIf so, it is best to adopt the rolling method.
The Rolling method is to take one out of the secondary and arrange the disks. Then repeat the above steps on all slave nodes. LastRemove the primary node, and then organize the disks on it. This can avoid the impact of these operations on online services.

Recommended useusePowerOf2SizesLet MongoDB use disks more effectively under frequent updates and deletions, because each application and release is a power of 2.