我需要向API发出大约25000个请求来获取数据,这些请求的关键字存储在MongoDb中。
目前我这样做(伪代码):
$documents = $collection->find([ //conditions here ]);
// iterate through each row/document
foreach ($documents as $document) {
//grab the keyword
$keyword = $document['keyword'];
// make an API request
$AdditionalData = APIRequest($keyword, $arguments);
// store fetched data in another collection
$anotherCollection->insert($AdditionalData);
}
…但是由于请求成千上万,我每天都要做这件事,所以我想知道是否有更好更有效的方法来做这件事?
如果我将关键字存储在一个数组中,那么它的单个查询到数据库,然后用它来做API请求会更快吗?我不确定这是否有帮助,但我建议对所有文档使用distinct。如果你有不同的查询是行不通的。
//this will filter all results and return only distinct keywords
$docs = $collection->distinct('keyword');
https://docs.mongodb.com/manual/reference/method/db.collection.distinct/对于find,尝试返回更少的数据量。只有关键字使用投影https://docs.mongodb.com/manual/reference/method/db.collection.find/find-projection
对于api请求,如果你可以发送一堆关键字,然后保存结果也会提高性能。
脱轨数据保存时间不变。