MongoDB 1.6 allows to define indexes to be run as background operations. Background indexes seems to be a little slower, but doesn't block other write/read operations so they seems to be the best choice when you need to create indexes on databases already populated with some data.
However, even with empty collections, background indexes allows you to reindex your collection in the future without worrying about concurrent request.
At first glance, I don't see any real advantage of using legacy indexes over background indexes. However, because MongoDB background indexes are not the default option, I'd like to know if is there any tradeoff I didn't considered.
When is preferred to use a standard index instead of a background index in MongoDB.
MongoDB uses multikey indexes to index the content stored in arrays. If you index a field that holds an array value, MongoDB creates separate index entries for every element of the array. These multikey indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays.
Fortunately, MongoDB has an option to build indexes in the background. It means MongoDB can still serve queries and you can alter the database meanwhile it is building the index. It is a really handy feature for larger collections to avoid downtime. Of course, background indexing doesn't come for free.
MongoDB provides a method called createIndex() that allows user to create an index. The key determines the field on the basis of which you want to create an index and 1 (or -1) determines the order in which these indexes will be arranged(ascending or descending).
In documents, the _id field is a default index which is automatically created by MongoDB and we are not allowed to drop this index.
Background indexes seems to be a little slower,
I think this is the key trade-off. In some cases, background indexes will be much, much slower. It's easy to imagine a DB with enough writes that the index creation takes hours or days to catch up.
If this is the case, you normally have to find a way to "queue" your writes until you're done. But being able to "lock out" writes during this time frame is a nice feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With