Php symfony3长foreach执行时间

Php symfony3长foreach执行时间,php,symfony,Php,Symfony,我得到了一个文件上传监听器,它上传一个文件,解析它,并通过Doctrine将数据放到数据库中。脚本如下: <?php namespace AppBundle\EventListener; use CommonBundle\Entity\Classifiers; use Doctrine\Common\Persistence\ObjectManager; use Oneup\UploaderBundle\Event\PostPersistEvent; use CommonBundle\En

我得到了一个文件上传监听器,它上传一个文件,解析它,并通过Doctrine将数据放到数据库中。脚本如下:

<?php

namespace AppBundle\EventListener;
use CommonBundle\Entity\Classifiers;
use Doctrine\Common\Persistence\ObjectManager;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use CommonBundle\Entity\ClassifierPhrase;
use CommonBundle\Entity\ClassifierSubject;

class UploadListener
{
/**
 * @var ObjectManager
 */
private $om;

public function __construct(ObjectManager $om)
{
    $this->om = $om;
}

public function onUpload(PostPersistEvent $event)
{
    $type = $event->getRequest()->get('type');
    if($event->getRequest()->get('classifier_id')){
        $classifier_id = $event->getRequest()->get('classifier_id');
        $classifier = $this->om->getRepository('CommonBundle:Classifiers')->findOneBy(['id' => $classifier_id]);
    }

    $file = $event->getFile();
    $contents = file_get_contents($file->getPathname());
    $entries = preg_split("/\\r\\n|\\r|\\n/", $contents);

        foreach($entries as $entry) {
            $time_start = microtime(true); //start here
            //some parsing logic
            $this->om->persist($phrase);
            $this->om->flush();
            $time_end = microtime(true);
            $execution_time = round(($time_end - $time_start)/60, 4);
            file_put_contents('C:\xampp\htdocs\perfomance.txt', 'time: ' . $execution_time. ' phrase: '.$entry[0].PHP_EOL, FILE_APPEND);
//takes about 0.005 sec per entry
        }
    $response = [$event->getResponse(), 'success' => true];
    return $response;
  }
}

ORM工具主要不适合大规模插入、更新或删除。每个RDBMS都有自己的、最有效的方法来处理此类操作,如果下面列出的选项不足以满足您的目的,我们建议您使用特定RDBMS的工具进行这些批量操作

这意味着您不应该将ORM用于批量插入。就这么简单。
请使用Doctrine DBAL和SQL。

谁是
$phrase
?php和Doctrine实体管理器在批处理操作方面并不那么出色。有很多类似的问题,有各种各样的提示。每次冲洗后不要冲洗,而是每隔100次冲洗一次。这应该有点帮助。当然,将文件内容放在循环中是没有帮助的。并确保您在禁用xdebug的生产模式下运行。感谢您指出。我已经将
flush()
从外部删除,但没有多大帮助
file\u put\u contents
仅用于日志记录,如果没有它,脚本在prod中无法快速工作。从开始到
file\u put\u contents
的每个foreach循环平均需要0.005秒,这对我来说是令人满意的,我认为问题不在其中(这就是我从这里删除解析逻辑的原因)。还有更多的调试工具可以帮助解决问题或类似的问题吗?