Pymongo significantly slower than mongo shell?

2019-05-10 03:39发布

问题:

I'm relatively new to mongodb, and having a performance problem in pymongo. I have a collection that's 50 GBs (uncompressed) 20 GBs (compressed via WiredTiger) with about 39 million documents. Querying it over indexed fields gives a result that's about 125,000 documents and 150 MBs uncompressed. When I do the following in the mongo shell, it takes about a second.

var result = db.my_collection.find(my_query).toArray()

However, when I do the same thing in pymongo, it takes over 7 seconds.

db = pymongo.MongoClient()['my_db']
result = list(db['my_collection'].find(my_query)) 

Some extra info:

  • I'm using Ubuntu 14.04, python 2.7.6, pymongo 3.2, and mongo 3.2.

  • I think my pymongo is configured to use C because I installed python-dev and both pymongo.has_c() and bson.has_c() show True.

  • Everything is run locally.

I find it hard to believe that pymongo is 7x slower than the mongo shell. What am I missing?