0

我将 mongodb 用于我们的 Application 。我通过将 system.profiling 级别设置为 2 来使用 mongodb 分析器,完成所有应用程序操作,从 system.profile 导出所有记录,最后根据分析器结果在集合上设置索引。

现在,当我确实解释了这些查询时

查询 1。

db.stocks.find({ symbol: "GOOG", date: "2013-09-13",type: "O",Mini: false, rootsymbol: "GOOG" }).sort( { "price": 1,"call":1} ).explain();
{
        "cursor" : "BtreeCursor symbol_1_date_1_type_1_Mini_1_rootsymbol_1_price_1",
        "nscanned" : 80,
        "nscannedObjects" : 80,
        "n" : 80,
        "scanAndOrder" : true,
        "millis" : 2,

查询 2。

 db.stocks.find({ symbol: "NVO" }).explain()
{
        "cursor" : "BtreeCursor symbol_1",
        "nscanned" : 1,
        "nscannedObjects" : 1,
        "n" : 1,
        "millis" : 0,
        "indexBounds" : {
                "symbol" : [
                        [
                                "NVO",
                                "NVO"
                        ]
                ]
        }
}

我对结果感到困惑nscannednscannedObjects并且n在我所有的查询中总是相等的。

如果 nscanned 、 nscannedObjects 和 n 值具有相等的值,请告诉我是否存在问题?

请让我知道我是否遗漏了什么或者它是否有问题?

4

2 回答 2

2

它们都相等的事实意味着您有良好的、未发现的索引使用情况。

我将分解结果:

"nscanned" : 80,

这是索引中扫描的数字

"nscannedObjects" : 80,

这是扫描索引后加载的文档数量,如果这个数字高于扫描的,那么你可能有错误的索引使用(取决于场景)。

时间nscannedObjects可能低于 nscanned 在覆盖索引上或(http://docs.mongodb.org/manual/reference/method/cursor.explain/#explain.nscannedObjects):

在具有重复文档的数组字段上的多键索引的情况下。

"n" : 80,

这是退回的金额。

但是,这并不意味着您对排序有一个好的结果,如第一个解释所示:

"scanAndOrder" : true,
于 2013-09-13T14:35:07.523 回答
1

Your first query is good - it is using the index to find the 80 matches that exist. "n" is the number of documents returned; "nscannedRecords" is the number it needed to scan during the query (it could be less than 'scanned' if you have a covered query). "nscanned" is the number of index entries scanned (if an index could be used). Sorting 80 records should be fairly quick; you don't have an index that you could use for it (from what you've shown us).

The second query is also great - it uses an index, and finds the one document that matches.

For more details on explain(), see http://docs.mongodb.org/manual/reference/method/cursor.explain/#explain-output-fields-core.

于 2013-09-13T14:42:56.590 回答