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()