【问题标题】:doctrine 1.2 performance, creating a large number of records (~5000)学说 1.2 性能,创建大量记录 (~5000)
【发布时间】:2023-03-12 10:47:01
【问题描述】:

假设 User 是扩展 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();

我发现此脚本在 30 秒后超时,并且无法创建所有用户。有没有办法优化此代码以更快地工作?我正在考虑只编写常规的旧 SQL 来创建记录,但随后我失去了 Doctrine_Record 的功能,包括插入钩子等(请注意,即使用户模型中没有钩子,此脚本也会超时)。

有什么建议吗?

【问题讨论】:

    标签: php mysql performance doctrine-1.2


    【解决方案1】:

    您应该尝试Doctrine_Collection 类,它允许批量插入:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2017-06-05
    • 2011-08-23
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多