私は多くのcsvの中で300000行のインポートに取り組んでいます。Doctrineは多くのデータを挿入します
まず、私は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と再帰関数に分割している:私は100行に分割さ多くのクエリを作成するには、このようにして
$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();
}
。 良い練習ですか、それとも多くの行を解析してItを挿入する方がいいですか?
グレートそれはより速く動作します! ありがとう –