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 关注
- 116 浏览
添加回答
举报