Tuesday, January 28, 2014

Optimizing Magento Performance

Enable Output Compression

This section will turn on the apache mod_deflate module, which compresses text, css, and javascript before it is sent to the browser. This results in a smaller download size. To enable, simply uncomment the appropriate lines so that it looks like the following:
<IfModule mod_deflate.c>

############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip

    # Insert filter on all content
    SetOutputFilter DEFLATE
    # Insert filter on selected content types only
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript

    # Netscape 4.x has some problems...
    BrowserMatch ^Mozilla/4 gzip-only-text/html

    # Netscape 4.06-4.08 have some more problems
    BrowserMatch ^Mozilla/4\.0[678] no-gzip

    # MSIE masquerades as Netscape, but it is fine
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

    # Don't compress images
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary

</IfModule>

Enable Expire Headers

NOTE: This does not work on Litespeed servers.
Browsers use Expires headers to determine how long a page component can be cached. Static components, like images, should have far-future expires headers, but truthfully, all page components should have expires headers. To turn this feature on, just uncomment the appropriate line and add "ExpiresActive On" right above it. See below:
<IfModule mod_expires.c>

############################################
## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires

    ExpiresActive On
    ExpiresDefault "access plus 1 year"

</IfModule>

Disable ETags

ETags are a way for browsers to validate cached components across subsequent visits. They can slow down a site served from a cluster if the cluster hasn't implemented them properly. It is best to just turn them off as follows:

############################################
## If running in cluster environment, uncomment this
## http://developer.yahoo.com/performance/rules.html#etags

    FileETag none

Combine CSS and JS Files

This feature reduces the number of HTTP requests. For versions earlier than 1.4.x, the Fooman_Speedster extension can be used instead.

WARNING WARNING: Combining CSS/JS when using CDN will cause CSS/JS to "break" until the CDN updates completely. It is recommended to perform this process at the beginning off-peak hours, to allow enough time for the "new" CSS/JS to reach the CDN.
  • In the Magento Admin, go to System > Configuration > Developer.
  • Under "Javascript Settings", change "Merge Javascript Files" to YES.
  • Under "CSS Settings", change "Merge CSS Files" to YES.
  • Clear the cache.

Enable Flat Catalog

Magento uses the EAV model to store customer and product data. This enables these objects to be incredibly extensible, but results in longer SQL queries and more reads. Enabling the Flat Catalog for Categories and Products merges product data into one table, thereby improving performance. Generally, all stores should enable Flat Catalog for Categories. Stores with over 1000 products should enable Flat Catalog for Products.
  • In the Magento Admin, go to System > Configuration > Catalog.
  • Under "Frontend", change "Use Flat Catalog Category" to YES.
  • Under "Frontend", change "Use Flat Catalog Product" to YES. (optional)
  • Clear the cache.

Enable the Magento Compiler

WARNING WARNING: Exercise caution when using extensions with the compiler, as some extensions may cause major issues for your site. It is recommended that you use as few extensions as possible to decrease the risk of compromising your site's stability.

Magento's application files are searched for in the following order:

  • app/code/local
  • app/code/community
  • app/code/core
  • lib
This search is performed for every page load, every time, resulting in a lot of filesystem reads. The Mage_Compiler reduces the number of reads by copying all of the application files to a single include directory. It also caches the most frequently used pages.
  • In the Magento Admin, go to System > Tools > Compilation.
  • Click "Run Compilation Process"

Magento Database Maintenance

Magento does many things well, but maintaining an efficient database is not one of them. Having a lot of products is a good reason to have a large database. Sadly, this isn't the only way your database can become big and sluggish. Maintaining the database through log cleaning can result in a dramatic improvement in site performance and latency. This guide explains how to clean up a Magento database that has grown too large for its own good.

NOTE: Before performing any operations on the database, please make a backup.

Log Cleaning

Magento maintains several tables for logging. These tables log things such as customer accesses and which products have been compared. Magento has a mechanism for cleaning these logs regularly, but unfortunately this feature is disabled by default and most customers do not turn it on. There are three ways to clean out these tables: via Log Cleaning in the Magento Admin, via log.php in the ../shell directory, and manually via phpMyAdmin or mysql client.

The following tables are managed by Magento's Log Cleaning function:

log_customer
log_visitor
log_visitor_info
log_url
log_url_info
log_quote
report_viewed_product_index
report_compared_product_index
report_event
catalog_compare_item

Log Cleaning via Admin
  • In the Magento Admin, go to System > Configuration.
  • In the left menu under Advanced click on System.
  • Under "Log Cleaning", change "Enable Log Cleaning" to YES and configure the Save Log for 15 days.

Code Level - Magento Optimization -

  • Calculating the size of an array on each iteration of a loop
  • SQL queries inside a loop
  • Loading the same model multiple times
  • Redundant data set utilization
  • Inefficient memory utilization