title: Doctrine 2.2 + Zend Framework pagination speed optimization
body: <p>I'm struggling with Doctrine 2 performce when using <code>HYDRATE_OBJECT</code>. When I switch from <code>HYDRATE_ARRAY</code> to <code>HYDRATE_OBJECT</code>, it takes nearly 10 times longer! I've used <a href="https://stackoverflow.com/questions/6635194/doctrine-2-and-zend-paginator/9334016#9334016">doctrine 2 and zend paginator</a> as reference:</p>
  
  <pre><code>$query = $em-&gt;createQuery($dql)
      -&gt;setHydrationMode(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY)
      -&gt;setParameter('x', 1);
  
  // Pagination
  $paginator = new Doctrine\ORM\Tools\Pagination\Paginator($query, false);
  $iterator = $paginator-&gt;getIterator();
  die(); // 160 ms
  </code></pre>
  
  <p>vs</p>
  
  <pre><code>$query = $em-&gt;createQuery($dql)
      -&gt;setHydrationMode(\Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT)
      -&gt;setParameter('x', 1);
  
  // Pagination
  $paginator = new Doctrine\ORM\Tools\Pagination\Paginator($query, false);
  $iterator = $paginator-&gt;getIterator();
  die(); // 1.4s
  </code></pre>
  
  <p>What should I watch out for? How to reduce the processing time and still utilize <code>HYDRATE_OBJECT</code>? Is there a better way to accomplish pagination?</p>
  
  <p><strong>*Edit</strong>: Using <code>-&gt;setFirstResult($itemsPerPage * $page - $itemPerPage)-&gt;setMaxResults($itemsPerPage);</code> significantly decrease loading time, but when using <code>$iterator</code>:</p>
  
  <pre><code>$adapter =  new \Zend_Paginator_Adapter_Iterator($iterator);
  $zend_paginator = new \Zend_Paginator($adapter);          
  $zend_paginator-&gt;setItemCountPerPage($itemsPerPage)
      -&gt;setCurrentPageNumber($page);
  </code></pre>
  
  <p>Zend only knows about <code>$itemsPerPage</code>, (<code>count($iterator) == $itemsPerPage</code>) and thus the pagination links always calculate 1 page only. How can I accomplish a proper pagination using Zend_Paginator, and only load <code>$itemsPerPage</code>entities?</p>