Compacting MongoDB Collections

in Administration

A while back we wrote a post explaining how to compact MongoDB data files, that example shows how to use some server side javascript and a cron job to automatically compact the data files on a schedule … however this isn’t always ideal as it will compact all the collections in a database at one time.

If you have a very large Collection you didn’t want to run the operation on …or a number of Collections in your database you didn’t wish to compact you were a bit out of luck.

Also, using repairDatabase() requires the disk space for the current database plus the repaired copy (i.e. double the disk space.)

Enter MongoDB 2.0 … in 2.0+ you can now use the simple compact command to target the compact to a single collection.

Running compact has three major performance benefits:

  • Compacts collection (less disk space.)
  • Defragments a collection (data pages are aligned better.)
  • Rebuilds and compacts the collection’s indexes (less RAM needed, and better perf.)

If  you have a lot of read/write/delete operations going on in your Collection this could possibly have a fairly noticeable performance impact.

There are two ways to run the compact command:

> db.yourCollection.runCommand("compact");
> db.runCommand({ compact : 'yourCollection' });

Now, that said there are two big downsides to using compact …

  • The compact command blocks operations on the collection until it’s done compacting (so it’s best to run this off hours during scheduled maintenance.)
  • It’s typically slower than repairDatabase in its actual operational time.

Those aside, as part of routine maintenance compact is a really helpful new feature and might justify an upgrade to 2.0 all on it’s own!

1 Comment