# Cache Zend_Db_Table MetaData with Zend_Cache


In this afternoon, my boss asked me to improve performance of project to release to client before Lunar new year holidays. The problem is: we have too many DESCRIBE query on every page load. I Google-ed & found the [Lysender](http://lysender.co.cc)'s solution, simple & easy to fix this problem:

> When you are using Zend\_Db\_Table (I sometimes only used Zend\_Db) you will notice that when you try to retrieve data from the database, the first query called is to DESCRIBE the table. Zend\_Db\_Table uses the information on DESCRIBE query to do its magic on SELECT.
> 
> As I have profiled my queries, I noticed that DESCRIBE query is the longest query (in most cases) which mean a big overhead over you retrieval operation. You have two options:
> 
> 1. Don’t use Zend\_Db\_Table (go for Zend\_Db)
> 2. Cache the MetaData
> 
> On this post, I’ll use the caching of MetaData. On your bootstrap file, put this piece of code:
> 
> 	//Caching
> 	$frontendOptions = array(
> 					'lifetime'				 => 25200,
> 	    				'automatic\_serialization' => true
> 	    				);
> 	$backendOptions  = array(
> 	   					 'cache\_dir'  => APPLICATION\_PATH . '/tmp'
> 	    				);
> 	$cache = Zend\_Cache::factory(
> 				'Core',
> 	                        'File',
> 	                        $frontendOptions,
> 	                        $backendOptions
> 	                    );
> 	//Cache table metadata
> 	Zend\_Db\_Table\_Abstract::setDefaultMetadataCache($cache);
> 
> The code simply instructs Zend\_Db\_Table\_Abstract to cache the result of DESCRIBE statement so that next time you retrieve data, the cache is used instead of repeatedly describing tables over and over again.
> 
> (But I noticed that INSERT and UPDATE statements don’t have DESCRIBE…)
> 
> After several tries with this caching, you will see files on the tmp folder like this: zend\_cache—internal-metadatas—5e7576e3cd79114d46850714e998a3b0.
> 
> Still there is an overhead on reading the cache file. If you want better caching, use memory based caching like MemCache.

Source: [http://lysender.co.cc/2009/03/zend-framework-optimization-tips](http://lysender.co.cc/2009/03/zend-framework-optimization-tips)

