有没有办法强制mongodb在ram中存储某些索引? [英] Is there a way to force mongodb to store certain index in ram?

查看:196
本文介绍了有没有办法强制mongodb在ram中存储某些索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有相对较大索引的集合(但可用ram少)并且查看此集合上的find的性能以及我的系统中由htop给出的免费ram的数量似乎mongo没有存储完整的索引内存。有没有办法强制mongo在ram中存储这个特定的索引?

I have a collection with a relatively big index (but less than ram available) and looking at performance of find on this collection and amount of free ram in my system given by htop it's seems that mongo is not storing full index in the ram. Is there a way to force mongo to store this particular index in the 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标签:

Not the all objects are tagged with "avi" tag:

> 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"
                                ]
                        ]
                ]
        }
}

当我搜索两个或更多标签时,它会发生这种情况(它会扫描每个项目,好像没有索引):

Also this happens when I search for two or more tags (it scans every item as if were no index):

> 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中的内容。

No. MongoDB allows the system to manage what is stored in RAM.

话虽如此,您应该能够通过对索引运行查询来将索引保留在RAM中(请查看定期查询提示)以防止它们过时。

With that said, you should be able to keep the index in RAM by running queries against the indexes (check out query hinting) periodically to keep them from getting stale.

有用的参考文献:

索引建议和常见问题解答

此外,Kristina Chodorow提供了关于这种关系的出色答案在MongoDB索引和RAM之间

Additionally, Kristina Chodorow provides this excellent answer regarding the relationship between MongoDB Indexes and RAM

更新:

之后他更新提供.explain()输出,我看到以下内容:

After the update providing the .explain() output, I see the following:


  • 查询正在点击索引。

  • nscanned是检查的项目数(文档或索引条目)。

  • nscannedObjects是扫描的文档数

  • n是与指定条件匹配的文档数

  • 您的数据集为300393个条目,即索引中的项目总数以及匹配结果。

  • The query is hitting the index.
  • nscanned is the number of items (docs or index entries) examined.
  • nscannedObjects is the number of docs scanned
  • n is the number of docs that match the specified criteria
  • your dataset is 300393 entries, which is the total number of items in the index, and the matching results.

我可能读错了,但我读到的是你收藏的所有物品都是有效的结果。在不知道您的数据的情况下,似乎每个项目都包含标签avi。这意味着另一件事就是这个指数几乎没用;当索引尽可能地缩小结果字段时,它们提供了最大的价值。

I may be reading this wrong, but what I'm reading is that all of the items in your collection are valid results. Without knowing your data, it would seem that every item contains the tag "avi". The other thing that this means is that this index is almost useless; indexes provide the most value when they work to narrow the resultant field as much as possible.

来自MongoDB的索引建议和常见问题解答页面:

From MongoDB's "Indexing Advice and FAQ" page:


了解explain的输出。在检查explain命令的输出时,有三个主要字段可以查看

Understanding explain's output. There are three main fields to look for when examining the explain command's output:


  • cursor:游标的值可以是BasicCursor或BtreeCursor。
    其中第二个表示给定查询正在使用索引。

  • nscanned:扫描的文档数。

  • n:查询返回的文档数
    。您希望n的值接近nscanned的
    值。您要避免的是进行集合扫描,
    即访问集合中的每个文档。当nscanned等于
    集合中的文档数时,这是

  • millis:完成
    所需的毫秒数查询。此值对于比较索引策略,索引
    与非索引查询等非常有用。

  • 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在ram中存储某些索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆