我正在处理许多csv中的300000行的导入。学说插入许多数据
首先,我将csv导入数据库中的每一行。
我想解析所有行并将其插入到具有某种关系数据的右表中。
所以我已经试过这样:
$qb = $this->entityManager->createQueryBuilder();
$flows = $qb->select('flow')
->from('AppBundle:FlowAndata', 'flow')
->getQuery()
->getResult();
$countRows = 0;
foreach ($flows as $row) {
//some check
$entity = new TestTable();
$entity->setCode($row->getCode());
//many other fields
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
在这种情况下,所有的过程花了大约5秒钟的每一行!
现在如果我添加setMaxResults这样的:
$qb = $this->entityManager->createQueryBuilder();
$flows = $qb->select('flow')
->from('AppBundle:FlowAndata', 'flow')
->setMaxResults(100)
->getQuery()
->getResult();
花了不到1秒!
所以我想获得的所有行,并将其与setMaxResult分成递归函数是这样的:
$qb = $this->entityManager->createQueryBuilder();
$flows = $qb->select('flow')
->from('AppBundle:FlowAndata', 'flow')
->getQuery()
->getResult();
$countFlows = count($flows);
$numberOfQuery = $countFlows/100;
for ($i = 0; $i <= $numberOfQuery; $i++) {
$this->entityManager->clear();
$qb = $this->entityManager->createQueryBuilder();
$flows = $qb->select('flow')
->from('AppBundle:FlowAndata', 'flow')
->setFirstResult($i * 100)
->setMaxResults(100)
->getQuery()
->getResult();
}
这样,我创造了许多查询分裂成100行。 是一种很好的做法,还是有更好的方法来解析许多行并插入它?
大它的工作原理来得更快! 谢谢 –