Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Internal implementation of indexing?

I've learned a lot of things about indexing and finding some stuff from here.

Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

But i still have some questions:

  1. While Creating index using (createIndex), is the Record always stored in RAM?
  2. Is every time need to create Index Whenever My application is going to restart ?
  3. What will Happen in the case of default id (_id). Is always Store in RAM.
  4. _id Is Default Index, That means All Records is always Store in RAM for particular collections?

Please help me If I am wrong. Thanks.

like image 843
nitesh singh Avatar asked Dec 31 '16 10:12

nitesh singh


1 Answers

I think, you are having an idea that indexes are stored in RAM. What if I say they are not.

First of all we need to understand what are indexes, indexes are basically a pointer to tell where on disk that document is. Just like we have indexing in book, for faster access we can see what topic is on which page number.

So when indexes are created, they also are stored in the disk, But when an application is running, based on the frequent use and even faster access they get loaded into RAM but there is a difference between loaded and created.

Also loading an index is not same as loading a collection or records into RAM. If we have index loaded we know what all documents to pick up from disk, unlike loading all document and verifying each one of them. So indexes avoid collection scan.

Creation of indexes is one time process, but each write on the document can potentially alter the indexing, so some part might need to be recalculating because records might get shuffled based on the change in data. that's why indexing makes write slow and read fast.

Again think of as a book, if you add a new topic of say 2 pages in between the book, all the indexes after that topic number needs to be recalculated. accordingly.

While Creating index Using (createIndex),Is Record always store in RAM ?.

  • No, records are not stored in RAM, while creating it sort of processes all the document in the collection and create an index sheet, this would be time consuming understandably if there are too many documents, that's why there is an option to create index in background.

Is every time need to create Index Whenever My application is going to restart ?

  • Index are created one time, you can delete it and create again, but it won't recreated on the application or DB restart. that would be insane for huge collection in sharded environment.

What will Happen in the case of default id (_id). Is always Store in RAM.

  • Again that's not true. _id comes as indexed field, so index is already created for empty collection, as when you do a write , it would recalculate the index. Since it's a unique index, the processing would be faster.

_id Is Default Index, That means All Records is always Store in RAM for particular collections ?

  • all records would only be stored in RAM when you are using in-memory engine of MongoDB, which I think comes as enterprise edition. Due to indexing it would not automatically load the record into RAM.
like image 66
Rahul Kumar Avatar answered Oct 03 '22 15:10

Rahul Kumar