Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is preferred to use a standard index instead of a background index in MongoDB?

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.

like image 874
Simone Carletti Avatar asked Oct 06 '10 14:10

Simone Carletti


People also ask

How does MongoDB choose which index to use?

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.

What is background index in MongoDB?

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.

What is different option for indexing in MongoDB?

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).

What is the default index in MongoDB?

In documents, the _id field is a default index which is automatically created by MongoDB and we are not allowed to drop this index.


1 Answers

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.

like image 67
Gates VP Avatar answered Sep 27 '22 02:09

Gates VP