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

index not used when argument to $in is empty array

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.0.1
    • Component/s: Querying
    • None

      A query with {field: {$in: []}} does not use an index, even if index does not touch the field being $in'd:

      test@dcrosta> db.foo.find()
      { "_id" : ObjectId("4ec67287f4c2536f2b40c43a"), "i" : 0, "len" : 0, "arr" : [ ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c43b"), "i" : 1, "len" : 1, "arr" : [ 0 ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c43c"), "i" : 2, "len" : 2, "arr" : [ 0, 1 ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c43d"), "i" : 3, "len" : 0, "arr" : [ ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c43e"), "i" : 4, "len" : 1, "arr" : [ 0 ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c43f"), "i" : 5, "len" : 2, "arr" : [ 0, 1 ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c440"), "i" : 6, "len" : 0, "arr" : [ ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c441"), "i" : 7, "len" : 1, "arr" : [ 0 ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c442"), "i" : 8, "len" : 2, "arr" : [ 0, 1 ] }
      { "_id" : ObjectId("4ec67287f4c2536f2b40c443"), "i" : 9, "len" : 0, "arr" : [ ] }
      test@dcrosta> db.foo.createIndex({len: 1})
      test@dcrosta> db.foo.find({arr: {$in: [1]}, len: 2}).explain()
      {
        "cursor" : "BtreeCursor len_1",
        "nscanned" : 3,
        "nscannedObjects" : 3,
        "n" : 3,
        "millis" : 0,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "isMultiKey" : false,
        "indexOnly" : false,
        "indexBounds" : {
          "len" : [
            [
              2,
              2
            ]
          ]
        }
      }
      test@dcrosta> db.foo.find({arr: {$in: []}, len: 2}).explain()
      {
        "cursor" : "BasicCursor",
        "nscanned" : 0,
        "nscannedObjects" : 0,
        "n" : 0,
        "millis" : 0,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "isMultiKey" : false,
        "indexOnly" : false,
        "indexBounds" : {
          
        }
      }
      

      This is especially problematic if you have notablescan set to true.

            Assignee:
            aaron Aaron Staple
            Reporter:
            dcrosta Daniel Crosta
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: