How to compress the data file of MongoDB
How to compress MongoDB data files, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
MongoDB adopts the mechanism of pre-allocation of disk space, in order to avoid disk fragments and further memory fragments caused by using mmap, but with the addition, deletion and modification of data, the data file will inevitably produce holes, resulting in a waste of disk space and memory.
How to compress the data file of MongoDB
1. Organize data files through repairDatabase
RepairDatabase is a method on db built into MongoDB. When this method is called, MongoDB scans all data in db and reorganizes the data collection by reinserting.
The price of this method is that the read and write operations on this db will become very slow, and even write operations will be lost. Therefore, it is best to stop the write operation of the client directly at this time.
You can create a bar.js file and write the following code:
/ / Getathecurrentcollectionsize.
Varstorage=db.foo.storageSize ()
Vartotal=db.foo.totalSize ()
Print ('StorageSize:'+tojson (storage))
Print ('TotalSize:'+tojson (total))
Print ('-')
Print ('Runningdb.repairDatabase ()')
Print ('-')
/ / Runrepair
Db.repairDatabase ()
/ / Getnewcollectionsizes.
Varstorage_a=db.foo.storageSize ()
Vartotal_a=db.foo.totalSize ()
Print ('StorageSize:'+tojson (storage_a))
Print ('TotalSize:'+tojson (total_a))
Then run:
$mongofoobar.js
Running result:
MongoDBshellversion:1.6.4
Connectingto:foo
StorageSize:51351
TotalSize:79152
Runningdb.repairDatabase ()
StorageSize:40960
TotalSize:65153
How to compress the data file of MongoDB
two。 If the application is acceptable, consider using cappedcollection
Cappedcollection is a special collection in MongoDB, its size can be limited, and the data is written cyclically within this limited size. After the data set reaches the upper limit, the new data will overwrite the old data. In this way, the holes in the disk will be eliminated automatically after a period of time.
In fact, the use of replication-related functions can also achieve the above data compression, the specific process is more troublesome, but also involves the switching of the client and other operations.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.