Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 and doctrine2 performance of simple DQL query
    primarykey
    data
    text
    <p>Im developing a front end for a site where performance is very important. Rapid development was also key, so I made the decision to go with symfony2 and to use the Doctrine2 ORM.</p> <p>When I request a simple page from symfony (without DB request) it responds in ~200ms. As soon as I request anything from the DB through Doctrine it jumps up to ~1300ms. </p> <p>I appreciate there is time to hydrade the records, but this is with me hydrating to array.</p> <p>Heres the controller code. The template just outputs the array count.</p> <pre><code>$repository = $this-&gt;getDoctrine()-&gt;getManager()-&gt;getRepository('AcmeProductBundle:Product'); $qb = $repository-&gt;createQueryBuilder('g'); $qb-&gt;addOrderBy('g.id', 'DESC'); $ret = null; $query = $qb-&gt;getQuery(); //$query-&gt;useResultCache(true, 3600); $ret = $query-&gt;getArrayResult(); return $this-&gt;render('AcmeCatalogBundle::test.html.twig', array('id' =&gt; count($ret))); </code></pre> <p>Looking at the Timline in symfony profiler toolbar, the contoller is taking ~1000ms and doctrine is taking ~1ms. But if i comment out the getResults() line the controller jumps down to ~45ms.</p> <p>Is this normal? What can I do about this?</p> <p><strong>UPDATE</strong> I ran the following test and it shows that the first query is where all the time is lost:</p> <pre><code>$ret = array(); $start = microtime(true); for ($i = 1; $i &lt;= 10; $i++) { $time_start = microtime(true); $query = $em-&gt;createQuery('SELECT p FROM AcmeProductBundle:Product p WHERE p.id = 1'); $products = $query-&gt;getResult(); $time_end = microtime(true); $ret[$i] = $time_end - $time_start; } $end = microtime(true); $ret['Total'] = $end - $start; return $this-&gt;render('AcmeCatalogBundle::test.html.twig', array('ret' =&gt; $ret)); </code></pre> <p>And the results:</p> <ol> <li>1.0216779708862</li> <li>0.00091791152954102</li> <li>0.00082588195800781</li> <li>0.00081419944763184</li> <li>0.00081706047058105</li> <li>0.00081610679626465</li> <li>0.00081491470336914</li> <li>0.00081706047058105</li> <li>0.00043296813964844</li> <li>0.0004270076751709</li> </ol> <p>Total - 1.0283808708191</p> <p>Could it be that the db connection or entity manager isnt being pooled perhaps?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload