Mongo 2.4.1,带有仲裁者的2成员副本
我认为这是一个非常简单的查询,返回不一致的结果:
objects = db.collection.find({'field.id': my_id, 'field1': None, 'field2': 'value'})
我收到77个对象,但集合中有248个与该查询匹配的对象。我怀疑查询优化器正在做一些我不期望的事情。收集中的所有索引都不稀疏。
在上述查询的.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
}
答案 0 :(得分:0)
可能是因为您有稀疏索引。
n
=符合查询选择条件的文档数nscanned
=数据库操作期间扫描的文档或索引条目数如果您有https://gist.github.com/mauritsvanrees/99cb4a25b622479e7dc3,则索引将不包含没有索引键的对象的任何条目。
从查询的外观来看,mongodb确实正在使用field1
的索引,而您的查询正在搜索该字段的null
值。因此,请检查您的索引是否稀疏 - 这很可能是原因。