Archive for February, 2010

Cache Zend_Db_Table MetaData with Zend_Cache

Thursday, February 11th, 2010

In this afternoon, my boss asked me to improving performance of project to release to client before we have Lunar new year holidays. The problem has too many DESCRIBE queries on every page load. I Google-ed & found the Lysender‘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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	//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

Overtime

Monday, February 1st, 2010

Lâu rồi mới phải làm overtime (OT) thế này. Hôm thứ 7 vừa rồi phải ngồi lại cty đến hơn 12h đêm mới về :( Rồi lại đưa chị Thủy “tét” (vs với con dở Thủy top :D ) về nhà cho an tòan, về đến nhà cũng hơn 1h sáng rồi. Log effort 14h mới kinh :”>

Nói chung là nếu hạn chế được overtime là tốt nhất, dĩ nhiên rồi. Nhưng thỉnh thoảng có 1 vài hôm phải OT cũng hay, vì nó phần nào cũng chỉ thêm được cho chúng ta biết, team chúng ta làm việc với nhau như thế nào, hỗ trợ nhau ra sao…

Tự nhiên thấy mệt mỏi, mệt mỏi vì cứ phải gặp những kẻ lưỡi dẻo & dài, những kẻ khoái “nổ”, những kẻ ăn có nói không… haizz…