Index not used in Multikeys with $in operator

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: 0.9.5
    • Component/s: Index Maintenance
    • None
    • Environment:
      Ubuntu Server 9.04, 768 RAM, fresh installation of MongoDB 0.9.5, pymongo connector
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      I create simple test similar described in docs (http://www.mongodb.org/display/DOCS/Multikeys) for problem demonstration:
      My comment added in output with "<<<===" construction.
      -------------------
      from pymongo.connection import Connection
      from pprint import pprint

      connection = Connection("localhost", 27017)
      db = connection.test

      db.create_collection("example")

      1. example list is [[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11],[10,11,12,13,14]...]
        samples = [range(i, i+5) for i in xrange(1, 10000, 3)]

      for item in samples:
      db.example.save(

      {"test":item}

      )

      db.example.ensure_index("test",1)
      query = {"test":{"$in":[1,100,9000]}}

      print "Result:"
      pprint(list(db.example.find(query)))

      print "\nQuery Explain:"
      explain = db.example.find(query).explain()
      pprint(dict(explain.items()))
      ----------------------------

      After run it, I receive explain output:
      ...<skipped>...
      {u'allPlans': [{u'cursor': u'BtreeCursor test_1',
      u'endKey':

      {u'test': 9000}

      ,
      u'startKey': {u'test': 1}}],
      u'cursor': u'BtreeCursor test_1',
      u'endKey':

      {u'test': 9000}

      ,
      u'millis': 25,
      u'n': 4,
      u'nscanned': 14998.0, <<<====== Why 'nscanned' so large?! With index!
      u'oldPlan': {u'cursor': u'BtreeCursor test_1',
      u'endKey':

      {u'test': 9000}

      ,
      u'startKey': {u'test': 1}},
      u'startKey': {u'test': 1}}
      -------------

      When I try to get records without IN

      explain = db.example.find(

      {"test":9000}

      ).explain()
      pprint(dict(explain.items()))

      result look as:

      {u'allPlans': [{u'cursor': u'BtreeCursor test_1',
      u'endKey':

      {u'test': 9000}

      ,
      u'startKey': {u'test': 9000}}],
      u'cursor': u'BtreeCursor test_1',
      u'endKey':

      {u'test': 9000}

      ,
      u'millis': 0, <<<========== very fast
      u'n': 1,
      u'nscanned': 1.0,
      u'oldPlan': {u'cursor': u'BtreeCursor test_1',
      u'endKey':

      {u'test': 9000}

      ,
      u'startKey': {u'test': 9000}},
      u'startKey': {u'test': 9000}}

      It's a normal behavior?

            Assignee:
            Eliot Horowitz (Inactive)
            Reporter:
            Kirill Mavreshko
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: