Skip to content

Use index for $in queries #198

Open
Open
@zorkian

Description

@zorkian

It's possible I'm doing something wrong, but it appears that indexes are not used when performing $in queries. Here's an example Mongo query:

>>> import pymongo
>>> db = pymongo.MongoClient('mongodb://10.101.0.44:27016/')['discord']
>>> db.users.find({ '_id': { '$in': [ 3187217, 4248493 ] } })

On the backend trace, this is emitted:

<Event Severity="10" Time="1562606405.236166" Type="SlowQuery" ID="0000000000000000"
  Database="discord" Collection="users" Query="{ _id: { $in: [ 3187217, 4248493 ] } }"
  Plan="{ type: &quot;filter&quot;,
          source_plan: { type: &quot;table scan&quot; },
          filter: &quot;ANY(ExtPath(_id) matching OR(EQUALS('3187217'), EQUALS('4248493')))&quot; }"
  Machine="0.0.0.0:27016" LogGroup="default" />

I verified, just to be sure, that the index exists:

>>> import pprint
>>> pprint.pprint(db.users.index_information())
{u'_id_': {u'key': [(u'_id', 1)],
           u'metadata version': 1,
           u'ns': u'discord.users',
           u'status': u'ready',
           u'unique': True}}

My expectation in this case would be that the document layer would do two indexed lookups and return the results, and it shouldn't need to do a table scan to accomplish this? I read through the differences and caveats but don't see this documented anywhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions