Persist and flush - Doctrine and MongoDB

2019-04-11 13:42发布

问题:

How fast is flush()? I'm adding several thousand item to a collection with persist(), then emptying the collection then flushing it.

$dm = $this->get('doctrine.odm.mongodb.document_manager');

while(stuff))
{
     $item = new Item();
     $item->setItem("item stuff");           
     $dm->persist($item);
}

$qb = $dm->createQueryBuilder('Bundle:Item')->remove();
$query = $qb->getQuery();
$query->execute();

$dm->flush(); 

I want to know how much time will the collection stay empty. Between the remove and the flush.

回答1:

I created a benchmark to profile flushes of a simple two-field document in various batch sizes: https://gist.github.com/2725976

$ php src/benchmark.php 10 100 1000 10000 20000 50000 100000
Flushing     10 items took  0.014058 seconds and   2097152 bytes
Flushing    100 items took  0.024325 seconds and    524288 bytes
Flushing   1000 items took  0.196992 seconds and   5505024 bytes
Flushing  10000 items took  2.563700 seconds and  57933824 bytes
Flushing  20000 items took  6.291873 seconds and  89915392 bytes
Flushing  50000 items took 19.118011 seconds and 240386048 bytes
Flushing 100000 items took 58.582809 seconds and 469499904 bytes

As you might expect, actually inserting the data into Mongo only accounts for a small fraction of these measurements. Doctrine is going to spent quite a bit of time walking through steps like events dispatching and changeset computation, the latter of which will be significantly impacted by the complexity of your domain model.

You can trace all of the Doctrine-specific operations in flush() by taking a look at UnitOfWork::commit().