Speeding up project using Zend_Db

<p>An existing project realized with the Zend Framework should be optimized in terms of performance. The use of Zend_Db slows down the database access because of its automatic table-scanning and overhead. The SQL-queries as well as the table structures are already optimized and should not be changed. The configuration on the server environment:</p><ul><li>PHP 5.2.6</li><li>MySQL Server Version: 5.0.75</li><li>Zend Framework 1.5</li><li>Apache 2.6.28-16</li><li>Suhosin-Patch</li></ul><p>The update to a newer version of Zend Framework is right now not possible because of some migration problems. The changes in the code should be measurable and viewable with XDEBUG/kcachegrind.</p>
2 answers

speeding up project using zend optimizer+

obviously you are using the zend framework for your application. zend also offers a free application stack including the zend framework, called 'zend server community edition'. the zend server includes a number of useful tools, one of them is the zend optimizer+.
the zend optimizer+ can be used to increase the overall performance of your application without any application changes. this does of course not directly address your performance problemes because of you database access, but it may make the performance issues acceptable because of the improved overall performance. using the optimizer and zend_cache as described in the other solution may be a viable way to solve your problem.



Using cache to speed up Zend_Db

The Zend Framework offers a class named Zend_Cache. One can use several different kinds of caching-methods. For example:

  • File
  • Sqlite
  • Memcached
  • APC
  • Xcache
  • ZendPlatform
  • TwoLevels
  • ZendServer_Disk/ShMem

Because of the defined goal to speed up Zend_Db we have to concentrate on the database activities. To use the magic methods of the database-class, this class has to scan the tables to get the structure to work with. With large tables this can be very costly. In picture 1 one can see in the bottom left time costs of over 2 million. Our goal is to save the structures (metadata) of the tables in the cache so the class doesn't always have to scan the whole table on every query. File-cacheUsing files as a cache may not be the fastest (to be exactly: the slowest) but the easiest way to optimize the database access. You just have to add the following code to your bootstrap.php: $frontendOptions = array(    'automatic_serialization' => true);$backendOptions = array(    'cache_dir' => '../cache/');$dbCache = Zend_Cache::factory('Core',                             'File',                             $frontendOptions,                             $backendOptions);Zend_Db_Table_Abstract::setDefaultMetadataCache($dbCache);      In picture 2 the impacts of this little piece of code can be seen. The time costs are reduced to a little over 1,3 million. As said before, file is the slowest of all optimizations. With the use of Memcached (a memcached server) the costs can be further reduced.