3

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
}
4

1 回答 1

0

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

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

如果您有一个稀疏索引,则该索引将不包含没有索引键的对象的任何条目。

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

于 2015-08-19T09:52:54.617 回答