2 回答

TA貢獻1799條經驗 獲得超8個贊
我改變了我的算法的工作方式,首先獲取所有需要更新的 id,然后以 500-1000 的批次從數據庫中獲取它們(我正在運行測試)。
/*
* to avoid creating arrays with too much objects, we loop on the ids and split them by DEFAULT_BATCH_SIZE
* this way we get them by packs of DEFAULT_BATCH_SIZE and add them by the same amount
*/
for ($i = 0 ; $i < sizeof($idsToRequest) ; $i++) {
$currentSetOfIds[] = $idsToRequest[$i];
// every time we have DEFAULT_BATCH_SIZE ids or if it's the end of the loop we update the documents
if ($i % self::DEFAULT_BATCH_SIZE == 0 || $i == sizeof($idsToRequest)-1) {
if ($currentSetOfIds) {
// retrieves from the database a batch of entities
$entities = $thatRepo->findBy(array('id' => $currentSetOfIds));
// serialize and create documents with the entities we got earlier
foreach($entities as $entity) {
$data = $this->serializer->serialize($entity, 'json', SerializationContext::create()->setGroups($groups));
$documents[] = new \Elastica\Document($entityToFind[$indexName][$entity->getId()], $data);
}
// update all the documents serialized
$elasticaType->updateDocuments($documents);
// reset of arrays
$currentSetOfIds = [];
$documents = [];
}
}
}
我正在以相同的數量更新它們,但它仍然沒有提高序列化方法的性能。我真的不明白它與序列化程序有什么不同,我有 9k 或 10k 個實體,而它從來不知道......

TA貢獻1862條經驗 獲得超6個贊
在我看來,您應該檢查內存消耗:您正在構建一個大數組,其中列出了很多對象。
您有兩種解決方案:使用生成器避免構建該數組,或者嘗試每“x”次迭代推送您的文檔并重置您的數組。
我希望這能讓您了解如何處理此類遷移。
順便說一句,我差點忘了告訴你避免使用 ORM/ODM 存儲庫來檢索數據(在遷移腳本中)。問題是他們會使用對象并給它們加水,老實說,在龐大的遷移腳本中,你只會永遠等待。如果可能,只需使用 Database 對象,這可能足以滿足您的需求。
- 2 回答
- 0 關注
- 145 瀏覽
添加回答
舉報