Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-14712

Limit scans one extra

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.6.1, 2.6.3
    • Component/s: Querying
    • None
    • ALL
    • Hide

      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.

      Show
      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.
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None

      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.

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            sammaye Sam Millman
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: