Home > Software engineering >  Symfony 5 doctrine flush slows down
Symfony 5 doctrine flush slows down

Time:06-30

I was just wondering why updating a record (flush) slows down over time. For example, if i want to check mulitple records and compare them with eachother and after comparisation update a column like setCompared(true);. The first 1000 records are fine and fast but after that, the flush seems like it slows down after each flush. Is there a reason for that, like maybe it builds up or something. Do i need to clear() after flush().

Does anyone have a explaination for that?

CodePudding user response:

You should do something like this according to the Doctrine documentation) :

<?php

$batchSize = 20;
for ($i = 1; $i <= 10000;   $i) {
    $user = new CmsUser;
    $user->setStatus('user');
    $user->setUsername('user' . $i);
    $user->setName('Mr.Smith-' . $i);
    $em->persist($user);
    if (($i % $batchSize) === 0) {
        $em->flush();
        $em->clear(); // Detaches all objects from Doctrine!
    }
}
$em->flush(); // Persist objects that did not make up an entire batch
$em->clear();

The following code shows an example for inserting 10000 objects with a batch size of 20. This means that we insert them 20 by 20 with the flush().

  • Related