search

Home  >  Q&A  >  body text

pymongo - mongodb 的find()耗时太长,应该怎样优化?

目的: 在超过10万条数据 随机 选取一条数据。
我的方法: data=db[item].find().skip(random_num).limit()
遇到问题: 耗费时间很长才能随机找到数据。根据profile测试,是因为find().怎么优化?

世界只因有你世界只因有你2811 days ago916

reply all(3)I'll reply

  • 仅有的幸福

    仅有的幸福2017-04-28 09:06:33

    It is recommended to do it according to the specific query conditions when randomly obtaining it. Do not skip directly.

    reply
    0
  • 某草草

    某草草2017-04-28 09:06:33

    Do you know mongo’s aggregation framework? Check it out, the key to high performance. As long as it is not cross-library or referenced, it can be used.

    reply
    0
  • 阿神

    阿神2017-04-28 09:06:33

    First create an index on the item, which can greatly improve the query speed. In addition, the random number conditions can be turned into specific query conditions (constructed with random numbers). In addition, the aggregation framework does not help with simple find queries like yours.

    reply
    0
  • Cancelreply