Optimizing Zend Framework and Doctrine applications

January 17, 2009 – 6:10 pm Tags: , , ,

I decided to try profiling my quiz application to see if I could speed it up. Not surprisingly, biggest performance penalties came from loading classes and Doctrine’s ORM magic.

Here are some findings on how to improve performance.


As you may have heard, optimizing without profiling first is a bad idea, as you may think something is slow when something completely different would be better to optimize.

So for the profiling purproses, I configured Xdebug to output cachegrind files, and installed KCachegrind for viewing them.

Zend Framework

One of the main causes here are require_once’s and Zend_Loader. The first thing you should do is to strip require_once’s from ZF’s library, as the example in the ZF manual shows. After running sed, there’s a notable difference in the speed.

If you are running APC (you should be), you can enable apc.include_once_override in your php.ini for some extra speed in file including.

In general, reading the Zend Framework performance guide is recommended, though not all of the optimizations mentioned there had any effect on my application. That was probably because the quiz app is relatively simple in terms of ZF interaction.

It should probably be noted, that according to KCachegrind, Zend_Loader was called 94 times! It could be beneficial to combine some of the classes used on every request into a single big include file.


Quick tricks

There are some built in features in Doctrine which you can enable to increase performance. These are the query and result caches. The query cache holds compiled DQL queries, and the result cache holds query results.

To learn about the query and result caches, see the Doctrine manual, caching chapter.

Other findings

You often hear that it’s better to use Doctrine_Query, than the find-methods on Doctrine_Table. Wrong!

It turns out that if you actually need to fetch the whole record, using the find methods is faster than using a Doctrine_Query! This may not be very surprising, as Doctrine_Query objects require the code to parse the DQL snippets. However, it may still be faster to use Doctrine_Query if you have large objects, but only need some of the data.

Changing the hydration mode for some queries to HYDRATE_ARRAY, so that instead of objects Doctrine returns arrays, didn’t field any significant gains. The biggest time consumer in Doctrine queries seemed to be the generation of SQL clauses from DQL.

In closing

There are many people who say ZF is slow. While it may be true on some level, it also boasts a much bigger feature set than many frameworks. Despite all this, optimizing ZF apps is pretty simple, even if you just follow the guidelines in the ZF manual.

What comes to Doctrine, the most beneficial way (in terms of performance) to optimize it is to not use it, and work with SQL queries yourself. However, it’s often faster to develop with Doctrine. You can also use heavy page-caching to reduce the need for calling the ORM magics, which would remove most of the penalties incurred.

There’s a good post on page-caching in PadrĂ¡ic Brady’s blog which is worth a read.

Share this:
  1. 13 Responses to “Optimizing Zend Framework and Doctrine applications”

  2. i also have noticed an increase in speed with ZF after following the manual performance guide.

    And even though its still a bit slow i think all the benefits you get by developing with ZF makes up for the speed.

    By solomongaby on Jan 17, 2009

  3. Another useful method to take a look at is Doctrine::compile() http://www.doctrine-project.org/Doctrine/1_1#method_compile (Or the Doctrine_Compiler class).

    By Vincent on Jan 17, 2009

  4. Any idea whether wrapping an require inside a class_exists(‘Classname’, false) is better or worse than a require_once?

    By Bryan on Jan 28, 2009

  5. How did you get Zend, doctrine and apc working?


    By cdub on Sep 2, 2009

  6. how do you get zend and apc to work together? Everything I read says they are not compatible. I would love to get them to play nicely together. I need APC, but our vps by default runs Zend. Using both would be ideal.

    By Jen on Nov 3, 2009

  7. JH: If you are running APC (you should be), you can enable apc.include_once_override in your php.ini for some extra speed in file including.

    ME: Be careful about this setting. It can also cause pages to fail. Although the description of that setting is very helpful for #ZF apps, it caused an app of mine to crash. Turning it off fixed it. And there’s even a comment in the PHP manual about problems with it.

    By Joe Devon on Apr 2, 2010

  8. Doctrine manual suggests the combination of multiple doctrine model files into one. How would you set this up and would lazy loading still work? Any examples of that?

    By webo on Nov 12, 2010

  9. If you require customized loading logic, such as in a case where you have concatenated your files into one, you probably would need to require_once the file manually.

    If you use APC’s require optimization and caching, I’m not sure whether you will get any benefit from putting multiple classes into a single file, though.

    By Jani Hartikainen on Nov 12, 2010

  1. 5 Trackback(s)

  2. May 16, 2009: Doctrine vs. Propel | CodeUtopia
  3. Nov 26, 2009: Integrating Zend Framework with Doctrine ORM | Kingdom of Roi
  4. Dec 5, 2009: Integrating Zend Framework with Doctrine ORM « Kartaca
  5. Dec 5, 2009: Kartaca » Integrating Zend Framework with Doctrine ORM
  6. Mar 6, 2010: Integrating Zend Framework with Doctrine ORM « Kartaca

Post a Comment

You can use some HTML (a, em, strong, etc.). If you want to post code, use <pre lang="PHP">code here</pre> (you can replace PHP with the language you are posting)