Sunday, August 25, 2019

MongoDB Index: Rebuild and Compact

REBUILD: Mongo can build index in two ways foreground and background. If you dont mention anything, by default it uses foreground building strategy. Foreground  imposes a lock on the collection for the duration of the build.
During foreground index build, no other query or write can happen on that collection. Even few server wide operations might be affected if they need that database lock.

You can specify the backgroud build of index in the

db.myCollection.ensureIndex({....}),{background:true})

but background index build take longer time.
=========================

You can choose to index rebuild using reIndex() function on an collection from the shell. this send s the reindex command to the server, this drops and recreate all the indexes on theat collection.

if you use db repair on the database, the indexes on a collection will be recreated as well.
But db repair is not necessary for healthy databases and should not be run unless absolutely necessary.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
COMPACT: During scheduled maintainence windows , you may run compact on your collections. compact defragment an collection and rebuild it indexes. Compact is a blocking operation and takes time to complete. But running compact during maintainence windows, ensure that your collections, storage and indexes are structually optimized. Compact command is performance on a single collection. to compact our messages collection.

>db.runCommand({compact:'messages'})
{ "ok" : 1 }
>

this is sample collection with little data, but your actual collection compact can take much time. Compact is also blocking , so make sure you run it only during maintainence windows.
Please note: On replica set, you can run offline secondaries in replica set. and stepDown() primary before compacting it.


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home