一直很好奇mongo执行db.collection.drop之后需不需要优化。
所以先查了官方文档:
drop
[quote]
http://docs.mongodb.org/manual/reference/command/drop/
The drop command removes an entire collection from a database.
Note that this command also removes any indexes associated with the dropped collection.
[/quote]
drop会连索引一块删除
Rewrites and defragments all data in a collection, as well as all of the indexes on that collection.
compact may increase the total size and number of your data files, especially when run for the first time. However, this will not increase the total collection storage space since storage size is the amount of data allocated within the database files, and not the size/number of the files on the file system.
compact requires up to 2 gigabytes of additional disk space while running. Unlike repairDatabase, compact does not free space on the file system.
compact commands do not replicate to secondaries in a replica set:
Compact each member separately.
Ideally run compact on a secondary. See option force:true above for information regarding compacting the primary.
[/quote]
可见compact是一个collection的磁盘碎片整理,且会block整个数据库
compact会增加mongo的磁盘占用(且需要2G临时空间),而repairDatabase可以free space
compact操作不会传播到secondaries上,需要一个一个操作
repairDatabase
[quote]
The repairDatabase command checks and repairs errors and inconsistencies with the data storage. The command is analogous to a fsck command for file systems.
Note When using journaling, there is almost never any need to run repairDatabase. In the event of an unclean shutdown, the server will be able restore the data files to a pristine state automatically.
As a side effect, the repairDatabase command will compact the database, as the compact command, and also reduces the total size of the data files on disk. The repairDatabase command will also recreate all indexes in the database.
[/quote]
repairDatabase用来检查和修复错误及数据不一致,与fsck相似
打开journal日志之后几乎没有必要再repariDatabase.
repairDatabase副作用是会compact,重建索引和减少磁盘占用