Doctrine2 - 在一个查询中多次插入
Doctrine2 - multiple insert in one query
我假设 doctrine2 会优化我的查询,为任何数据库事务提供最佳性能。
我在数据库中插入大约 500 条记录 table,我开始注意到它创建了 500 多个查询来插入记录(每条记录一个查询),我想知道,为什么不教义利用多个插入一次性插入所有记录,这不会减少负载并优化查询吗?我是否从学说中遗漏了有关此行为的某些信息?
这是我用于插入的代码:
$content = json_decode($response->getBody());
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
foreach ($content as $value) {
$log = new log();
$log->fromArray($value);
$em->persist($log);
}
$em->flush();
更新1:
以下是请求的 fromArray() 的内容,此函数的目的基本上是将数组中的值合并为 class 属性:
/**
* Map parameters with class property.
*
* @param $array array
* @access public
* @return $this
*/
public function fromArray(array $array)
{
foreach ($array as $property => $value) {
$method = 'set'.ucwords($property);
$this->$method($value);
}
return $this;
}
这里是$content
的内容:
Array
(
[0] => stdClass Object
(
[id] => 111
[guid] => aaaa-bbbb-cccc
[wid] => 100
[pid] => 101
[start] => 2014-11-22T12:44:44+00:00
[stop] => 2014-11-22T15:23:11+00:00
[duration] => 9507
[description] => Log description
[tags] => Array
(
[0] => test
)
[at] => 2014-11-24T07:28:09+00:00
[uid] => 51
)
[1] => stdClass Object
(
[id] => 112
[guid] => dddd-eee
[wid] => 100
[pid] => 101
[billable] =>
[start] => 2014-11-22T15:35:07+00:00
[stop] => 2014-11-22T15:45:21+00:00
[duration] => 614
[description] => Lorem description
[tags] => Array
(
[0] => php
[1] => pm
)
[at] => 2014-11-24T04:35:30+00:00
[uid] => 51
)
)
请查看 this 文档。在每次迭代中,Doctrine 都会为你创建一个 insert ...
查询,毕竟,当你调用 flush()
时,Doctrine 在一个循环内一次将所有这些插入查询发送到 db(使用像 [=12 这样的机制) =])
在您的情况下,这些查询中的任何一个都是插入,因此,正如我在评论中所述,您的情况没有异常情况。 ORM 可能不适合所有情况。
我假设 doctrine2 会优化我的查询,为任何数据库事务提供最佳性能。
我在数据库中插入大约 500 条记录 table,我开始注意到它创建了 500 多个查询来插入记录(每条记录一个查询),我想知道,为什么不教义利用多个插入一次性插入所有记录,这不会减少负载并优化查询吗?我是否从学说中遗漏了有关此行为的某些信息?
这是我用于插入的代码:
$content = json_decode($response->getBody());
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
foreach ($content as $value) {
$log = new log();
$log->fromArray($value);
$em->persist($log);
}
$em->flush();
更新1:
以下是请求的 fromArray() 的内容,此函数的目的基本上是将数组中的值合并为 class 属性:
/**
* Map parameters with class property.
*
* @param $array array
* @access public
* @return $this
*/
public function fromArray(array $array)
{
foreach ($array as $property => $value) {
$method = 'set'.ucwords($property);
$this->$method($value);
}
return $this;
}
这里是$content
的内容:
Array
(
[0] => stdClass Object
(
[id] => 111
[guid] => aaaa-bbbb-cccc
[wid] => 100
[pid] => 101
[start] => 2014-11-22T12:44:44+00:00
[stop] => 2014-11-22T15:23:11+00:00
[duration] => 9507
[description] => Log description
[tags] => Array
(
[0] => test
)
[at] => 2014-11-24T07:28:09+00:00
[uid] => 51
)
[1] => stdClass Object
(
[id] => 112
[guid] => dddd-eee
[wid] => 100
[pid] => 101
[billable] =>
[start] => 2014-11-22T15:35:07+00:00
[stop] => 2014-11-22T15:45:21+00:00
[duration] => 614
[description] => Lorem description
[tags] => Array
(
[0] => php
[1] => pm
)
[at] => 2014-11-24T04:35:30+00:00
[uid] => 51
)
)
请查看 this 文档。在每次迭代中,Doctrine 都会为你创建一个 insert ...
查询,毕竟,当你调用 flush()
时,Doctrine 在一个循环内一次将所有这些插入查询发送到 db(使用像 [=12 这样的机制) =])
在您的情况下,这些查询中的任何一个都是插入,因此,正如我在评论中所述,您的情况没有异常情况。 ORM 可能不适合所有情况。