skip to Main Content

Given an entity Client, I have several columns, such as clientName, clientNo, etc.
The table itself holds 250+ entries. There are several ManyToOne links, with tables that hold 500.000+ entries.

For reasons I don’t understand, this query:

$queryBuilder = $this->getDoctrine()
            ->getRepository(Client::class)
            ->createQueryBuilder('c');

$queryBuilder
    ->select('partial c.{id, buha, short, name, price1, price2, price3, price4, hasSsv, isActive}')
    ->orderBy('c.name', 'ASC');

$clients = $queryBuilder->getQuery()->getResult();

and a twig template that only renders those fields, the profiler shows 1018 Queries in 25 Statements.

It queries for example for (which is one of the huge tables)

/**
* @ORMOneToMany(targetEntity="AppEntityWebDatabase", mappedBy="client")
*/
protected $webDatabase;

but I don’t know why? I tried fetch="EXTRA_LAZY" and fetch="EAGER", both moke no difference. There are more relations like this one, which I don’t care about with the above query.

I am using Symfony 4.4 with PHP 8.0 and MySQL 8 on Ubuntu 22.04.

2

Answers


  1. Only thing that stands out to me is the partial, which is deprecated. I would not be surprised that you select them partially, it then gets hydrated and for a good hydrate it needs more info ans selects it.

    If you can, just remove the select from the QB1, and/or remove the partial, and/or change the return to getScalarResult.

    A simple test would be the getScalarResult, if you implement that, do the num of queries reduce significantly? Then its likely something with hydration.

    1 Selecting what you need is a good MySQL practice, but IMO not a good Doctrine one.

    Login or Signup to reply.
  2. Instead of setting the partial keyword, you must set the HINT_FORCE_PARTIAL_LOAD Flag.

    $clients = $queryBuilder->getQuery()->setHint(DoctrineORMQuery::HINT_FORCE_PARTIAL_LOAD, true)->getResult();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search