3

在上限集合上按反向插入顺序排序的最快方法是什么(“rf”已被稀疏索引)

db.log.find({ rf : 'o-5556457634'}).sort({ '$natural' : -1 }).explain();
{
"cursor" : "ReverseCappedCursor",
"nscanned" : 1654468,
"nscannedObjects" : 1654468,
"n" : 4,
"millis" : 2932,
"nYields" : 5,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {

}
}

似乎“自然”绕过了索引(“rf”)字段的使用,显着减慢了查询速度。这是预期的预期行为吗?不应该在查找/索引之后计算“自然”排序吗?

没有“自然”排序:

db.log.find({ rf : 'o-5556457634'}).explain();
{
"cursor" : "BtreeCursor rf_1",
"nscanned" : 4,
"nscannedObjects" : 4,
"n" : 4,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
    "rf" : [
        [
            "o-5556457634",
            "o-5556457634"
        ]
    ]
}

提示确实强制引擎使用“rf”索引,但结果绕过(反向)“自然”排序

db.log.find({ rf : 'o-5556457634'}).sort({ '$natural' : -1 }).hint({rf :1}).explain();
{
"cursor" : "BtreeCursor rf_1",
"nscanned" : 4,
"nscannedObjects" : 4,
"n" : 4,
"scanAndOrder" : true,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
    "rf" : [
        [
            "o-5556457634",
            "o-5556457634"
        ]
    ]
}
}
4

2 回答 2

2

Faced the same problem but found a solution. You can create index on the fields you're mentioning in find filter with addition of the "_id":-1 field and then use sort({"_id":-1}). Helped me.

于 2015-06-20T12:14:57.550 回答
1

当您添加sort.

您可以尝试添加.hint({rf :1})到查询中以查看会发生什么吗?

于 2012-04-20T02:32:26.720 回答