Search code examples
symfony1doctrinequery-optimization

Symfony : How can I optimize Doctrine query?


I want to know how I can optimize my Doctrine queries because some of my queries are too long.

I don't want a method particularly to my query, but common methods I can apply to all requests, because I don't find information about this.


Solution

  • Here are some tips to make Doctrine perform better:

    http://www.doctrine-project.org/projects/orm/1.2/docs/manual/improving-performance/en#improving-performance

    The changes that I've noticed make the biggest change:

    • Use array hydration instead of object hydration whenever possible (it's MUCH more efficient)
    • Don't use magic finders except when writing prototype code
    • Doctrine has very nice caching utilities; Setup a query and a result cache for your queries if you haven't yet (it's very automated and easy to setup: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/caching/en)
    • Remember to join all the relations that you use in your processing code in the DQL query; not doing so will make Doctrine launch a "hidden" query for every access to an unfetched relation.