在2.0以后,可以使用compact对集合进行压缩,释放空间,这个类似于repairdatabase,但是repairdatabase只能对整个数据库进行压缩,所以在效率和速度上,compact更快,也不会需要那么多的磁盘空间。<wbr style=""><div style=""> 运行格式如下:</div> <div style=""> <div style="line-height:25px">> db.runCommand( { compact : 'mycollectionname' } )</div> <div style="line-height:25px">> // or</div> <div style="line-height:25px">> db.mycollection.runCommand( "compact" )</div> </div> <div style=""> <span style="font-family:helvetica,arial,sans-serif; line-height:17px; background-color:rgb(243,244,235); font-size:13px"></span> <h3 style="padding-bottom:0px; line-height:normal; margin:20px 0px 0.5em; padding-left:0px; padding-right:0px; color:rgb(0,51,102); font-size:14pt; font-weight:bold; text-decoration:none!important; padding-top:0px"> Options</h3> <ul style="position:relative; padding-bottom:0px; line-height:13pt; list-style-type:disc; margin:0px 0px 1em; padding-left:1.2em; padding-right:0px; color:rgb(76,58,44)!important; font-size:10pt; text-decoration:none!important; padding-top:0px"> <li style="padding-bottom:0px; line-height:13pt; list-style-type:disc; margin:0px 0px 0px 1em; padding-left:0px; padding-right:0px; font-size:10pt; padding-top:0px"> <tt style="line-height:23px; margin:0px">force:true</tt>Because this blocks all other activity (in v2.2, only for its database), the compact command returns an error when invoked on a replica set primary. Use<tt style="line-height:23px; margin:0px">force:true</tt>to run on a replica set primary.</li> <li style="padding-bottom:0px; line-height:13pt; list-style-type:disc; margin:0px 0px 0px 1em; padding-left:0px; padding-right:0px; font-size:10pt; padding-top:0px"> <tt style="line-height:23px; margin:0px">paddingFactor:<pf></tt>(v2.2+). Specify a padding factor for the compacted documents. By default the documents are fully compacted with no padding. If you do updates on the documents which grow them, and especially if you have several indexes for the collection, you will want some padding. Default is 1.0 (no padding), maximum 4.0.</li> <li style="padding-bottom:0px; line-height:13pt; list-style-type:disc; margin:0px 0px 0px 1em; padding-left:0px; padding-right:0px; font-size:10pt; padding-top:0px"> <tt style="line-height:23px; margin:0px">paddingBytes:<bytes></tt>(v2.2+.) Specify padding as an absolute number of bytes. Can be useful if your documents start at small but then grow a large amount; for example if they start 40 bytes long and then you grow then by 1KB, paddingBytes:1024 might be reasonable whereas paddingFactor:4.0 would only add 40*(4.0-1)=120 bytes of padding.</li> <li style="padding-bottom:0px; line-height:13pt; list-style-type:disc; margin:0px 0px 0px 1em; padding-left:0px; padding-right:0px; font-size:10pt; padding-top:0px"> <span style="color:#000000; line-height:23px; background-color:rgb(255,255,255)"></span> <pre style="text-align:left; padding-bottom:0px; line-height:1.3; overflow-x:auto; overflow-y:auto; margin:10px 0px; padding-left:0px; padding-right:0px; font-family:'Courier New',Courier,monospace; white-space:pre-wrap; padding-top:0px"><span style="color:#808080; line-height:23px; margin:0px">// example. always add at least 100 bytes of padding, and at least 10% of the doc size too </span>> db.runCommand( { compact : 'mycollectionname', paddingBytes:100, paddingFactor:1.1 } )</pre> </li> </ul> </div> <div style=""><br style="line-height:25px"></div> <div style="">另外如果在replica sets中,需要注意的几点:</div> <div style=""> <div style="line-height:25px">1.在mongodb replica sets中主节点直接运行会报错,需要加上force:true</div> <div style="line-height:25px"> db.runCommand( { compact : 'mycollectionname',force:true})</div> <div style="line-height:25px">2.如果在从节点运行,那么从节点状态会变成recovering状态,结束后,自动转变为secondary状态</div> <div style="line-height:25px">3.在replica sets中,要分别在每个节点执行,一个节点执行了compact,其他节点并不能复制</div> <div style="line-height:25px"><br style="line-height:25px"></div> <div style="line-height:25px"><span style="font-family:helvetica,arial,sans-serif; color:#03366; line-height:normal; background-color:rgb(243,244,235); font-size:19px; font-weight:bold">Interrupting a compaction ---终止compact导致的一些结果</span></div> <div style="line-height:25px"> <span style="font-family:helvetica,arial,sans-serif; line-height:17px; background-color:rgb(243,244,235); font-size:13px"></span> <p style="padding-bottom:0px; line-height:13pt; margin:0px 0px 10px; padding-left:0px; padding-right:0px; max-width:800px; color:rgb(76,58,44)!important; font-size:10pt; text-decoration:none!important; padding-top:0px"> At the beginning of compaction, indexes are dropped for the collection. At the end, indexes are rebuilt. Thus, if you kill the compaction in the middle of its operation, either with<a title="Viewing and Terminating Current Operation" href="http://www.mongodb.org/display/DOCS/Viewing+and+Terminating+Current+Operation" rel="nofollow" style="line-height:23px; margin:0px; color:rgb(0,51,102); text-decoration:none">killOp</a>or a server failure, indexes may be missing from your collection. If you are running with<tt style="line-height:23px; margin:0px">--journal</tt>, no data should ever be lost on a crash during compaction, although on startup the indexes will not be present. (Regardless of this, always do a backup before system maintenance such as this!) When the indexes are rebuilt, they will be in the<a title="Index Versions" href="http://www.mongodb.org/display/DOCS/Index+Versions" rel="nofollow" style="line-height:23px; margin:0px; color:rgb(0,51,102); text-decoration:none">2.0 index format</a>.</p> <p style="padding-bottom:0px; line-height:13pt; margin:0px 0px 10px; padding-left:0px; padding-right:0px; max-width:800px; color:rgb(76,58,44)!important; font-size:10pt; text-decoration:none!important; padding-top:0px"> If there's a crash while the command is running, then as long as journaling is enabled, your data will be safe.</p> <p style="padding-bottom:0px; line-height:13pt; margin:0px 0px 10px; padding-left:0px; padding-right:0px; max-width:800px; color:rgb(76,58,44)!important; font-size:10pt; text-decoration:none!important; padding-top:0px"> Additionally, if a compaction operation is interrupted, much of the existing free space in a collection may become un-reusable. In this scenario, it is recommended that compaction be run again, to completion, to restore use of this free space</p> <p style="padding-bottom:0px; line-height:13pt; margin:0px 0px 10px; padding-left:0px; padding-right:0px; max-width:800px; color:rgb(76,58,44)!important; font-size:10pt; text-decoration:none!important; padding-top:0px"> 主要说的是索引在执行中,会重建,如果意外终止,会丢失数据,不过如果开启了journal,数据相对来说安全很多,还有就是空闲空间会变成不能重用,需要再次执行compact,恢复空闲空间的使用。</p> </div> </div> </wbr>
mongodb compact
最新推荐文章于 2023-04-19 09:31:37 发布