gpt4 book ai didi

mongodb - 无法获取覆盖查询以仅在 mongodb 中使用索引

转载 作者:IT老高 更新时间:2023-10-28 13:34:06 26 4
gpt4 key购买 nike

我正在尝试使用覆盖索引在我的使用 mongodb 的应用上实现词干文本搜索。

我设置了以下索引:

ensureIndex({st: 1, n: 1, _id: 1});

但是当我对查询运行 explain() 时,无论我做什么,我都无法让 indexOnly 读取为真。

db.merchants.find({st: "Blue"}, {n:1,_id:1}).explain()
{
"cursor" : "BtreeCursor st_1_n_1__id_1",
"nscanned" : 8,
"nscannedObjects" : 8,
"n" : 8,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : true,
"indexOnly" : false,
"indexBounds" : {
"st" : [
[
"Blue",
"Blue"
]
],
"n" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"_id" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}

我已经发现索引中键的顺序很重要。例如,如果我使用 {_id, n:1, st:1} 它根本没有使用这个索引来执行查询。我还在某处读到,太少的文档可能会使用 explain() 触发不可预测的行为,因为多种策略同样快。但是在这种情况下,我看到它使用了正确的索引,但它不仅仅使用了索引。这是怎么回事?

我正在使用 mongoid,我相信 mongo 2.0.8。

更新:

改用 Mongoid v3.1.4 和 mongod v2.2

这是 mongod 从 mongoid 看到的查询: Mon Jul 15 10:47:26 [conn14] runQuery called spl_development.merchants { $query: { st: { $regex: "cr", $options: "i "} }, $explain: true } Mon Jul 15 10:47:26 [conn14] query spl_development.merchants query: { $query: { st: { $regex: "cr", $options: "i"} }, $explain: true } ntoreturn:0 keyUpdates:0 locks(micros) r:212 nreturned:1 reslen:393 0ms

所以投影不会被发送到 mongod 层,而只是在应用层处理它。不理想!

这已被认为是 mongoid 中的一个错误,可以在此处进行跟踪: https://github.com/mongoid/mongoid/issues/3142

最佳答案

我希望您的查询不能使用覆盖索引,因为您有一个包含在索引中的数组的字段。这在解释中建议使用 "isMultiKey": true

如文档中所述(Create Indexes that Support Covered Queries):

MongoDB cannot use a covered query if any of the indexed fields in any of the documents in the collection includes an array. If an indexed field is an array, the index becomes a multi-key index and cannot support a covered query.

关于mongodb - 无法获取覆盖查询以仅在 mongodb 中使用索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14190637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com