【问题标题】:Optimise Pymongo query time优化 Pymongo 查询时间
【发布时间】:2022-12-09 13:01:15
【问题描述】:

我在 mongodb 上运行查询并寻找解决方案来优化所用时间。

我的查询就像collection.find({'nameId':989080880,'Date':{'$gte':startDate}})

我所做的如下

pd.DataFrame(collection.find({'nameId':989080880,'Date':{'$gte':startDate}}))

此查询耗时:x 毫秒

然后我试过了

document=[]
for doc in collection.find({'nameId':989080880,'Date':{'$gte':startDate}}):
   document.append(doc)

但它只比 x ms 提高了 15%

无法索引,因为“nameId”是一个长整数,索引将需要更多的 RAM 等。

期待一些建议

【问题讨论】:

    标签: python-3.x mongodb-query pymongo


    【解决方案1】:

    首先是使用列表理解来优化列表,例如:

     document=[doc for doc in collection.find({'nameId':989080880,'Date':{'$gte':startDate}})]
    

    其次,如果您不需要文档中的所有字段,只需使用投影,它是一个包含您需要返回的所有字段的对象。 在您的查找查询中使用 projection = { name: 1, address: 1},您的所有文档仅返回名称、地址字段和 _id。 更多信息可以阅读here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-10
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多