5. Content compression
Content compression is an easy and efficient way to save bandwidth and hence speed up your website. Gzip and deflate are the two techniques used for content compression. In CodeIgniter you can do it by simply setting “compress_output” variable to true in config.php file.
$config['compress_output'] = TRUE;
You can also do it by using your htaccess file. Paste the following lines of code in your htaccess file.
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
If you want to use deflate mechanism, add the following lines of code to your htaccess file
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0 no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
6. Database queries optimization
CodeIgniter runs on MySQL or PostgreSQL database. We can use CodeIgniter’s built-in query caching for caching long-running or arduous queries. The sample syntax is given below. Simple turn db->cache_on before a query and turn db->cache_off after the query returns results.
// Turn caching on
$query = $this->db->query("SELECT * FROM mytable");
// Turn caching off for this one query
$query = $this->db->query("SELECT * FROM members WHERE member_id = '$current_user'");
// Turn caching back on
$query = $this->db->query("SELECT * FROM another_table");
You will still better look into all the SQL queries and find out the time each query takes to fetch the result. For SQL optimization, you should setup slow query log through your web host. You can also check current queries through command
Or you can manually test the queries in phpMyAdmin or any database management tool which will show you the time taken by the query to fetch result. If query is fetching too many records, you can lazy load those records, i.e. fetch few records that display on top of the page at the time of page loading while those that appear at the bottom via ajax request. This way your initial page loading time will be decreased. You can also use pagination for this purpose.
7. Reduce server response time
Time taken by the server to respond to a browser request is called server response time. The server response time plays a very significant role in the CodeIgniter performance optimization. If you apply all the optimization techniques to your website but the server response time is not good, you will not get the desired results.
Various steps should be taken to reduce server response time, these include using better web hosting services, using less server resources on the website or buying a dedicated server setup such as cloud or VPS. Some hostings such as Amazon Lightsail come with a pre-configured apache mod_pagespeed. That can be helpful in serving contents faster.
8. Upgrade CodeIgniter version
This may be an extreme option but upgrading your CodeIgniter version as well as PHP version can be a good avenue of performance optimization. Please note that if you upgrade PHP to 5.4+, CodeIgniter 2.x may stop working. For that, please plan accordingly on a staging server and discuss before flipping the switch.