gpt4 book ai didi

MongoDb geoIntersects 不使用 IndexOnly

转载 作者:可可西里 更新时间:2023-11-01 09:55:49 27 4
gpt4 key购买 nike

我有一个名为 search2 的集合,其中包含大约 20000 个这样的文档:

    {
"loc": {
"type": "Polygon",
"coordinates": [
[
[
43.78526674007639,
11.14739998758569
],
[
43.78526674007639,
11.183372851822439
],
[
43.79443488391605,
11.183372851822439
],
[
43.79443488391605,
11.264311796355125
],
[
43.812771171595415,
11.264311796355125
],
[
43.83110745927479,
11.264311796355125
],
[
43.83110745927479,
11.273305012414314
],
[
43.849443746954144,
11.273305012414314
],
[
43.858611890793824,
11.273305012414314
],
[
43.858611890793824,
11.264311796355125
],
[
43.8769481784732,
11.264311796355125
],
[
43.8769481784732,
11.246325364236752
],
[
43.88611632231286,
11.246325364236752
],
[
43.88611632231286,
11.237332148177565
],
[
43.895284466152546,
11.237332148177565
],
[
43.895284466152546,
11.228338932118376
],
[
43.904452609992234,
11.228338932118376
],
[
43.904452609992234,
11.165386419704065
],
[
43.895284466152546,
11.165386419704065
],
[
43.895284466152546,
11.156393203644878
],
[
43.88611632231286,
11.156393203644878
],
[
43.8769481784732,
11.156393203644878
],
[
43.858611890793824,
11.156393203644878
],
[
43.849443746954144,
11.156393203644878
],
[
43.849443746954144,
11.165386419704065
],
[
43.83110745927479,
11.165386419704065
],
[
43.83110745927479,
11.156393203644878
],
[
43.812771171595415,
11.156393203644878
],
[
43.812771171595415,
11.14739998758569
],
[
43.79443488391605,
11.14739998758569
],
[
43.78526674007639,
11.14739998758569
]
]
]
},
"docId": 1,
"docVote": 0,
"title": "title-1",
"_id": {
"$oid": "5248725d2dd5622510000001"
}
}

我用这个命令定义了一个索引:

    db.search2.ensureIndex({"loc":"2dsphere"});

集合中只有这个索引和“_id”字段的默认索引。

当我执行以下查询时,我希望解释中的 indexOnly 参数设置为 true:

    db.search2.find({
loc: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [
[
[43.7269795, 11.1540365],
[43.7269796, 11.1540365],
[43.7269796, 11.1540366],
[43.7269795, 11.1540366],
[43.7269795, 11.1540365]
]
]
}
}
}
}, {
loc: 1,
_id: 0
}).hint({"loc":"2dsphere"}).explain()

但这是结果:

    {
"cursor" : "S2Cursor",
"isMultiKey" : true,
"n" : 14,
"nscannedObjects" : 14,
"nscanned" : 186,
"nscannedObjectsAllPlans" : 14,
"nscannedAllPlans" : 186,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 20,
"indexBounds" : {

},
"nscanned" : 186,
"matchTested" : NumberLong(80),
"geoTested" : NumberLong(80),
"cellsInCover" : NumberLong(1),
"server" : "*******"
}

我注意到 isMultiKey 为真,由文档多边形语法引起。游标是 S2Cursor 所以我想索引被使用了。但是为什么 indexOnly 是假的呢?是由于多边形语法吗?所以不可能有 IndexOnly=true ?提前致谢

最佳答案

indexOnly 解释来自here状态:

indexOnly is a boolean value that returns true when the query is covered by the index indicated in the cursor field. When an index covers a query, MongoDB can both match the query conditions and return the results using only the index because:

  • all the fields in the query are part of that index, and

  • all the fields in the query are part of that index, and

来自here它指出:

An index cannot cover a 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 index and cannot support a covered query.
  • any of the indexed fields are fields in subdocuments.

希望对你有帮助

关于MongoDb geoIntersects 不使用 IndexOnly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19093490/

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