目的: 在超过10万条数据 随机 选取一条数据。
我的方法: data=db[item].find().skip(random_num).limit()
遇到问题: 耗费时间很长才能随机找到数据。根据profile测试,是因为find().怎么优化?
仅有的幸福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.
某草草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.
阿神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.