【发布时间】:2014-02-05 22:27:23
【问题描述】:
我有一个脚本需要获取数据库中的条目列表,然后迭代那些在另一个表中创建新条目(如果它们不存在)。
目前正在做:
foreach($entries as $entry){
$newItem = new Item();
$newItem->setAttribute($entry->getAttribute());
$entityManager->persist($newItem);
try{
$entityManager->flush();
} catch(\Exception $e){
if(!strpos($e->getMessage(),'Duplicate')){
throw $e;
}
$entityManager = $this->getDoctrine()->getManager();
//refreshes the entity manager
}
}
但是这样做非常耗时,有 1000 个条目,脚本有时需要 10 分钟以上才能完成。我已经看到其他帖子建议在进行这样的批处理以每 20 条左右刷新一次记录时,问题是如果这 20 条中的一条是重复的,那么整个事务就会终止,我不确定我将如何回去尝试找到违规条目以将其排除,然后再次重新提交。
对此的任何帮助将不胜感激。
【问题讨论】:
-
不要在
flush()你的批次在foreach()循环内...
标签: php mysql symfony doctrine-orm