[SERVER-14712] Limit scans one extra Created: 29/Jul/14  Updated: 10/Dec/14  Resolved: 30/Jul/14

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.6.1, 2.6.3
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Sam Millman Assignee: David Storch
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Operating System: ALL
Steps To Reproduce:

Insert this set of test data:

> db.rooms.find()
{ "_id" : ObjectId("53ad206e1d8f2d8351182830"), "id" : 1, "from" : ISODate("2014-06-26T00:00:00Z"), "to" : ISODate("2014-06-28T00:00:00Z") }
{ "_id" : ObjectId("53ad276f1d8f2d8351182831"), "id" : 1, "from" : ISODate("2014-06-24T00:00:00Z"), "to" : ISODate("2014-07-01T00:00:00Z") }
{ "_id" : ObjectId("53ad28ad1d8f2d8351182832"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-06-28T00:00:00Z") }
{ "_id" : ObjectId("53ad28c61d8f2d8351182833"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-07-03T00:00:00Z") }
{ "_id" : ObjectId("53ad29971d8f2d8351182834"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-06-21T00:00:00Z") }

Create these indexes:

> db.rooms.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.rooms"
        },
        {
                "v" : 1,
                "key" : {
                        "id" : 1
                },
                "name" : "id_1",
                "ns" : "test.rooms"
        }
]

And then run this query:

> db.rooms.find({id:1}).sort({id:1}).limit(2).explain()
{
        "cursor" : "BtreeCursor id_1",
        "isMultiKey" : false,
        "n" : 2,
        "nscannedObjects" : 2,
        "nscanned" : 3,
        "nscannedObjectsAllPlans" : 2,
        "nscannedAllPlans" : 3,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "indexBounds" : {
                "id" : [
                        [
                                1,
                                1
                        ]
                ]
        },
        "server" : "ubuntu:27017",
        "filterSet" : false
}

with or without the sort will result in the same answer.

Participants:

 Description   

I have filed this under indexing since it only seems to happen when an index is used.

I noticed that the nscanned on 2.6.1 for limit is always one extra than needed.



 Comments   
Comment by David Storch [ 30/Jul/14 ]

Resolving as Works as Designed. For an explanation of why, please see SERVER-14183 (a similar bug report resolved with Works as Designed).

Thanks for reporting this, and please do reach out with any further bug reports or questions!

Generated at Thu Feb 08 03:35:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.