Php Symfony,创建新对象慢

Php Symfony,创建新对象慢,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我需要编写一个foreach循环,用户可以在其中使用doctor订阅并插入到数据库中。我的代码: $i=0; $batchSize=20; foreach ($members as $member) { $subscription = new Subscription($company, $user); $entityManager->persist($subscription); // debug $i++; error_log($i);

我需要编写一个foreach循环,用户可以在其中使用doctor订阅并插入到数据库中。我的代码:

$i=0;
$batchSize=20;

foreach ($members as $member) {
    $subscription = new Subscription($company, $user);
    $entityManager->persist($subscription);
    // debug
    $i++;
    error_log($i);

    if ($i % $batchSize === 0) {
        $entityManager->flush();
        $entityManager->clear();
    }
}
这个代码真的很慢。对于大约100个用户,此代码需要几分钟来执行。这应该快很多,对吗

当我删除对象创建(以及setMember和entityManager行)时,执行此代码的时间不到一秒钟

我做错了什么

更新:实体代码 由于代码机密,我更改了一些变量。如果有任何代码是错误的,那是因为我做了快速的更改,所以我可以在这里发布我的问题

上述订阅类中使用的AttributeCollection类:

class AttributeCollection
{

    protected $id;
    protected $attributes;

    public function __construct()
    {
        $this->attributes = new ArrayCollection();
    }

    public function removeDefaults()
    {
        foreach ($this->attributes as $attribute)
            if ($attribute->isSystemDefault())
                $this->removeAttribute($attribute);
    }

    public function clearDefaults()
    {
        foreach ($this->attributes as $attribute)
            $attribute->setSystemDefault(false);
    }

    public function getAttributes()
    {
        return $this->attributes;
    }

    public function addAttribute(Attribute $attribute)
    {
        if (!$this->findAttributeByName($attribute->getName()))
            $this->attributes[] = $attribute;
    }

    public function removeAttribute(Attribute $attribute)
    {
        $this->attributes->removeElement($attribute);
    }

    public function findAttributeByName($name)
    {
        foreach ($this->attributes as $attribute)
            if ($attribute->getName() == $name)
                return $attribute;
        return;
    }

    public function get($name)
    {
        $attribute = $this->findAttributeByName($name);
        if ($attribute)
            return $attribute->getValue();
        return;
    }

    public function getAll()
    {
        return $this->attributes->toArray();
    }

}

对于这样小的实体,使用这样小的
$batchSize
不是最佳选择

文档中介绍了这种持久化方法,作为如何优化内存和连接加载的示例,而不是整个脚本的速度

如果在
$i
$batchSize
上删除这些检查,速度会更快

foreach ($members as $member) {
    $subscription = new Subscription();
    $subscription->setMember($member);
    $entityManager->persist($subscription);
}
$entityManager->flush();

还要记住,在dev env中,您的
debug
选项设置为
enabled
,对数据库的所有查询都记录在文件中。这也会浪费时间。

事实证明,通过构造函数传递这些模型会导致问题

我更改了代码:

class Subscription {
    ...
    public function __construct($Company == null, $member == null) {
        ...
    }
}
在我的foreach循环中:

$subscription = new Subscription();
$subscription->setCompany($company);
$subscription->setMember($member);

这样,一切都会顺利进行,正如预期的那样。我不知道为什么构造器会减慢速度。但显然是这样。

可能刷新
20个
实体是一项耗时的任务。您是否尝试过降低
$batchSize
?@D4V1D我现在知道创建模型非常耗时。如果我删除与条令相关的行,并离开模型创建。行
error\u log($i)是什么做什么?我的意思是它使用哪个处理器?它是否每次都打开/关闭日志文件?@JovanPerovic它只是将$i写入日志文件。我知道这不是问题所在,因为我
tail-f
文件。如果没有模型创建,$i会立即打印出来。我明白了。您是否尝试在
prod
中运行此操作?如果是这样,结果是什么?如上所述,“当我删除对象创建(以及setMember和entityManager行)时,执行此代码的时间不到一秒钟。”$I+$batchSize检查不是我的问题,也不是检查。我的意思是
$entityManager->flush()
$entityManager->clear()如果在所有操作之后只进行一次刷新,则速度会更快。而且这些刷新和清除也不是问题所在。我删除了这些,只留下模型创建。这个模型的创建似乎需要很长的时间,他有多少,比如说,500多名成员?他一定会很快把公羊累死的。我认为他的方法是正确的@AngeloA:newsubscription()的作用是什么做什么?它会在实体中创建一些内部对象吗?@JovanPerovic我的意思是,如果他有大约100个成员,并且没有重实体,那么他就不需要使用这种“优化”。
$subscription = new Subscription();
$subscription->setCompany($company);
$subscription->setMember($member);