During the development of your Magento eCommerce store, performance usually takes a back seat to functionality and design. However, after you’ve installed all of your extensions and have checked the spelling on your last cms page, you’ll want to focus your attention on making your shop as responsive as possible for your customers. Granted, it pays to have a terrific hosting provider that understands how to optimize the server environment, but there are also some things you can do on your end to minimize latency and make your shop run as efficiently as possible.
Tip 1: Tweak your .htaccess file:
Magento’s .htaccess file comes pre-configured with a few tweaks to make your pages load faster, but because hosting environments differ, many of these are commented out or set to low defaults.
1. Turn on output compression
mod_deflate is enabled on all of our servers. This module compresses your text-based files prior to sending them over the network. The customer’s browser then automatically uncompresses them prior to displaying them to the user. To enable this uncomment the appropriate lines as follows:
## enable apache served files compression
# Insert filter on all content
# Insert filter on selected content types only
# 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 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
2. Enable far-future expires
Some content, such as images and css, don’t change all that often. For content such as this, you can improve performance by taking advantage of a web browser’s caching behavior. Setting far-future expires headers will allow web browsers to cache images and other static files so that upon subsequent visits, they won’t have to be re-downloaded.
## Add default Expires header
ExpiresDefault "access plus 1 year"
Tip 2: Refresh your indexes
Magento keeps indexes of some of your data in special tables to make queries more efficient. When you were developing your store, you were adding and deleting products, categories, urls, etc. This activity causes your indexes to have gaps and stale data. When you’re ready to go live, you’ll want to refresh all of your indexes in order to rebuild those tables.
There are two ways to do this: from your Admin dashboard or from the command line. To rebuild your indexes from your Magento Admin panel, just go to System > Index Management, select All, select Reindex Data, and then click Submit.
It’s also possible to rebuild your indexes from the command line using the shell tools included in Magento versions 1.4 and higher. These tools are located in your MAGENTO_ROOT/shell/ directory and can be run with a call to php’s command line interface:
[bash]$ php -f indexer.php help
Usage: php -f indexer.php — [options]
info Show allowed indexers
reindexall Reindex Data by all indexers
help This help
As you can see, the same functionality that can be found in the Admin panel under Index Management can be found here. To rebuild all of your indexes, just enter:
[bash]$ php -f indexer.php reindexall[/bash]
Tip 3: Turn on caching
When you’re developing your site, you probably turned off Magento’s native cache so that you would be able to see your design changes instantly. Now that you’ve finalized your design work, make sure you turn the cache back on. This improves performance 5-10 times over the cache being off.
Ever wonder what gets cached? Here’s an overview:
- Configuration files (xml)
- Layouts (xml)
- HTML blocks for Top Navigation, and Footer
- Translations stored as an array
- Data collections for Website, Store, and Store View
Tip 4: Use the Flat Catalog feature
Magento makes heavy use of the EAV model for storing customer, product, and category data in its database. The main benefit of the EAV model is that it allows for completely extensible attributes for those objects. The main drawback is that those attributes are stored in multiple tables rather than one very large table. This means that SQL queries need to be longer and more complex. Using the Flat Catalog feature creates new tables where each row contains all of the necessary data about a product or category. You can imagine that this makes the SQL queries much more efficient.
The Flat Catalog for Categories should be used for all Magento shops. The Flat Catalog for Products only begins to show a performance benefit once you’ve reached the 1000 SKU point. You can enable both of these in the back end:
- Go to System > Index Management and make sure “Product Flat Data” and “Category Flat Data” are built and ready.
- Go to System > Configuration > Catalog > FrontEnd and select “Yes” for Use Flat Catalog Category and (if desired) Use Flat Catalog Product.
Tip 5: Enable the Mage Compiler
This is a new feature introduced in version 1.3.2.x and bundled with 1.4.x, although it is still listed as “beta”. To understand how this feature can help improve your site’s performance, you have to understand what happens when a browser requests a URL from Magento. By default, and for every request, magento will look in the following directories, in the following order:
This is what gives you, the developer, the wonderful ability to override and extend Magento to your heart’s content. If you want to change or modify some class, you just put your code in app/code/local/ and Magento will find it first and run it instead of the core definition in app/code/core.
As great as this is, it results in a great deal of disk I/O for every request. That’s where the Mage Compiler comes it. It essentially copies all of the class definitions and code found under app/code into a singe directory: includes/src/. It then gets rid of all of the other include paths and replaces them with the one. This way, Magento only needs to search one directory rather than four for each request.
It’s important to compile after you’ve finished installing your modules and developing your custom code if you want this to work properly. This too can be enabled either in the Admin panel or on the command line. In the Magento Admin, go to System > Tools > Compilation, and click Run Compilation Process. To run from the command line, use the following syntax (script is also located in the MAGENTO_ROOT/shell directory):
[php]$ php -f compiler.php help
Usage: php -f compiler.php — [options]
state Show Compilation State
compile Run Compilation Process
clear Disable Compiler include path and Remove compiled files
enable Enable Compiler include path
disable Disable Compiler include path
help This help[/php]
Tip 6: Make use of parallel downloads to speed page rendering
Most web browsers limit the number of concurrent downloads to around 4 for each unique URL found in a web page. If all of your content is coming from the same host, this limitation can introduce unnecessary delays in rendering the page. You can trick the browser into downloading your content in parallel by using Pointer Domains and Magento’s base url settings.
You can do this in your Siteworx control panel under Hosting Features > Domains > Pointer Domains. Create pointer domains called media.example.com, js.example.com, and skin.example.com. Or, you can just create static.example.com. Next, log in to your Magento Admin panel and go to System > Configuration > Web, and change the secure and unsecure base urls for the media, skin, and js options to the pointer domains you just created. Your final configuration should look like the following: