Bootstrap

mongo删除大表之后的操作

一直很好奇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会连索引一块删除

compact
[quote]
http://docs.mongodb.org/manual/reference/command/compact/

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,重建索引和减少磁盘占用


小结:
所以从文档来看,没有直接必要在删大表之后重新repairDatabase。暂时没找到证据说明repairDatabase之后可以提高性能,只是能够把磁盘空间回收而已

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;