7

我目前正在使用 Zend 框架 1.11.11 和 Doctrine 2.2 构建一个 Web 应用程序。我搜索了很多优化所用框架的最佳方法和技巧。

在我搜索 Zend 框架优化的过程中,我偶然发现了这篇文章: 优化 Zend 框架

有趣的是,它提到了一些从框架本身的使用中出现的优化技术,并且在手册本身中没有涉及,例如:

  • 禁用 viewRenderer 并自己处理视图渲染。

现在我正在寻找可以应用于 Doctrine 2 的类似技术,而不是手册中提到的那些。

笔记

我知道很多人会提到分析查询、缓存和查找应用程序的瓶颈,但这不是我们要寻找的 :) 我正在寻找在 Doctrine 2 使用期间发现的实用增强功能。

谢谢 ;)

4

1 回答 1

11

Well, optimizing and hacking Doctrine 2 will not be an easy task. I can only advise you to follow the official "best practice":

  • Use Query Cache
  • Use Metadata Cache
  • Pre-generate proxies
  • Avoid listeners (or merge them by event type (flush, update), it will avoid the lookup time and the loop for subscribed events)
  • Use lazy-loading whenever it is possible
  • Make sure your relationships or inheritance are not messed up

(Note that I didn't mentioned Result cache which should not be a way to optimize an application)

From my use, the most important part I had to optimize was not Doctrine itself (while there are optimizations to do to the core) but the generated Query, as always, I EXPLAINed the queries and optimized indexes.

Doctrine 2 can be high memory consuming so loading a lot of entities at once may slow down your application, you may find it useful to known about clear(), detach(), iterate() methods.

Despite the fact Doctrine 2 can sometimes be slow, I mostly noticed that I was able to optimize application somewhere else, within the Zend Framework or PHP themselves.

Let's say, Doctrine 2 takes 100ms where Zend Framework takes 300ms for a total of 450ms (I/O stuffs, PHP internal functions, etc..)

If you can divide easily by two the time taken by Zend Framework, optimizing Doctrine 2 to gain like 10% will not increase notably the speed of your application. Think about it twice.

Here are a few tips:

  • Create your own view instead of using View Helpers (avoid the helper lookup)
  • Cache your Zend_Config object (really heavy load)
  • Avoid Regex routes whenever possible (ZF routes are a big bottleneck)
  • Use a ClassMap autoloader instead of the native Zend_Loader_Autoloader

There are tons of optimization to do, some have a real impact while others don't.

Make sure to find them by profiling your application, an easy and cross-platform is to use webgrind.

于 2012-05-10T12:04:09.977 回答