繁体   English   中英

实体管理器在Symfony上的性能

[英]performance of Entity manager on Symfony

我必须发出用于数据迁移的命令,但是时间执行时间太长,很长一段时间后崩溃,我需要使其速度更快,您能帮我吗? 这是一个例子:

  1. 情况

    • 我有一个实体
    • 我有一个客户实体
    • 我有一个MatterCustomer实体
    • 事项有一个或多个事项
    • 客户可以是无人或多人
      请注意,实体之间的链接设置正确
  2. 命令

     function execute(InputInterface $input, OutputInterface $output) { // Here connection to old database $this->pdo = $this->container()->get('app.command_oldbase.odbase_helper')->connectToDatabase(); $em = $this->getContainer()->get('doctrine')->getManager(); $em->getConnection()->beginTransaction() $em->getConnection()->setAutoCommit(false); try{ $this->migrateMatters($output); } catch (Exception $e) { $em->getConnection()->rollback(); throw $e; } $em->getConnection()->commit(); } function migrateMatters(OutputInterface $output) { $statement = $this->pdo->prepare('Select id,name From oldmatter'); $statement->execute(); $oldmatters = $statement->fetchAll(); foreach ($oldmatters As $oldmatter) { $this->em = $this->getContainer()->get('doctrine')->getManager(); $matter = (new Matter()) ->setName($oldmatter['name']) ; $this->em->persist($matter); $this->em->flush(); $this->setMatterCustomer($matter, $oldmatter['id']); $this->em->clear(); } } function setMatterCustomer(Matter $matter, int $oldmatter_id) { $statement = $this->pdo->prepare('Select customername From oldmattercustomers Where oldmatter_id = :oldmatter_id'); $statement->bindParam(':oldmatter_id', $oldmatter_id); $statement->execute(); $oldcustomers = $statement->fetchAll(); foreach ($oldcustomers As $oldcustomer) { //search customer in Customer entity $customer = $this->em->getRepository(Customer::class)->findOneBy(['name' => $oldcustomer['customername']]); //search if MatterCustomer exist $matterCustomer = $this->em->getRepository(MatterCustomer::class)->findOneBy(['matter' => $matter, 'customer' => $customer]); if ($matterCustomer == null) { $matterCustomer = (new MatterCustomer()) ->setContact($contact) ->setMatter($matter) ; //add MatterCustomer to arrayCollection in Matter $matter->addCustomer($matterCustomer); $this->em->persist($matter); $this->em->flush(); } } } 
  1. 使用批处理

  2. 对问题客户使用层叠保留 因此,首先需要创建Matter实例,然后再添加到实例MatterCustomer中,依此类推。 然后,仅执行持久性事件实例。

  3. 如果存在多个实体,则可能禁用日志记录将减少内存消耗

$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

  1. 使用--no-debug选项运行该命令(或在prod模式下运行),它将减少内存消耗。

暂无
暂无

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

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