mongodb 了解MongoDB缓存系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17534628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
understand MongoDB cache system
提问by Ludo
This is a basic question, but very important, and i am not sure to really get the point.
这是一个基本问题,但非常重要,我不确定是否真的明白这一点。
On the official documentation we can read
在官方文档上我们可以阅读
MongoDB keeps all of the most recently used data in RAM. If you have created indexes for your queries and your working data set fits in RAM, MongoDB serves all queries from memory.
MongoDB 将所有最近使用的数据保存在 RAM 中。如果您为查询创建了索引并且您的工作数据集适合 RAM,MongoDB 将从内存中提供所有查询。
The part i am not sure to understand is
我不确定理解的部分是
If you have created indexes for your queries and your working data set fits in RAM
如果您为查询创建了索引并且您的工作数据集适合 RAM
what does mean "indexes" here?
这里的“索引”是什么意思?
For example, if i update a model, then i query it, because i have updated it, it's now in RAM so it will come from the memory, but this is not very clear in my mind.
例如,如果我更新一个模型,然后我查询它,因为我已经更新了它,它现在在 RAM 中,所以它将来自内存,但这在我看来并不是很清楚。
How can we be sure that datas we query will come from the memory or not? I understand that MongoDB uses the free memory to cache datas about the memory which is free on the moment, but does someone could explain further the global behavior ?
我们如何确定我们查询的数据是否来自内存?我知道 MongoDB 使用空闲内存来缓存有关当前空闲内存的数据,但是有人可以进一步解释全局行为吗?
In which case could it be better to use a variable in our node server which store datas than trust the MongoDB cache system?
在哪种情况下,在我们的节点服务器中使用存储数据的变量比信任 MongoDB 缓存系统更好吗?
How do you globally advise to use MongoDB for huge traffic?
您如何在全球范围内建议将 MongoDB 用于巨大的流量?
采纳答案by Sammaye
Note: This was written back in 2013 when MongoDB was still quite young, it didn't have the features it does today, while this answer still holds true for mmap, it does not for the other storage technologies MongoDB now implements, such as WiredTiger, or Percona.
注意:这是在 2013 年 MongoDB 还很年轻的时候写的,它没有今天的功能,虽然这个答案仍然适用于 mmap,但它不适用于 MongoDB 现在实现的其他存储技术,例如 WiredTiger ,或佩尔科纳。
A good place to start to understand exactly what isan index: http://docs.mongodb.org/manual/core/indexes/
开始了解什么是索引的好地方:http: //docs.mongodb.org/manual/core/indexes/
After you have brushed up on that you will udersand why they are so good, however, skipping forward to some of the more intricate questions.
在您重新了解之后,您将了解为什么它们如此出色,但是,跳到一些更复杂的问题。
How can we be sure that datas we query will come from the memory or not?
我们如何确定我们查询的数据是否来自内存?
One way is to look at the yields
field on any query explain()
. This will tell you how many times the reader yielded its lock because data was not in RAM.
一种方法是查看yields
任何查询的字段explain()
。这将告诉您阅读器因数据不在 RAM 中而放弃锁定的次数。
Another more indepth way is to look on programs like mongostatand other such programs. These programs will tell you about what page faults (when data needs to be paged into RAM from disk) are happening on your mongod
.
另一种更深入的方法是查看诸如mongostat 之类的程序和其他此类程序。这些程序会告诉您在您的mongod
.
I understand that MongoDB uses the free memory to cache datas about the memory which is free on the moment, but does someone could explain further the global behavior ?
我知道 MongoDB 使用空闲内存来缓存有关当前空闲内存的数据,但是有人可以进一步解释全局行为吗?
This is actually incorrect. It is easier to just say that MongoDB does this but in reality it does not. It is in fact the OS and its own paging algorithms, usually the LRU, that does this for MongoDB. MongoDB does cache index plans for a certain period of time though so that it doesn't have to constantly keep checking and testing for indexes.
这实际上是不正确的。简单地说 MongoDB 这样做更容易,但实际上它没有。实际上是操作系统和它自己的分页算法,通常是LRU,为 MongoDB 做这件事。MongoDB 确实会在一段时间内缓存索引计划,因此它不必经常检查和测试索引。
In which case could it be better to use a variable in our node server which store datas than trust the MongoDB cache system?
在哪种情况下,在我们的节点服务器中使用存储数据的变量比信任 MongoDB 缓存系统更好吗?
Not sure how you expect that to work...I mean the two do quite different things and if you intend to read your data from MongoDB into your application on startup into that var then I definitely would not recommend it.
不确定你希望它如何工作......我的意思是两者做的事情完全不同,如果你打算在启动时将你的数据从 MongoDB 读入你的应用程序到那个 var 中,那么我绝对不会推荐它。
Besides OS algorithms for memory management are extremely mature and fast, so it is ok.
此外,操作系统的内存管理算法非常成熟和快速,所以还可以。
How do you globally advise to use MongoDB for huge traffic?
您如何在全球范围内建议将 MongoDB 用于巨大的流量?
Hmm, this is such a huge question. Really I would recommend you Google a little in this subject but as the documentation states you need to ensure your working set fits into RAM for one.
嗯,这是一个很大的问题。真的,我会在这个主题上向您推荐一些 Google,但正如文档所述,您需要确保您的工作集适合 RAM。
Here is a good starting point: What does it mean to fit "working set" into RAM for MongoDB?
这是一个很好的起点:将“工作集”放入 MongoDB 的 RAM 中意味着什么?
回答by Curt
MongoDB attempts to keep entire collections in memory: it memory-maps each collection page. For everything to be in memory, both the data pages, and the indices that reference them, must be kept in memory.
MongoDB 尝试将整个集合保存在内存中:它对每个集合页面进行内存映射。为了使所有内容都在内存中,数据页和引用它们的索引都必须保存在内存中。
If MongoDB returns a record, you can rest assured that it is now in memory (whether it was beforeyour query or not).
如果 MongoDB 返回一条记录,您可以放心,它现在在内存中(无论是否在您查询之前)。
MongoDB doesn't keep a "cache" of records in the same way that, say, a web browser does. When you commit a change, both the memory and the disk are updated.
MongoDB 不会像 Web 浏览器那样保存记录的“缓存”。当您提交更改时,内存和磁盘都会更新。
Mongo is greatwhen matched to the appropriate use cases. It is veryhigh performance if you have sufficient server memory to cache everything, and declines rapidly past that point. Many, many high-volume websites use MongoDB: it's a good thing that memory is so cheap, now.
与适当的用例匹配时,Mongo很棒。这是非常高的性能,如果你有足够的服务器内存来缓存一切,并迅速下降过去的那个点。许多大量的网站使用 MongoDB:现在内存如此便宜,这是一件好事。