O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
As described in the Caching section, <strong>Yii</strong> provides several caching solutions that may<br />
improve the performance of a Web application significantly. If the generation of some data<br />
takes long time, we can use the data caching approach to reduce the data generation<br />
frequency; If a portion of page remains relatively static, we can use the fragment caching<br />
approach to reduce its rendering frequency; If a whole page remains relative static, we can<br />
use the page caching approach to save the rendering cost for the whole page.<br />
If the application is using Active Record, we should turn on the schema caching to save<br />
the time of parsing database schema. This can be <strong>do</strong>ne by configuring the<br />
CDbConnection::schemaCachingDuration property to be a value greater than 0.<br />
Besides these application-level caching techniques, we can also use server-level caching<br />
solutions to boost the application performance. As a matter of fact, the APC caching we<br />
described earlier belongs to this category. There are other server techniques, such as<br />
Zend Optimizer, eAccelerator, Squid, to name a few.<br />
Database Optimization<br />
Fetching data from database is often the main performance bottleneck in a Web<br />
application. Although using caching may alleviate the performance hit, it <strong>do</strong>es not fully<br />
solve the problem. When the database contains enormous data and the cached data is<br />
invalid, fetching the latest data could be prohibitively expensive without proper database<br />
and query design.<br />
Design index wisely in a database. Indexing can make SELECT queries much faster, but it<br />
may slow <strong>do</strong>wnINSERT, UPDATE or DELETE queries.<br />
For complex queries, it is recommended to create a database view for it instead of issuing<br />
the queries inside the PHP code and asking DBMS to parse them repetitively.<br />
Do not overuse Active Record. Although Active Record is good at modeling data in an<br />
OOP fashion, it actually degrades performance due to the fact that it needs to create one<br />
or several objects to represent each row of query result. For data intensive applications,<br />
using DAO or database APIs at lower level could be a better choice.<br />
Last but not least, use LIMIT in your SELECT queries. This avoids fetching overwhelming<br />
data from database and exhausting the memory allocated to PHP.<br />
Minimizing Script Files<br />
Complex pages often need to include many external JavaScript and CSS files. Because<br />
each file would cause one extra round trip to the server and back, we should minimize the<br />
number of script files by merging them into fewer ones. We should also consider reducing<br />
the size of each script file to reduce the network transmission time. There are many tools<br />
around to help on these two aspects.<br />
For a page generated by <strong>Yii</strong>, chances are that some script files are rendered by<br />
components that we <strong>do</strong> not want to modify (e.g. <strong>Yii</strong> core components, third-party<br />
components). In order to minimizing these script files, we need two steps.