MongoDB $query 运算符忽略索引?

2023-11-22

我创建了一个测试 MongoDB 集合“样本集合”,其文档如下所示:

 "_id" : ObjectId("510929e041cb2179b41ace1c"),
 "stringField" : "Random string0",
 "longField" : NumberLong(886)

并在字段“stringField”上有索引。 当我执行时

db.sampleCollection.find({"stringField":"Random string0"}).explain()

一切都好:

 "cursor" : "BtreeCursor stringField_1",
 "isMultiKey" : false,
 "n" : 2,
 "nscannedObjects" : 2,
 "nscanned" : 2,
 "nscannedObjectsAllPlans" : 2,
 "nscannedAllPlans" : 2,
 "scanAndOrder" : false,
 "indexOnly" : false,
 "nYields" : 0,
 "nChunkSkips" : 0,
 "millis" : 0,
 "indexBounds" : {
         "stringField" : [
                 [
                         "Random string0",
                         "Random string0"
                 ]
         ]
 }

but

db.sampleCollection.find({$query:{"stringField":"Random string0"}}).explain()

gets me

"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 4,
"nscanned" : 4,
"nscannedObjectsAllPlans" : 4,
"nscannedAllPlans" : 4,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {

}

这看起来不是问题,但我正在使用org.springframework.data.mongodb查询结构的生产和使用中的框架是编写存储库的唯一方法。 因此我有一个完全忽略索引数据的数据库。

这是对的吗?或者我误解了什么?


这很有趣,我无法决定是否说这是一个错误,这取决于你:

有两种可用的语法:http://docs.mongodb.org/manual/reference/operator/query/

当您使用:

db.collection.find( { age : 25 } )

也会

db.collection.find( { age : 25 } ).explain()
db.collection.find( { age : 25 } ).hint(someindex)

工作正常。

当您使用解决方案时(其他语法):

db.collection.find( { $query: { age : 25 } } )

的输出

db.sampleCollection.find({$query:{"stringField":"Random string0"}}).explain()

将显示类似不使用索引的查询

如果您还使用 .hint 作为索引,它将忽略结果。 :)(那是我不太明白)

幸运的是,这些操作还有另一种语法:您可以使用:

db.sampleCollection.find({$query:{"stringField":"Random string0"}, $explain:1})

它将有正确的输出并向我展示索引的用法。 $hint 也有类似的语法。

您可以在此处查看文档:http://docs.mongodb.org/manual/reference/meta-query-operators/

我发现这真的很有趣,所以我打开了分析器:

我制作了一个测试集合(queryTst),其中包含大约 250k 文档,每个文档只有 _id 和结构中的年龄字段,并带有年龄索引。

对于此查询:

db.queryTst.find({$query:{"age":16},$explain:1})

i got:

{
    "cursor" : "BtreeCursor age_1",
    "isMultiKey" : false,
    "n" : 2,
    "nscannedObjects" : 2,
    "nscanned" : 2,
    "nscannedObjectsAllPlans" : 2,
    "nscannedAllPlans" : 2,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "age" : [
            [
                16,
                16
            ]
        ]
    },
    "allPlans" : [
        {
            "cursor" : "BtreeCursor age_1",
            "n" : 2,
            "nscannedObjects" : 2,
            "nscanned" : 2,
            "indexBounds" : {
                "age" : [
                    [
                        16,
                        16
                    ]
                ]
            }
        }
    ],
    "oldPlan" : {
        "cursor" : "BtreeCursor age_1",
        "indexBounds" : {
            "age" : [
                [
                    16,
                    16
                ]
            ]
        }
    },
    "server" : ""
}

为了这:

 db.queryTst.find({$query:{"age":16},$explain:1}).explain()

i got:

"cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 0,
    "nscannedObjects" : 250011,
    "nscanned" : 250011,
    "nscannedObjectsAllPlans" : 250011,
    "nscannedAllPlans" : 250011,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 103,
    "indexBounds" : {

    },

在探查器日志中:对于第一个

{
    "ts" : ISODate("2013-01-30T20:35:40.526Z"),
    "op" : "query",
    "ns" : "test.queryTst",
    "query" : {
        "$query" : {
            "age" : 16
        },
        "$explain" : 1
    },
    "ntoreturn" : 0,
    "ntoskip" : 0,
    "nscanned" : 2,
    "keyUpdates" : 0,
    "numYield" : 0,
    "lockStats" : {
        "timeLockedMicros" : {
            "r" : NumberLong(368),
            "w" : NumberLong(0)
        },
        "timeAcquiringMicros" : {
            "r" : NumberLong(8),
            "w" : NumberLong(5)
        }
    },
    "nreturned" : 1,
    "responseLength" : 567,
    "millis" : 0,
    "client" : "127.0.0.1",
    "user" : ""
}

对于第二个:

{
    "ts" : ISODate("2013-01-30T20:35:47.715Z"),
    "op" : "query",
    "ns" : "test.queryTst",
    "query" : {
        "query" : {
            "$query" : {
                "age" : 16
            },
            "$explain" : 1
        },
        "$explain" : true
    },
    "ntoreturn" : 0,
    "ntoskip" : 0,
    "nscanned" : 250011,
    "keyUpdates" : 0,
    "numYield" : 0,
    "lockStats" : {
        "timeLockedMicros" : {
            "r" : NumberLong(104092),
            "w" : NumberLong(0)
        },
        "timeAcquiringMicros" : {
            "r" : NumberLong(13),
            "w" : NumberLong(5)
        }
    },
    "nreturned" : 1,
    "responseLength" : 373,
    "millis" : 104,
    "client" : "127.0.0.1",
    "user" : ""
}

这在某种程度上对我来说意味着解释()导致混合语法中的表扫描。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MongoDB $query 运算符忽略索引? 的相关文章

随机推荐