Why and when is necessary to rebuild indexes in MongoDB?
Asked Answered
A

2

30

Been working with MongoDB for a while and today I had a doubt while discussing with a colleague.

The thing is that when you create an index in MongoDB, the collection is processed and the index is built.

The index is updated within insertion and deletion of documents so I don't really see the need to run a rebuild index operation (which drops the index and then rebuild it).

According to MongoDB documentation:

Normally, MongoDB compacts indexes during routine updates. For most users, the reIndex command is unnecessary. However, it may be worth running if the collection size has changed significantly or if the indexes are consuming a disproportionate amount of disk space.

Does someone has had the need of running a rebuild index operation that worth it?

Arliearliene answered 20/5, 2015 at 9:11 Comment(2)
I just use it if I add new index.Ahq
But if you add a new index it is built right then, you don't need to reindexArliearliene
B
13

As per the MongoDB documentation, there is generally no need to routinely rebuild indexes.

NOTE: Any advice on storage becomes more interesting with MongoDB 3.0+, which introduced a pluggable storage engine API. My comments below are specifically in reference to the default MMAP storage engine in MongoDB 3.0 and earlier. WiredTiger and other storage engines have different storage implementations for data & indexes.

There may be some benefit in rebuilding an index with the MMAP storage engine if:

  • An index is consuming a larger than expected amount of space compared to the data. Note: you need to monitor historical data & index size to have a baseline for comparison.

  • You want to migrate from an older index format to a newer one. If a reindex is advisible this will be mentioned in the upgrade notes. For example, MongoDB 2.0 introduced significant index performance improvements so the release notes include a suggested reindex to the v2.0 format after upgrading. Similarly, MongoDB 2.6 introduced 2dsphere (v2.0) indexes which have a different default behaviour (sparse by default). Existing indexes are not rebuilt after index version upgrades; the choice of if/when to upgrade is left to the database administrator.

  • You have changed the _id format for a collection to or from a monotonically increasing key (eg. ObjectID) to a random value. This is a bit esoteric, but there's an index optimisation that splits b-tree buckets 90/10 (instead of 50/50) if you are inserting _ids that are always increasing (ref: SERVER-983). If the nature of your _ids changes significantly, it may be possible to build a more efficient b-tree with a re-index.

For more information on general B-tree behaviour, see: Wikipedia: B-tree

Visualising index usage

If you're really curious to dig into the index internals a bit more, there are some experimental commands/tools you can try. I expect these are limited to MongoDB 2.4 & 2.6 only:

Browband answered 21/5, 2015 at 7:4 Comment(4)
I observed a strange behaviour, when I sample a random record from a collection, sometimes it return empty cursor, do you think this is an indexing problem?Canaletto
apparently the malformed index caused the {$sample} return zero document problem, rebuild index fixed that problemCanaletto
@Browband after doing a createIndex on a large collection, should i run reIndex() to have all the existing documents be indexed ?Saundrasaunter
@eranotzap If an index has been created it includes all matching documents that are existing or added/updated in the indexed collection in future -- you do not need to routinely reindex. I would note that this original question & answer are from 2015 when MongoDB 3.0 was the latest server release series. Indexing (and reindexing) concepts have not changed, but the server product has evolved significantly and I would keep that in mind when looking for information to apply to modern server versions.Browband
D
7

While I don't know the exact technical reasons why, in MongoDB, I can make some assumptions about this, based on what I know about indexing from other systems and based on the documentation that you quoted.

The General Idea Of An Index

When moving from one document to the next, in the full document collection, there is a lot of wasted time and effort skipping past all the data that doesn't need to be dealt with. If you're looking for document with id "1234", having to move through 100K+ of each document makes it slow

Rather than having to search through all of the content of each document in the collection (physically moving the disk read heads, etc), an index makes this fast. It's basically a key/value pair that gives you the id and the location of that document. MongoDB can quickly scan through all of the id's in the index, find the locations of the documents that it needs, and go load them directly.

Allocating File Size For An Index

Indexes take up disk space because they are basically a key/value pair stored in a much smaller location. If you have a very large collection (large number of items in the collection) then your index grows in size.

Most operating systems allocate chunks of disk space in certain block sizes. Most database also allocate disk space in large chunks, as needed.

Instead of growing 100K of file size when 100K of documents are added, MongoDB will probably grow 1MB or maybe 10MB or something - I don't know what the actual growth size is. In SQL Server, you can tell it how fast to grow, and MongoDB probably has something like that.

Growing in chunks give the ability to 'grow' the documents in to the space faster because the database doesn't need to constantly expand. If the database now has 10MB of space already allocated, it can just use that space up. It doesn't have to keep expanding the file for each document. It just has to write the data to the file.

This is probably true of collections and indexes for collections - anything that is stored on disk.

File Size And Index Re-Building

When a large collection has a lot of documents added and removed, the index becomes fragmented. index keys may not be in order because there was room in the middle of the index file and not at the end, when the index needed to be built. Index keys may have a lot of space in between them, as well.

If there are 10,000 items in the index, and # 10,001 needs to be inserted, it may be inserted in the middle of the index file. Now the index needs to re-build itself to put everything back in order. This involves moving a lot of data around, to make room at the end of the file and put item # 10,001 at the end.

If the index is constantly being thrashed - lots of stuff removed and added - it's probably faster to just grow the index file size and always put stuff at the end. this is fast to create the index, but leaves empty holes in the file where old things were deleted.

If the index file has empty space where deleted things used to be, this is wasted effort when reading the index. The index file has more movement than needed, to get to the next item in the index. So, the index repairs itself... which can be time consuming for very large collections or very large changes to a collection.

Rebuild For A Large Index File

It can take a lot of disk access and I/O operations to correctly compact the index file back down to a reasonable size, with everything in order. Move out of place items to temp location, free up space in right spot, move them back. Oh by the way, to free up space, you had to move other items to temp location. It's recursive and heavy-handed.

Therefore, if you have a very large number of items in a collection and that collection has items added and removed on a regular basis, the index may need to be rebuilt from scratch. Doing this would wipe the current index file and rebuild from the ground up - which is probably going to be faster than trying to do thousands of moves inside of the existing file. Rather than moving things around, it just writes them sequentially, from scratch.

Large Change In Collection Size

Giving everything I'm assuming above, a large change in the collection size would cause this kind of thrashing. If you have 10,000 documents in the collection and you delete 8,000 of them... well, now you have empty space in your index file where the 8,000 items used to be. MongoDB needs to move the remaining 2,000 items around in the physical file, to rebuild it in a compact form.

Instead of waiting around for 8,000 empty spaces to be cleaned up, it might be faster to rebuild from the ground up with the remaining 2,000 items.

Conclusion? Maybe?

So, the documentation that you quoted is probably going to deal with "big data" needs or high thrashing collections and indexes.

Also keep in mind that I'm making an educated guess based on what I know about indexing, disk allocation, file fragmentation, etc.

My guess is that "most users" in the documentation, means 99.9% or more of mongodb collections don't need to worry about this.

MongoDB specific case

According to MongoDB documentation:

The remove() method does not remove the indexes

So if you delete documents from a collection you are wasting disk space unless you rebuild the index for that collection.

Discomposure answered 20/5, 2015 at 16:4 Comment(1)
Unfortunately your description of indexes misses the fundamental underlying concepts of a B-tree data structure and does not accurately represent how indexes work in MongoDB :). Keys are stored in buckets which represent a range of values... insertion or deletion of a single document does not requiring "rebuilding" an index, it is just adding keys in the appropriate place of the data structure. There's a much better description on Wikipedia. As far as the remove() command goes: it doesn't remove index definitions (but index entries are indeed removed).Browband

© 2022 - 2024 — McMap. All rights reserved.