Reputation: 2605
Does MongoDB load all the indexes from multiple databases and collections in memory after it starts or whenever it will load indexes only when it encountered a query? I am trying to understand does MongoDB supports the eager or lazy loading of indexes.
Upvotes: 2
Views: 557
Reputation: 28366
It lazily loads indexes. It may even load only part of an index if it doesn't need it in its entirety for the current query.
To demonstrate this:
{i: <int>}
{i:1}
db.testcollection.stats().indexSizes
db.serverStatus().wiredTiger.cache["bytes currently in the cache"]
At this point you should be able to note that the total amount of data in the cache is less than the size of the indexes on the test collection
db.testcollection.count({i:{$lt:50}})
If you explain the count command, it should show it using a COUNT_SCAN stage, which means that it is completely service by the index and does not need to load any documents.
Upvotes: 4