Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb background indexes - are they still background once created?

When creating an index in mongodb, you can specify the background: true flag, which causes the index creation to be non-blocking. This is great in production since you don't want the whole database locked up while creating an index that you clearly didn't critically need before (since you didn't have it).

Reading the docs, it seems like this flag only determines how the index is created, and once it's done being built the index acts exactly like a normal index. Which is what I'd want -- I wouldn't want the index to get out of sync with docs because it's being updated in the background, although I can imagine a database that does this.

I'm asking here because the getIndexes command shows that the index is still marked as background even after it's created. Is this just a reminder about how it was created? Or do background indexes behave differently after being created? Maybe some subtlety with replication?

like image 444
Leopd Avatar asked Mar 22 '13 20:03

Leopd


People also ask

What is background index in MongoDB?

When creating an index in mongodb, you can specify the background: true flag, which causes the index creation to be non-blocking. This is great in production since you don't want the whole database locked up while creating an index that you clearly didn't critically need before (since you didn't have it).

How are indexes maintained in MongoDB?

Indexes in MongoDB Without them, the database must scan every document in a collection or table to select those that match the query statement. If an appropriate index exists for a query, the database can use the index to limit the number of documents it must inspect.

How many indexes does MongoDB create by way of default for a new collection?

To support efficient queries of geospatial coordinate data, MongoDB provides two special indexes: 2d indexes that uses planar geometry when returning results and 2dsphere indexes that use spherical geometry to return results.

How many indexes can be created in MongoDB?

Maximum Ranges. A collection cannot have more than 64 indexes. The length of the index name cannot be longer than 125 characters. A compound index can have maximum 31 fields indexed.


1 Answers

Once created they act just like regular indexes. They persist in getIndexes just as a reminder, similar to how unique, sparse and so on do.

But it also has the other meaning. Just because foreground indexes block all writers, in this case you won't be able to perform db.testCollection.getIndexes() until all indexes have been created. Meanwhile when you create background index, then you can call db.testCollection.getIndexes() and you will see, that index seems to be already created.

But in this case we can't be sure whether indexes have been actually created or not. In this case you need to call db.currentOp() and if you see something like

{   "inprog": [     {       "opid": 2001060,       "active": true,       "secs_running": 1,       "op": "insert",       "ns": "test.system.indexes",       "insert": {         "v": 1,         "key": {           "a": 1         },         "ns": "test.testCollection",         "name": "a_1",         "background": 1       },           ....       "msg": "bg index build Background Index Build Progress: 368640/1000000 36%",       "progress": {         "done": 368640,         "total": 1000000       }       ...     }   ] } 

then it means, that creation of background indexes is still in progress, and also you can see some information about the process.

For example you may do some rough calculations: 368640 out of 1000000 takes 1 seconds (+1 second as possible offset), therefore everything should take 3-6 seconds (eventually it took 4.8 s).

Obviously if you can't see such operation in progress, then indexes are already created.

Note: if you have many concurrent operations, then you may specify searсh argument for db.currentOp(), f.e.

db.currentOp({"insert.background":1}) 
like image 189
n1ckolas Avatar answered Sep 19 '22 05:09

n1ckolas