How does mongodb reclaim the space that should be vacated after the document is "shrunk"?
漂亮男人
漂亮男人 2017-05-02 09:24:24
0
2
596

The version used is mongodb-win32-x86_64-3.2.1, and the default WiredTiger storage engine is used.

There are approximately 4 million documents in a collection, occupying 40G of space; later, the useless part of each document processing was traversed and updated (the document was not deleted). The script to perform this task ran and completed as scheduled. According to its output statistics, the document The overall reduction has been reduced to about 45% of the original, which means that more than 20G of space has been freed up. However, the database file size has not become smaller, but has increased a little (about 1%).

Read the chapter about storage in the document, https://docs.mongodb.com/v3.2...,其中提到删除文档导致的空记录占用空间可通过compact回收(使用WiredTiger存储引擎时),没提到前述这种修改文档形成的“虚占空间”的情形,我运行了compact,几秒钟就结束了,存储文件没有变小。那么该怎么办呢?

> use cache
switched to db cache
> db.htmldocs.runCommand("compact")
{ "ok" : 1 }
> db.runCommand({compact: "htmldocs"})
{ "ok" : 1 }
漂亮男人
漂亮男人

reply all(2)
左手右手慢动作

Yes, if WT uses the latest version, it does seem to be lighter-weight, but the unavoidable problem is that it is still a heavy operation. Imagine disk defragmentation in Windows, the principle is the same. A more recommended, less impactful approach is to use a replica set to rollingly reinitialize all nodes, and the space can be released. compact确实可以回收空间,但是因为某个bug这应该是在3.2的某个版本之后生效的,我不记得具体版本号(3.2.9?),但是可以肯定3.2.1肯定无效,而升级到最新版本3.2.12肯定能解决这个问题。
相对于repairDatabase来说,compact

The more important question is, does reclaiming this space actually make sense to you? Most of the time, the fact that the system has occupied these spaces means that it is likely to occupy these spaces again (unless there are changes in the system design). Instead of releasing it and reallocating it later, it is better to keep it there and the system will reuse it.

伊谢尔伦

The following is my understanding:

1. Compact of a single collection will not reclaim the storage space of the file system; only db.repairDatabase() of the entire database can reclaim the storage space of the file system. Please try using repirDatabase().

2. You can also use the export/import collection method

Be aware of the impact of the above operations on system performance. for reference.

I’m on vacation these days, and I wish you a happy Spring Festival holiday in advance.

Love MongoDB! Have Fun!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template