gpt4 book ai didi

MongoDB 的结果与解释中的 n > nscanned 不一致

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

Mongo 2.4.1,带仲裁器的 2 成员副本集

我认为这是一个返回不一致结果的非常简单的查询:

objects = db.collection.find({'field.id': my_id, 'field1': None, 'field2': 'value'})

我得到了 77 个对象,但集合中有 248 个对象与该查询匹配。我怀疑查询优化器正在做一些我不期望的事情。 collection 上的索引都不是稀疏的。

在上述查询的 .explain() 中,我得到

{cursor: 'BtreeCursor field.id_1', n: 283, nscanned: 77}

为什么nscanned小于283?

我还应该注意到,虽然 77 长度的结果非常一致,但有时完全相同的查询会返回包含 238 个文档的结果。

编辑:完整的 .explain() 包含在下面。我注意到它看着 BtreeCursor field.id_1两次。这正常吗?

{
"nYields": 0,
"nscannedAllPlans": 1011,
"allPlans": [
{
"cursor": "BtreeCursor field2_1",
"indexBounds": {
"field2": [
["moment", "moment"]
]
},
"nscannedObjects": 78,
"nscanned": 78,
"n": 0
},
{
"cursor": "BtreeCursor field.id_1_field3_1_field2_1_field1_1_created_at_-1",
"indexBounds": {
"field1": [
[null, null]
],
"created_at": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
],
"field3": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
],
"field2": [
["moment", "moment"]
],
"field.id": [
["oid-50eba1a2256b4738d0e5254d", "oid-50eba1a2256b4738d0e5254d"]
]
},
"nscannedObjects": 78,
"nscanned": 78,
"n": 78
},
{
"cursor": "BtreeCursor field1_1_field3_1_field4_1_field5_-1_field2_1_created_at_-1",
"indexBounds": {
"field4": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
],
"field3": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
],
"field1": [
[null, null]
],
"created_at": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
],
"field5": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
],
"field2": [
["moment", "moment"]
]
},
"nscannedObjects": 78,
"nscanned": 78,
"n": 0
},
{
"cursor": "BtreeCursor field1_1_field3_1_field4_1_field2_1_created_at_-1",
"indexBounds": {
"field1": [
[null, null]
],
"created_at": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
],
"field4": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
],
"field2": [
["moment", "moment"]
],
"field3": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
]
},
"nscannedObjects": 78,
"nscanned": 78,
"n": 0
},
{
"cursor": "BtreeCursor field1_-1_created_at_-1",
"indexBounds": {
"field1": [
[null, null]
],
"created_at": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
]
},
"nscannedObjects": 78,
"nscanned": 78,
"n": 0
},
{
"cursor": "BtreeCursor field.id_1_field3_1_field1_1_created_at_-1",
"indexBounds": {
"field1": [
[null, null]
],
"created_at": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
],
"field3": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
],
"field.id": [
["oid-50eba1a2256b4738d0e5254d", "oid-50eba1a2256b4738d0e5254d"]
]
},
"nscannedObjects": 78,
"nscanned": 78,
"n": 78
},
{
"cursor": "BtreeCursor field.id_1_field6_1_field2_1",
"indexBounds": {
"field6": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
],
"field2": [
["moment", "moment"]
],
"field.id": [
["oid-50eba1a2256b4738d0e5254d", "oid-50eba1a2256b4738d0e5254d"]
]
},
"nscannedObjects": 76,
"nscanned": 77,
"n": 76
},
{
"cursor": "BtreeCursor field.id_1_field7_1",
"indexBounds": {
"field7": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
],
"field.id": [
["oid-50eba1a2256b4738d0e5254d", "oid-50eba1a2256b4738d0e5254d"]
]
},
"nscannedObjects": 78,
"nscanned": 78,
"n": 71
},
{
"cursor": "BtreeCursor field.id_1",
"indexBounds": {
"field.id": [
["oid-50eba1a2256b4738d0e5254d", "oid-50eba1a2256b4738d0e5254d"]
]
},
"nscannedObjects": 78,
"nscanned": 78,
"n": 77
},
{
"cursor": "BtreeCursor field.id_1",
"indexBounds": {
"field.id": [
["oid-50eba1a2256b4738d0e5254d", "oid-50eba1a2256b4738d0e5254d"]
]
},
"nscannedObjects": 77,
"nscanned": 77,
"n": 72
},
{
"cursor": "BasicCursor",
"indexBounds": {},
"nscannedObjects": 77,
"nscanned": 77,
"n": 0
}
],
"millis": 14,
"nChunkSkips": 0,
"server": "c3:10003",
"n": 283,
"cursor": "BtreeCursor field.id_1",
"oldPlan": {
"cursor": "BtreeCursor field.id_1",
"indexBounds": {
"field.id": [
["oid-50eba1a2256b4738d0e5254d", "oid-50eba1a2256b4738d0e5254d"]
]
}
},
"scanAndOrder": false,
"indexBounds": {
"field.id": [
["oid-50eba1a2256b4738d0e5254d", "oid-50eba1a2256b4738d0e5254d"]
]
},
"nscannedObjectsAllPlans": 1000,
"isMultiKey": false,
"indexOnly": false,
"nscanned": 77,
"nscannedObjects": 77
}

最佳答案

这可能是因为您的索引稀疏

  • n = 符合查询选择条件的文档数
  • nscanned = 在数据库操作期间扫描的文档或索引条目数

如果你有一个 sparse index ,索引将不包含没有索引键的对象的任何条目。

从查询的外观来看,mongodb 确实在使用 field1 的索引,并且您的查询正在搜索该字段的 null 值。因此,请检查您的索引是否稀疏 - 这很可能是原因。

关于MongoDB 的结果与解释中的 n > nscanned 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449181/

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