The performance of network applications is affected by many factors. Database access, file system operations, network bandwidth, and so on are potential influencing factors. Yii has reduced the performance impact of the framework in various aspects. However, there are still many applications...
The performance of network applications is affected by many factors. Database access, file system operations, network bandwidth, and so on are potential influencing factors. Yii has reduced the performance impact of the framework in various aspects. However, there are still many improvements in user applications to improve performance.
1. enable APC extension
Enabling php apc extension may be the easiest way to improve the overall performance of an application. This extension caches and optimizes PHP intermediate code and avoids the time required to parse the PHP script for each new request.
2. disable debugging mode
Disabling the debug mode is another easy way to improve performance. If constantYII_DEBUG
It is determined to be true, and the Yii application will run in debug mode. Debugging Mode is useful in the development phase, but it affects performance because some components cause additional system overhead. For example, the message logger records additional debugging information for the recorded information.
3. use
yiilite.php
When php apc extension is enabledyii.php
Replace with another nameyiilite.php
To further improve the performance of the Yii-powered application.
Fileyiilite.php
Included in each Yii release. It is a collection of commonly used Yii files. In the file, annotations and trace statements are removed. Thereforeyiilite.php
This reduces the number of referenced files and avoids the execution of trace statements.
Note: Useyiilite.php
Without APC enabled, the performance will be reduced becauseyiilite.php
Contains some classes not required for each request, which will take additional parsing time. At the same time, you should also note that you can useyiilite.php
It will be slower, even if APC is opened. It is best to usehello world
Run a benchmark program to determine whether to useyiilite.php
.
4. use cache technology
As described in the cache section, Yii provides several caching solutions that can effectively improve performance. If it takes a long time to generate some data, we can use the data cache method to reduce the frequency of data generation. if part of the page remains relatively fixed, we can use the fragment cache method to reduce the rendering frequency. if a whole page remains relatively fixed, we can use the page cache method to save the page rendering costs.
If the application is using Active Record, we should open the data structure cache to save time for parsing the data table structure. You can set the CDbConnection: schemaCachingDuration attribute to a value greater than 0.
In addition to these application-level cache technologies, we can also use service-level cache solutions to improve application performance. In fact, the php apc cache we described previously belongs to this item. There are also other server technologies, such as Zend Optimizer, eAccelerator, Squid, and others are not listed one by one.
5. database optimization
Retrieving data from a database is often a major bottleneck for network applications. Although using cache can reduce performance loss, it cannot solve the fundamental problem. When the database package contains a large amount of data and the cached data is invalid, without a good database and query optimization design, obtaining the latest data will be very resource-consuming.
Design indexes intelligently in a database. Index allowsSELECT
Faster query, but it will makeINSERT
,UPDATE
OrDELETE
Slow query.
For complex queries, we recommend that you create a database view for it, instead of generating query statements using PHP code so that DBMS can parse them repeatedly.
Do not abuse Active Record. Although Active Record is good at modeling data in an OOP style, it actually creates one or several pairs for it to represent each query result, reducing the performance. For data-intensive applications, using DAO or database interfaces at the underlying layer is a better choice.
Last but not least, in yourSELECT
Used in queryLIMIT
. This will avoid extracting too much data from the database and consuming the memory allocated for PHP.
6. minimize script files
Complex pages often need to introduce many external JavaScript and CSS files. Because each file will cause an additional round-trip, we should minimize the number of script files through joint files. We should also consider reducing the size of each script file to reduce the network transmission time. There are many tools to help improve these two aspects.
For a page generated by Yii, the exception is some script files rendered by components which we do not want to change (such as Yii core components and third-party components ). To minimize these script files, we need two steps.
Note:The following describesscriptMap
Features are supported since version 1.0.3.
First, the script is minimized by configuring the scriptMap attribute of the clientScript application component. It can be completed in application configuration or in code. For example,
$cs=Yii::app()->clientScript;$cs->scriptMap=array( 'jquery.js'=>'/js/all.js', 'jquery.ajaxqueue.js'=>'/js/all.js', 'jquery.metadata.js'=>'/js/all.js', ......);
The above code maps these JavaScript files to URLs./js/all.js
. If any of these JavaScript files needs to be introduced by some components, Yii will introduce this URL (once) instead of individual script files.
Second, we need to use some tools to combine (and compress) JavaScript files into a separate file and save itjs/all.js
.
The same technique applies to CSS files.
With the help of Google AJAX Libraries API, we can improve the page loading speed. For example, we can introducejquery.js
Instead of from our own server. To do this, we first configurescriptMap
As follows,
$cs=Yii::app()->clientScript;$cs->scriptMap=array( 'jquery.js'=>false, 'jquery.ajaxqueue.js'=>false, 'jquery.metadata.js'=>false, ......);
By ing (map) these script files are false, we prevent Yii from generating code to introduce these files. As an alternative, we write the following code on the page to directly introduce files from Google,
......
The above is the Yii Framework official guide series 52-topic: performance adjustment content. For more information, see PHP Chinese network (www.php1.cn )!