Jinzhao Huo
Jinzhao Huo

Reputation: 857

How to improve CGridView's performance in Yii

I am just curious about the performance of CGridView in Yii Framework. I built a simple app with Yii. on a Model's admin page, simply listing all records, with pagination (30/page) , and custom sorting, which is working great so far.

But I find that it takes 4-5 seconds to render the CGridView. Is it normal to render a 30 records page in 4-5 secs? I kind of feel it's slow! Because it's not a complicated table, with only 8 columns. And the SQL query generated is running fast (done in 2-3 ms), although including a few relations.

I googled a lot on the optimization, and found this official document: http://www.yiiframework.com/doc/guide/1.1/en/topics.performance

Focus on the CGridView problem, I tried to use cache, and find COutputCache which is a really good helper.

My practice is add COutputCache in the filter() function of the Controller:

public function filters()
{
    return array(
        'accessControl', // perform access control for CRUD operations
        array(  'COutputCache+admin,_lists',   'duration' => 600,'varyByParam'=>array('sort','page'),     ),
    );
}

You could add normal view pages and also the partial page, set the expire time in seconds, and tell Yii which parameter to detect the cache change.

And this helps a lot when you re-visit the page, including sorting and paging. Low down page render time to 40-50 ms which is awesome!

But I haven't found a good way to speed up the first time rendering. Do you have any good suggestions?

Upvotes: 3

Views: 3383

Answers (3)

DiegoCoderPlus
DiegoCoderPlus

Reputation: 770

You´ve got to check the sorting, whenever the sorting field is not an index you will get slow results from the query built by Yii

Upvotes: 0

verybadbug
verybadbug

Reputation: 929

In my case reason of low performance is use html type for data columns.

Upvotes: 1

Chux
Chux

Reputation: 1227

First, display your log in your webpage to see what is going on so we can help.

Second, no, it is not normal. ¿What server are you on? Are you using CActiveRecord relations? how many? maybe the answer is just to use CDbCriteria.with to do not lazy load the related tables.

Upvotes: 0

Related Questions