[SERVER-4317] index not used when argument to $in is empty array Created: 18/Nov/11  Updated: 07/Mar/14  Resolved: 18/Nov/11

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.0.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Daniel Crosta Assignee: Aaron Staple
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-4325 BasicCursor may be reported by explai... Closed
is related to SERVER-4327 allow dummy table scans in no table s... Closed
Participants:

 Description   

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.



 Comments   
Comment by Daniel Crosta [ 18/Nov/11 ]

Originally reported on mongoengine-dev: https://groups.google.com/group/mongoengine-dev/browse_thread/thread/1337847b1a31840a

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