MongoDB - 解释特定的解释输出 [英] MongoDB - interpret particular explain output

查看:126
本文介绍了MongoDB - 解释特定的解释输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是MongoDB版本2.4.8。

I am using MongoDB version 2.4.8.

[test] 2014-03-25 14:42:13.0 >>> db.users.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "test.users",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "username" : 1,
                        "age" : 1
                },
                "ns" : "test.users",
                "name" : "username_1_age_1"
        },
        {
                "v" : 1,
                "key" : {
                        "age" : 1,
                        "username" : 1
                },
                "ns" : "test.users",
                "name" : "age_1_username_1"
        }
]
[test] 2014-03-25 14:44:36.550 >>>

[test] 2014-03-25 14:33:12.945 >>> db.users.find({"age" : 14, "username" : /.*/}).explain()
{
        "cursor" : "BtreeCursor age_1_username_1 multi",
        "isMultiKey" : false,
        "n" : 16850,
        "nscannedObjects" : 16850,
        "nscanned" : 16850,
        "nscannedObjectsAllPlans" : 16850,
        "nscannedAllPlans" : 16850,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 86,
        "indexBounds" : {
                "age" : [
                        [
                                14,
                                14
                        ]
                ],
                "username" : [
                        [
                                "",
                                {

                                }
                        ],
                        [
                                /.*/,
                                /.*/
                        ]
                ]
        },
        "server" : "server01:27017"
}

解释输出在用户名部分中的含义是什么?

What does the explain output mean in its username part?

            "username" : [
                    [
                            "",
                            {

                            }
                    ],
                    [
                            /.*/,
                            /.*/
                    ]
            ]

如果我正式和非正式地看待这部分,我很难理解这部分没有问题。

I have troubles understanding this part no
matter if I look at it formally and informally.

推荐答案

那里的输出特别适用于未绑定到字符串起始位置的正则表达式。因此对于将要扫描索引而不是集合的正则表达式(即使在这种情况下它将扫描整个索引),需要有一组起始边界和结束边界:

The output there is particular to a regex that is not bound to the starting position of the string. So for a regex that is going to scan the index and not the collection (even though it will scan the whole index in this case) there needs to be a set of starting bounds and ending bounds:

考虑使用不同正则表达式的第一个查询:

Consider the first query with a different regex:

db.collection.find({ "username": /bob/ }).explain()

    "indexBounds" : {
            "username" : [
                    [
                            "",
                            {

                            }
                    ],
                    [
                            /bob/,
                            /bob/
                    ]
            ]
    },

所以这意味着有一个搜索整个字符串,然后匹配将以包含bob作为字符串一部分的内容结束。所以第一部分是lexical匹配边界,第二部分是要应用的实际正则表达式:

So that signifies there is a the whole string to search through and then the match will end on something that contains "bob" as part of the string. So the first part is the "lexical" match bounds and the second part is the actual regex to be applied:

以下查询更清楚地显示了这一点:

The following query shows this more clearly:

db.collection.find({ username: /^bob/ }).explain()

    "indexBounds" : {
            "username" : [
                    [
                            "bob",
                            "boc"
                    ],
                    [
                            /^bob/,
                            /^bob/
                    ]
            ]
    },

由于它锚定在字符串的开头,因此需要测试的索引的唯一条目与bob和boc之间的词法匹配。正则表达式再次作为边界的第二部分包含。

Since that is anchored to the start of the string, the only entries of the index that need to be tested match "lexically" between "bob" and "boc". The regex is again contained as the second part of the bounds.

边界条目通常在内部被描述为两部分元素,正则表达式就是这种情况,在第一部分中有字符串边界,这对于匹配索引是有意义的,然后正则表达式适用于那些匹配的entires。

The bounds entries are generally described as "two part" elements internally, and there is this case for regular expressions, which in the first part has the string bounds which makes sense for matching the index, and then for the regex to apply to those matching entires.

作为最后的注释然后请参阅以下内容:

As a final note then see the following:

db.collection.find({ username: {$gt: ""} }).explain()

    "indexBounds" : {
            "username" : [
                    [
                            "",
                            {

                            }
                    ]
            ]
    },

这与您的初始查询本质上相同,即匹配任何字符串。

Which is intrinsically the same as your initial query, that says to match any string.

这篇关于MongoDB - 解释特定的解释输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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