我有一个索引相对较大的集合(但小于可用的 ram),并查看这个集合中 find 的性能以及 htop 给出的我的系统中的可用 ram 数量,似乎 mongo 没有将完整索引存储在内存。有没有办法强制 mongo 将此特定索引存储在 ram 中?
查询示例:
> db.barrels.find({"tags":{"$all": ["avi"]}}).explain()
{
"cursor" : "BtreeCursor tags_1",
"nscanned" : 300393,
"nscannedObjects" : 300393,
"n" : 300393,
"millis" : 55299,
"indexBounds" : {
"tags" : [
[
"avi",
"avi"
]
]
}
}
并非所有对象都被标记为“avi”标签:
> db.barrels.find().explain()
{
"cursor" : "BasicCursor",
"nscanned" : 823299,
"nscannedObjects" : 823299,
"n" : 823299,
"millis" : 46270,
"indexBounds" : {
}
}
没有“$all”:
db.barrels.find({"tags": ["avi"]}).explain()
{
"cursor" : "BtreeCursor tags_1 multi",
"nscanned" : 300393,
"nscannedObjects" : 300393,
"n" : 0,
"millis" : 43440,
"indexBounds" : {
"tags" : [
[
"avi",
"avi"
],
[
[
"avi"
],
[
"avi"
]
]
]
}
}
当我搜索两个或更多标签时也会发生这种情况(它会扫描每个项目,就好像没有索引一样):
> db.barrels.find({"tags":{"$all": ["avi","mp3"]}}).explain()
{
"cursor" : "BtreeCursor tags_1",
"nscanned" : 300393,
"nscannedObjects" : 300393,
"n" : 6427,
"millis" : 53774,
"indexBounds" : {
"tags" : [
[
"avi",
"avi"
]
]
}
}
最佳答案
没有。 MongoDB 允许系统管理存储在 RAM 中的内容。
话虽如此,您应该能够通过定期对索引运行查询(查看 query hinting)将索引保留在 RAM 中,以防止它们过时。
有用的引用资料:
此外,Kristina Chodorow 还提供了 excellent answer regarding the relationship between MongoDB Indexes and RAM
更新:
在提供 .explain() 输出的更新后,我看到以下内容:
- 查询正在命中索引。
- nscanned 是检查的项目(文档或索引条目)的数量。
- nscannedObjects 是扫描的文档数
- n 是符合指定条件的文档数
- 您的数据集是 300393 个条目,即索引中的项目总数以及匹配结果。
我可能读错了,但我读到的是您收藏中的所有项目都是有效结果。在不知道您的数据的情况下,似乎每个项目都包含标签“avi”。这意味着的另一件事是这个索引几乎没用。索引在尽可能缩小结果字段时提供最大的值(value)。
来自 MongoDB 的“Indexing Advice and FAQ”页面:
Understanding explain's output. There are three main fields to look for when examining the explain command's output:
- cursor: the value for cursor can be either BasicCursor or BtreeCursor. The second of these indicates that the given query is using an index.
- nscanned: he number of documents scanned.
- n: the number of documents returned by the query. You want the value of n to be close to the value of nscanned. What you want to avoid is doing a collection scan, that is, where every document in the collection is accessed. This is the case when nscanned is equal to the number of documents in the collection.
- millis: the number of milliseconds require to complete the query. This value is useful for comparing indexing strategies, indexed vs. non-indexed queries, etc.
关于mongodb - 有没有办法强制 mongodb 在 ram 中存储某些索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9912018/