yefrem
yefrem

Reputation: 666

Pagerfanta and Doctrine2 COUNT optimization

I'm using Pagerfanta and Doctrine Adapters with Symfony2 and Silex. As my database became bigger I noticed huge load on admin stats pages that display big data with pagination. I checked profiler and saw unbelievably inefficient queries:

SELECT DISTINCT id16
FROM (
    SELECT f0_.username AS username0, ..., f0_.added_on AS added_on20
    FROM fos_user f0_ ORDER BY f0_.id DESC
) dctrn_result
LIMIT 50 OFFSET 0;

SELECT COUNT(*) AS dctrn_count
FROM (
    SELECT f0_.username AS username0, ..., f0_.added_on AS added_on20
    FROM fos_user f0_ ORDER BY f0_.id DESC
) dctrn_result
LIMIT 50 OFFSET 0;`

First query was easy to fix by creating fixed version of DoctrineORMAdapter class. The code that generates COUNT() query is more complicated so I decided to ask if there's any solution for this.

So is there any way to make Pagerfanta not running nested queries?

Upvotes: 6

Views: 3300

Answers (2)

Vigintas Labakojis
Vigintas Labakojis

Reputation: 1069

Better late than never: I've hit the same wall today with >200k records and found a solution.

Pagerfanta internally uses Doctrine\ORM\Tools\Pagination\CountOutputWalker to count objects which results in a count query like this:

SELECT 
  COUNT(*) AS dctrn_count 
FROM 
  (
    SELECT 
      DISTINCT id_0 
    FROM 
      (
        SELECT 
          m0_.id AS id_0, 
          ...
        FROM 
          messaging_messages m0_ 
        ORDER BY 
          m0_.id DESC
      ) dctrn_result
  ) dctrn_table

To bypass CountOutputWalker we can pass a flag when instantiating DoctrineORMAdapter. So instead of simply

$adapter = new DoctrineORMAdapter($qb);

you do

$adapter = new DoctrineORMAdapter($qb, true, false);

(third parameter). This turns the count query into a much more efficient one:

SELECT 
  count(DISTINCT m0_.id) AS sclr_0 
FROM 
  messaging_messages m0_

You have to update whiteoctober/Pagerfanta to 1.0.3 though.

Issue

Related commit

Upvotes: 10

Vigintas Labakojis
Vigintas Labakojis

Reputation: 1069

In your case it's not pagerfanta that does the sub-queries. It's the source your query builder instance it's coming from.

I usually have a function in entity repository that returns a plain query builder instance instead of the results. It's left to you to write an efficient query builder. Then I feed that query builder into DoctrineORMAdapter.

I've got this helper function that I use throughout my projects:

/**
 * Pass an array, entity or a custom QueryBuilder instance to paginate.
 * Takes an array of parameters as a second argument.
 * Default parameter values:
 *
 * $params = array(
 *     'curPage' => 1,
 *     'perPage' => 15,
 *     'order' => 'DESC'
 * );
 *
 * @param mixed $object
 * @param array $params
 *
 * @return Pagerfanta
 */
public function paginate($object, $params = array())
{
    if (is_array($object)) {
        $adapter = new ArrayAdapter($object);
    } elseif ($this->isEntity($object)) {
        $qb      = $this->em->createQueryBuilder()
            ->select('s')
            ->from($this->getEntityName($object), 's')
            ->orderBy('s.id', isset($params['order']) ? $params['order'] : 'DESC');
        $adapter = new DoctrineORMAdapter($qb);
    } elseif ($object instanceof QueryBuilder) {
        $adapter = new DoctrineORMAdapter($object);
    }
    $pager = new Pagerfanta($adapter);
    $pager->setMaxPerPage(isset($params['perPage']) ? $params['perPage'] : 15);
    $pager->setCurrentPage(isset($params['curPage']) ? $params['curPage'] : 1);

    return $pager;
}

You can pass an array, entity or a query builder instance and it will return an appropriately paginated object ready to use.

You probably know how it's done, but anyway, here's what I have in my entity repository - one function returns query builder instance (perfect for pagerfanta), the other returns an array to be used elsewhere:

public function getMessageQueryBuilder($campaignId, $eqCriteriaArray = array(), $neqCriteriaArray = array())
{
    $qb = $this->createQueryBuilder('m');
    $qb->select('m')
        ->leftJoin('m.campaign', 'c')
        ->leftJoin('m.sentBy', 'u')
        ->where($qb->expr()->eq('m.campaign', $campaignId));
    foreach ($eqCriteriaArray as $property => $value) {
        $qb->andWhere($qb->expr()->eq($property, $qb->expr()->literal($value)));
    }
    foreach ($neqCriteriaArray as $property => $value) {
        $qb->andWhere($qb->expr()->neq($property, $qb->expr()->literal($value)));
    }

    return $qb->orderBy('m.id', 'DESC');
}

public function filterMessages($campaignId, $eqCriteriaArray = array(), $neqCriteriaArray = array())
{
    return $this->getMessageQueryBuilder($campaignId, $eqCriteriaArray, $neqCriteriaArray)->getQuery()->getResult();

Then I combine those two to get the actual pager object:

$singleSmsPager = $this->pagerUtil->paginate(
    $this->em->getRepository('TreasureForgeMessageBundle:Message')
        ->getMessageQueryBuilder(CcToolSender::CAMPAIGN_ID, array(), array('u.username' => 'admin')),
    array(
        'curPage' => $singleSmsPage,
        'perPage' => 10
    )
);

Upvotes: 0

Related Questions