pymongo,count查询比mongo shell慢

时间:2015-07-08 07:53:25

标签: python mongodb pymongo

大家。我在pymongo遇到了麻烦。

  • pymongo 3.0.3
  • MongoDB 2.6.10(在localhost上)

以下查询在mongo shell上运行,我得到了3秒的结果。

$ mongo mydb
> db.products.count({'categories': { '$elemMatch': {'code': /^11/}}})
891115

但是,通过pymongo的python脚本,我花了30秒才得到结果。

$ python
Python 3.4.3 (default, Jun 26 2015, 04:26:33)

>>> import pymongo
>>> import re
>>> 
>>> cli = pymongo.MongoClient()
>>> coll = cli.mydb.products
>>> 
>>> coll.count({'categories': { '$elemMatch': {'code': re.compile(r'^11')}}})
891115

(参考)

$ mongo mydb
> db.products.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "mydb.products"
        },
        {
                "v" : 1,
                "key" : {
                        "brand" : 1
                },
                "name" : "brand_1",
                "ns" : "mydb.products"
        },
        {
                "v" : 1,
                "key" : {
                        "categories.code" : 1
                },
                "name" : "categories.code_1",
                "ns" : "mydb.products"
        },
        {
                "v" : 1,
                "key" : {
                        "categories.code" : 1,
                        "brand" : 1
                },
                "name" : "categories.code_1_brand_1",
                "ns" : "mydb.products"
        }
]

> db.products.find({categories: {$elemMatch: {code: /^11/}}}).explain()
{
        "cursor" : "BtreeCursor categories.code_1",
        "isMultiKey" : true,
        "n" : 891115,
        "nscannedObjects" : 891115,
        "nscanned" : 891116,
        "nscannedObjectsAllPlans" : 891216,
        "nscannedAllPlans" : 891217,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 6962,
        "nChunkSkips" : 0,
        "millis" : 2764,
        "indexBounds" : {
                "categories.code" : [
                        [
                                "11",
                                "12"
                        ],
                        [
                                /^11/,
                                /^11/
                        ]
                ]
        },
        "server" : "ip-10-4-0-124:27017",
        "filterSet" : false
}

有人可以给我一些建议吗,为什么当我用python脚本运行它时会变慢?

1 个答案:

答案 0 :(得分:1)

查询错了。 在前缀搜索中,使用' re' Python的模块而不是' $ regex' pymongo。

# wrong. MongoDB does not use index.
coll.count({'categories': { '$elemMatch': {'code': re.compile(r'^11')}}})

# correct. MongoDB use index.
coll.count({'categories': { '$elemMatch': {'code': { '$regex' : '^11' }}}})

谢谢!