2016-08-27 28 views
0

我需要向API提出大約250萬次請求才能獲取數據,並且這些請求的關鍵字存儲在MongoDb中。查詢數據庫並製作API請求的高效方法

目前我做這個(pseduo代碼):

$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請求?

回答

1

不知道這是否會有所幫助,但我會建議使用不同的文件。如果你有查詢不同意不會工作。

//this will filter all results and return only distinct keywords 
$docs = $collection->distinct('keyword'); 

https://docs.mongodb.com/manual/reference/method/db.collection.distinct/

內容查找試圖返回的數據量較少。只使用投影的關鍵字 https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-projection

對於api請求,如果您可以發送一堆關鍵字,然後保存結果也會提高性能。

偏移量保存數據仍然是同一時間。

希望這會有所幫助。

+0

哦,關鍵字已經是唯一的,它基本上得到一個產品'文檔',然後它的唯一'MPN'。 – 3zzy