发布于 2015-09-14 15:03:32 | 281 次阅读 | 评论: 0 | 来源: 网络整理
2.0 新版功能.
The compact command rewrites and defragments a single collection. Additionally, the command drops all indexes at the beginning of compaction and rebuilds the indexes at the end. compact is conceptually similar to repairDatabase, but works on a single collection rather than an entire database.
The command has the following syntax:
{ compact: <collection name> }
You may also specify the following options:
参数: |
|
---|
警告
Always have an up-to-date backup before performing server maintenance such as the compact operation.
Note the following behaviors:
compact blocks all other activity. In MongoDB 2.2, compact blocks activities only for its database. You may view the intermediate progress either by viewing the mongod log file, or by running the db.currentOp() in another shell instance.
compact removes any padding factor in the collection when issued without either the paddingFactor option or the paddingBytes option. This may impact performance if the documents grow regularly. However, compact retains existing paddingFactor statistics for the collection that MongoDB will use to calculate the padding factor for future inserts.
compact generally uses less disk space than repairDatabase and is faster. However,the compact command is still slow and does block other database use. Only use compact during scheduled maintenance periods.
If you terminate the operation with the db.killOp() method or restart the server before it has finished:
compact may increase the total size and number of our 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 a small amount of additional disk space while running but unlike repairDatabase it does not free space on the file system.
You may also wish to run the collStats command before and after compaction to see how the storage space changes for the collection.
compact commands do not replicate to secondaries in a replica set:
警告
If you run compact on a secondary, the secondary will enter a RECOVERING state to prevent clients from sending read operations during compaction. Once the compaction finishes the secondary will automatically return to SECONDARY state. See state for more information on states.
Refer to the “partial script for automating step down and compaction” for an example of this procedure.
compact is a command issued to a mongod. In a sharded environment, run compact on each shard separately as a maintenance operation.
It is not possible to compact capped collections because they don’t have padding, and documents cannot grow in these collections. However, the documents of a capped collections are not subject to fragmentation.
也可以参考