Doctrine2 Batch Insert

2019-06-21 03:09发布

Is it possible to insert several entities to DB with a single query? When I use an example from here I can see several queries in the Web Debugger

UPDATED 23.08.2012

I found the following related links. I hope it will help to someone to understand a batch processing:

  1. http://www.doctrine-project.org/blog/doctrine2-batch-processing.html
  2. doctrine2 - How to improve flush efficiency?
  3. Doctrine 2: weird behavior while batch processing inserts of entities that reference other entities

The main things:

Some people seem to be wondering why Doctrine does not use multi-inserts (insert into (...) values (...), (...), (...), ...

First of all, this syntax is only supported on mysql and newer 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 are several orders of magnitude faster.

These are the reasons why it is not worth the effort to implement an abstraction that performs multi-inserts on mysql and postgresql in an ORM. I hope that clears up some questionmarks.

1条回答
老娘就宠你
2楼-- · 2019-06-21 03:37

I think that there will be several insert statements, but only one query to the database per "flush" call.

As mentioned here http://doctrine-orm.readthedocs.org/en/2.0.x/reference/working-with-objects.html

Each "persist" will add an operation to the current UnitOfWork, then it is the call to EntityManager#flush() which will actually write to the database (encapsulating all the operation of the UnitOfWork in a single transaction).

But I have not checked that the behavior I describe above is the actual behavior.

Best regards, Christophe

查看更多
登录 后发表回答