저는 Doctrine을 처음 접했고 여전히 저에게 흐릿한 부분이 있습니다. 이 경우 루프와 엔터티 관리자를 사용하여 데이터베이스에 새 레코드를 삽입합니다. 잘 작동하지만 Doctrine이 엔터티별로 하나의 삽입 쿼리를 만드는 것으로 나타났습니다.
Doctrine2와 Symfony 2.3을 사용하여 모든 값이 포함 된 삽입 쿼리를 1 개만 만들도록 설정하는 방법을 알고 싶습니다 (물론 1 개의 엔티티에 대해서만 이야기하고 있습니다).
내가 의미하는 것은 이것을 바꾸는 것입니다.
INSERT INTO dummy_table VALUES (x1, y1)
INSERT INTO dummy_table VALUES (x2, y2)
으로
INSERT INTO dummy_table VALUES (x1, y1), (x2, y2)
내 코드는 다음과 같습니다.
$em = $this->container->get('doctrine')->getManager();
foreach($items as $item){
$newItem = new Product($item['datas']);
$em->persist($newItem);
}
$em->flush();
해결 방법
어떤 사람들은 왜 교리가 사용하지 않는지 궁금해하는 것 같습니다. 다중 삽입 ((...) 값에 삽입 (...), (...), (...), ...
우선,이 구문은 mysql 이상에서만 지원됩니다. postgresql versions. Secondly, there is no easy way to get hold of all the generated identifiers in such a multi-insert when using AUTO_INCREMENT or SERIAL and an ORM needs the identifiers for identity management of the objects. Lastly, insert performance is rarely the bottleneck of an ORM. Normal inserts are more than fast enough for most situations and if you really want to do fast bulk inserts, then a multi-insert is not the best way anyway, i.e. Postgres COPY or Mysql LOAD DATA INFILE은 몇 배 더 빠릅니다.
이것이 구현하는 데 노력할 가치가없는 이유입니다. abstraction that performs multi-inserts on mysql and postgresql in an ORM.
Doctrine2 일괄 처리에 대한 자세한 내용은 여기에서 읽을 수 있습니다.
DBAL로 전환하거나 일정량의 삽입 후 엔터티 관리자를 플러시하여 데이터를 소규모 일괄 처리로 처리 할 수 있습니다.
$batchSize = 20;
foreach ($items as $i => $item) {
$product = new Product($item['datas']);
$em->persist($product);
// flush everything to the database every 20 inserts
if (($i % $batchSize) == 0) {
$em->flush();
$em->clear();
}
}
// flush the remaining objects
$em->flush();
$em->clear();
참조 페이지 https://stackoverflow.com/questions/18654530
'MySql' 카테고리의 다른 글
MySQL 업데이트가 있으면 삽입하십시오-중복 키 혼동시 (0) | 2020.12.23 |
---|---|
MySQL Symfony2 Doctrine을 사용하여 기존 데이터베이스에서 엔티티 가져 오기 (0) | 2020.12.23 |
MySQL mysql : Left Join이 인덱스를 사용하지 않는 이유는 무엇입니까? (0) | 2020.12.23 |
MySQL What is the best datatype for storing URLs in a MySQL database? (0) | 2020.12.23 |
MySQL MYSQL datetime 열에 삽입 할 datetime c #의 형식 문자열 (0) | 2020.12.23 |
댓글