【发布时间】:2020-11-09 07:53:55
【问题描述】:
我必须导入大约 20,000 行的 CSV 文件。此 CSV 是从 FTP 服务器读取的,该服务器每 30 分钟更新一次。但是我编写的代码已经花费了超过 45 分钟的时间来导入。这是非常缓慢的。谁能帮帮我。
foreach ($readers as $key => $row) {
$totalRecords +=1;
$filterArray = $this->entityManager->getRepository(Article::class)->findBy(['id' => $row['id']]);
if (empty($filterArray)) {
$notFoundRecords +=1;
continue;
}
$foundRecords +=1;
$this->processPriceRow($row);
}
protected function processPriceRow($row)
{
$existingRecord = $this->entityManager
->getRepository(WareHouse::class)
->findBy(['id' => $row['product_id']]);
if (empty($existingRecord)) {
return $this->fillArticleWareHouse($row);
}
}
protected function fillArticleWareHouse($row, $i, $batchSize)
{
$newWareHouse = new WareHouse();
....
....
...
// Insert.
$this->entityManager->persist($newWareHouse);
$this->entityManager->flush();
}
我正在考虑根据 batchSize = 100 来保存数据。但由于我在函数内部有函数,所以我也无法实现它。
【问题讨论】:
-
你有没有试过在每个实体持久化后不运行
flush?这将有很大帮助
标签: php symfony4 csv-import