Question on mongodb Index Mechanism?

  mongodb, question

Mongodb uses index to query, does it put the data index into memory when the data is created, or does it load the index into memory when the data is queried, and does the index in memory not automatically clear if it is resident?
How to estimate the amount of memory occupied by data index, for example, 1kb for a piece of data, how much memory will be occupied by creating index for that piece of data?

Writing data and modifying indexes are completed in one transaction. Yes, although MongoDB only started supporting external affairs from 4.0, the WiredTiger engine supported internal affairs from the beginning. Since the index must be changed at the same time when writing data, first you must find out which part of the index to change. Therefore, it is also necessary to query the index when updating. Since you want to query, the index must be in memory.
All WiredTiger caches are cleared according to the Least Recently Used (LRU) principle, regardless of index. If you use it frequently, you will have a greater chance to reside in memory.
Finally, there is no such simple calculation method. Indexes are essentially key-value pairs. Values take up the same space, but the keys are different in size. Moreover, the index keys will be compressed, making it difficult to estimate the final actual size. The simplest way is to simulate a batch of data according to your requirements and then check it out.Db. < collection >. stats ()Which will have the index size. Then, according to your data quantity and index size, estimate roughly how big the index will be when the actual data quantity is reached. This size is also roughly the amount of memory needed.