简体   繁体   English

如何在Yii中提高CGridView的性能

[英]How to improve CGridView's performance in Yii

I am just curious about the performance of CGridView in Yii Framework. 我只是对Yii Framework中CGridView的性能感到好奇。 I built a simple app with Yii. 我用Yii构建了一个简单的应用程序。 on a Model's admin page, simply listing all records, with pagination (30/page) , and custom sorting, which is working great so far. 在Model的管理页面上,只需列出所有记录,分页(30页/页)和自定义排序,到目前为止效果很好。

But I find that it takes 4-5 seconds to render the CGridView. 但我发现渲染CGridView需要4-5秒。 Is it normal to render a 30 records page in 4-5 secs? 在4-5秒内渲染30个记录页面是否正常? I kind of feel it's slow! 我觉得它很慢! Because it's not a complicated table, with only 8 columns. 因为它不是一个复杂的表,只有8列。 And the SQL query generated is running fast (done in 2-3 ms), although including a few relations. 生成的SQL查询运行速度很快(在2-3毫秒内完成),虽然包含一些关系。

I googled a lot on the optimization, and found this official document: http://www.yiiframework.com/doc/guide/1.1/en/topics.performance 我搜索了很多优化,并找到了这份官方文档: 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. 专注于CGridView问题,我尝试使用缓存,并找到COutputCache这是一个非常好的帮手。

My practice is add COutputCache in the filter() function of the Controller: 我的做法是在Controller的filter()函数中添加COutputCache:

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. 您可以添加普通视图页面和部分页面,以秒为单位设置过期时间,并告诉Yii哪个参数可以检测缓存更改。

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! 低页面渲染时间为40-50毫秒 ,真棒!

But I haven't found a good way to speed up the first time rendering. 但是我没有找到加速第一次渲染的好方法。 Do you have any good suggestions? 你有什么好的建议吗?

在我的情况下,性能低的原因是使用数据列的html类型。

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? 您使用的是CActiveRecord关系吗? how many? 多少? maybe the answer is just to use CDbCriteria.with to do not lazy load the related tables. 也许答案只是使用CDbCriteria.with来不延迟加载相关的表。

您必须检查排序,每当排序字段不是索引时,您将从Yii构建的查询中获得缓慢的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM