Question about the mechanism of mongodb index?

when mongodb uses an index query, whether to put the data index into memory when the data is created, or whether the index is loaded into memory when the query is made, and whether the index in memory will not be automatically cleared?
how to estimate the memory occupied by the data index, such as a data size of 1kb, how much memory will be occupied by the data creation index?

Apr.01,2021

Writing data and modifying the index are done in a single transaction. Yes, although MongoDB has only supported external transactions since 4. 0, the WiredTiger engine has supported internal transactions from the beginning. Since you want to change the index at the same time when writing data, you must first 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 recent principle of least use of (LRU), whether it is an index or not. If you use it frequently, you have a better chance of residing memory.
Last question, there is no such simple method of calculation. An index is essentially a key-value pair. Values take up the same amount of space, but the size of the key is not the same. And the index keys will also be compressed, it is difficult to estimate the final actual size. The easiest way is to simulate a batch of data according to your request, and then look at db. < Collection > .stats () , which will have the index size. Then estimate how big the index will be when the actual amount of data is reached based on the amount of data and the size of the index. This size is also roughly the amount of memory that needs to be occupied.

Menu