Doctrine2批次插入 [英] Doctrine2 Batch Insert

查看:156
本文介绍了Doctrine2批次插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用单个查询将多个实体插入到DB中?
当我使用这里我可以在Web Debugger中看到几个查询

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

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 - 如何提高冲洗效率?

  3. 原则2:批处理引用其他实体的实体插入时的奇怪行为

  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

主要内容:


有些人似乎想知道为什么Doctrine不使用
多插入(insert into(...)values(...) ,(...),(...),...

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

首先,这种语法只支持mysql和更新的postgresql versi附件。其次,当使用
AUTO_INCREMENT或SERIAL时,没有简单的方法来获取所有
所生成的标识符,并且ORM需要标识符
管理对象。最后,插入性能很少是ORM的
瓶颈。正常插入对于
大多数情况来说都非常快,如果你真的想做快速批量插入,那么一个
多插入不是最好的方式,比如Postgres COPY或Mysql
加载数据INFILE的速度是几个数量级。

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.

这些是为什么不实现在mysql和postgresql上执行多插入的抽象工作的原因
一个ORM。
我希望清除一些问号。

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.


推荐答案

我认为会有是几个insert语句,但是每个flush调用只对数据库进行一次查询。

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

如上所述
http://doctrine-orm.readthedocs.org/en/2.0.x/reference/working -with-objects.html

每个persist将对当前的UnitOfWork添加一个操作,然后调用EntityManager#flush(),实际上会写入数据库(在单个事务中封装UnitOfWork的所有操作)。

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.

最好的问候,
Christophe

Best regards, Christophe

这篇关于Doctrine2批次插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆