3

What's the fastest method to sort by reverse insertion order on a capped-collection ('rf' has been sparse-indexed)

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" : {

}
}

seems like 'natural' bypass the use of the indexed ('rf') field, significantly slowing the query. Is this an expected expected behaviour? Shouldn't the 'natural' sort be computed after the find/index?

without the 'natural' sort:

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"
        ]
    ]
}

Hint does force the engine to use the 'rf' index but the result bypass the (reverse) 'natural' sort

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"
        ]
    ]
}
}

2 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like the query optimizer is doing the wrong thing when you add the sort.

Can you try adding .hint({rf :1}) to the query to see what happens?

3 Comments

Just updated the question with the hint-explain. It does force the engine to use the 'rf' index but the result bypass the (reverse) 'natural' sort.
Yes, with the same results (reverse natural sort being ignored). Ideally I would like to avoid 'hint' to keep the query as simple/intuitive as possible, as the 'find' query will be dynamic. Trying to understand if it's expected for a 'natural' sort to throw the index-usage out of the window.
Apparently there is no mechanism to use natural sort AND indexes. Pretty odd, but seems like there is no built-in method to accomplish a basic task like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.