Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用.count进行分页时,是否有办法优化我的性能?_Python_Mongodb_Mongodb Query_Pymongo - Fatal编程技术网

Python 使用.count进行分页时,是否有办法优化我的性能?

Python 使用.count进行分页时,是否有办法优化我的性能?,python,mongodb,mongodb-query,pymongo,Python,Mongodb,Mongodb Query,Pymongo,我需要执行分页,其中我选择基于用户的供应商和产品,以便在vulnerable\u product或vulnerable\u configuration的字段上进行正则化,但是我的查询cves=colCVE.find({cpe\u searchField:{“$regex”:cpe\u regex,$options:'I'})。排序(“修改的”,-1)。限制(限制)。跳过).allow_disk_use(True)需要大约的执行时间:0:00:00.407544,但此时要使用。count执行完整的

我需要执行分页,其中我选择基于用户的供应商和产品,以便在
vulnerable\u product
vulnerable\u configuration
的字段上进行正则化,但是我的查询
cves=colCVE.find({cpe\u searchField:{“$regex”:cpe\u regex,$options:'I'})。排序(“修改的”,-1)。限制(限制)。跳过).allow_disk_use(True)
需要大约的执行时间:0:00:00.407544,但此时要使用。count执行完整的分页算法,因为我需要总计数
cves.count()
0:00:00.407544
到50秒+。我该如何改进这一点

注意:此查询仅检索1510个OBJ。我需要加快速度,或者至少我可以如何将速度~00。?s提高到~1-2s

想法:

  • .计数>50秒+
  • .计数(真)>30秒+
代码

def cvesForCPE(cpe, vulnProdSearch=False, limit=0, skip=0):
    if not cpe:
        return []

    cpe_regex = cpe
    final_cves = []
    cpe_searchField = (
        "vulnerable_product" if vulnProdSearch else "vulnerable_configuration"
    )


    # default strict search
    cves = colCVE.find({cpe_searchField: {"$regex": cpe_regex, '$options' : 'i'}}).sort("Modified", -1).limit(limit).skip(skip).allow_disk_use(True)
    final_cves = cves

    final_cves = sanitize(final_cves)
    return cves.count()