coopcycle / coopcycle-web

Logistics & marketplace platform. Only for worker-owned business.
https://coopcycle.org
Other
565 stars 129 forks source link

Optimize performance of restaurant pages #2005

Open alexsegura opened 3 years ago

alexsegura commented 3 years ago

When displaying a restaurant page, or when serializing a restaurant menu, i.e when iterating lots of products, there is a performance problem related to Doctrine.

By default, Doctrine uses lazy loading for associations, which means that when we need to iterate all products + all options of products, it will trigger a query for each product, and another query for each option of each product.

Using eager loading on some associations allows a huge performance boost.

On the trace below (79 products), using eager loading (37ddc9e251acac838f747862470f8527436e027a) has helped save more than 1s of execution time.

But maybe it's not good to setup eager loading in Doctrine mapping files? Maybe it should be decided depending on the context?

See 5 Doctrine ORM Performance Traps You Should Avoid

Capture d’écran 2020-12-31 à 15 05 23
alexsegura commented 3 years ago

Loading translations also generates a lot of queries.

Translations could easily be cached as they don't change much.

For this we could use Doctrine 2nd level cache. See ORMTranslatableListener as an example on how to do it. Probably need to use Doctrine\ORM\Mapping\ClassMetadataInfo::enableAssociationCache()