I have a MongoDB collection which has some thousands of complex document objects. When I run a query which has some more than 10 - 15 find conditions of nested and deeply nested values, it takes around 20-30 ms to yield the result. I was googling about improving query speed and I came to know there is a way to store entire collection in memory.
Can somebody please through some light over how to keep entire collection cached in memory?
I don't wanna lose my on-disk collection, I just want to have my entire collection to be available in memory and I'm planning to update in memory collection every few minutes from my on-disk collection.
NOTE: I don't wanna use the only in-memory collection, I want to preserve my ability to have persistent data
答案 0 :(得分:0)
根据您使用的存储引擎,使用MMAPv1,您可以将数据和索引加载到内存中,然后运行touch命令:
db.runCommand({ touch: "collection", data: true, index: true })
可以在此处找到更多信息:https://docs.mongodb.com/manual/reference/command/touch/
但是,如果你正在使用WiredTiger存储引擎,你可以编写一个简单的查询来命中所有索引:
db.collection.find({indexedField: 1}).count()
或使用find:
在内存中加载文档db.collection.find({documentsIWant: true})