简体   繁体   English

原则1.2的性能,创建大量记录(〜5000)

[英]doctrine 1.2 performance, creating a large number of records (~5000)

Assuming User is a model which extends Doctrine_Record, consider the below (arbitrary) code that creates 5000 user records. 假设用户是扩展Doctrine_Record的模型,请考虑下面的(任意)代码,该代码创建了5000条用户记录。

$conn->beginTransaction();

for ($i = 1, $i < 5001, $i++) {

    $user = new User();
    $user->some_field = $i;
    $user->save();
    $user->free(true);
}

$conn->commit();

I am finding that this script is timing out after 30 seconds and not managing to create all the Users. 我发现此脚本在30秒后超时,无法创建所有用户。 Are there ways to optimize this code to work more quickly? 有什么方法可以优化此代码以使其更快地工作? I am considering just writing regular old SQL to create the records but then I lose the power of the Doctrine_Record, including insert hooks etc (Note that this script times out even if there are NO hooks in the User model). 我正在考虑只编写常规的旧SQL来创建记录,但是随后我失去了Doctrine_Record的功能,包括插入挂钩等(请注意,即使用户模型中没有挂钩,该脚本也会超时)。

Any advice? 有什么建议吗?

You should try the Doctrine_Collection class, which allows bulk inserts : 您应该尝试Doctrine_Collection类,该类允许批量插入:

$collection = new Doctrine_Collection('tablename');
$collection->add($record1);
$collection->add($record2);
$collection->add($record3);
$collection->add($record4);
$collection->save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM