i'm looking on performance (server load time) of magento site and i'm trying to tune search result pages. I realized that when I disabled all heavy things like top navigation, lev layered navigation and product listing and I cleared all cache then after this magento core does like 60 SQL queries agains a database. Does anyone have any procedure how to rid of them or how to reduce them to some acceptable amount?
Also can I somehow reduce a time spent during creating of blocks?
Thank you very much,
Jaro.
Magento is a extremely flexible ecommerce framework, but that flexibility comes with a price: performance. This answer is a collection of pointers and some details on caching (especially for blocks).
One thing to consider is the Magento environment, e.g. tuning the php, the web server (favor nginx over Apache), and MySQL. Also, set up a good caching backend for Magento. All these are covered e.g. in the Magento Performance Whitepaper that applies also to the CE.
After the environment is set up, the other side of things is the code.
Reducing the number of queries is possible for some pages by enabling the flat table catalog (System > Configuration > Catalog > Frontend), but you will always have a high number of queries.
You also can't really reduce the time spent creating the blocks except by tuning the environment (APC, memory, CPU). So as the other commenters said, your best choice is utilizing the caching functionality that Magento has built in.
Magento Block Caching
Because you specifically mentioned blocks in the question, I'll elaborate a bit more on block caching. Block caching is governed by three properties:
- cache_lifetime
- cache_key
- cache_tags
All these properties can be set in the _construct()
method of a block using setData() or magic setters, or by implementing the associated getter methods (getCacheLifetime()
, getCacheKey()
, getCacheTags()
).
The cache_lifetime is specified in (integer) seconds. If it is set to false
(boolean), the block will be cached for ever (no expiry). If it is set to null
the block will not be cached (this is the default in Mage_Core_Block_Abstract
).
The cache_key is the unique string that is used to identify the cache record in the cache pool. By default it is constructed from the array returned by the method getCacheKeyInfo()
.
// Mage_Core_Block_Abstract
public function getCacheKeyInfo()
{
return array(
$this->getNameInLayout()
);
}
public function getCacheKey()
{
if ($this->hasData('cache_key')) {
return $this->getData('cache_key');
}
/**
* don't prevent recalculation by saving generated cache key
* because of ability to render single block instance with different data
*/
$key = $this->getCacheKeyInfo();
//ksort($key); // ignore order
$key = array_values($key); // ignore array keys
$key = implode('|', $key);
$key = sha1($key);
return $key;
}
The best way to customize the cache key in custom blocks is to override the getCacheKeyInfo()
method and add the data that you need to uniquely identify the cached block as needed.
For example, in order to cache a different version of a block depending on the customer group you could do:
public function getCacheKeyInfo()
{
$info = parent::getCacheKeyInfo();
$info[] = Mage::getSingleton('customer/session')->getCustomerGroupId()
return $info;
}
The cache_tags are an array that enable cache segmentation. You can delete sections of the cache matching one or more tags only.
In the admin interface under System > Cache Management you can see a couple of the default cache tags that are available (e.g. BLOCK_HTML, CONFIG, ...). You can use custom cache tags, too, simply by specifying them.
This is part of the Zend_Cache
implementation, and needs to be customized far less frequently compared to the cache_lifetime
and the cache_key
.
Other Caching
Besides blocks Magento caches many other things (collection data, configuration, ...).
You can cache your own data using Mage::app()->saveCache()
, Mage::app()->loadCache()
, Mage::app()->cleanCache()
and Mage::app()->removeCache()
. Please look in Mage_Core_Model_App
for details on these methods, they are rather straight forward.
You will also want to use a full page cache module. If you are using the Magento EE, you already have one. Otherwise search Magento Connect - there are many options (commercial).
Some of those modules also tune various parts of Magento for you beyond the full page caching aspect, e.g. Nitrogento (commercial).
Using a reverse proxy like Varnish is also very beneficial.
There are quite a number of blog posts on this subject. Here is one post by the publishers of the Nitrogento extension.
If you are running Magento on a more low-scale environment, check out my post on the optimization of the file cache backend on magebase.com.
This thread is old but very useful.
I am adding additional comments for speed:
- Instead of using Apache use nginx or litespeed.
- Make sure flat catalog is used.
- If possible use FPC.
- compiler mode to be set on.
- Merge css and js(Fooman Speedster ).
- Use image sprites to reduce number of request.
- Use query cache but avoid size greater then 64 MB.
- Remove all modules not in use by removing there xml.Just disabling will not do.
- Session to be on Ram.
- Use of APC recommended.
- Your cron should be run in offpeak hours.
- Delete additional stores if not in use.
- Delete cart rules if not in use.
- optimize image for size.
- Use Ajax where ever possible.
- CMS blocks take more time then a magento block so unless you want a block to be modified do not use CMS blocks.
- Do not use collection count use collection getSize to get what is the collection size.
- Minimize number of searchable attributes as these result in columns in flat catalog table and will slow down your search.
- Use of Solr search is recommended. It comes with EE version but it can be installed with CE as well.
- Minimize customer group as suggested in comment.
- Enable compression in .htaccess (mod_gzip for Apache 1.3, mod_deflate for Apache 2)
- Remove staging stores if on EE.
- Use Apache mod_expires and be sure to set how long files should be cached.In case you are on Apache server.
- Use a Content Delivery Network (CDN).
- Enable Apache KeepAlives.
- Make your output W3C compliant
- Use of getChildHtml('childName') is recommended as this will cache block against direct use of block code in .phtml file.
- Make sure cron is run so as to clean logs stored in data base.
- Number of days log should be minimized as per requirement.
- Load cache on RAM if memory permits.
- Reduce hard disc file reads and try reads from ram as this is faster.
- Upgrade PHP version to above 5.3
- If on EE make sure that most pages are delivered without application initialization.Even if one container needs application initialization its going to effect execution speed as apart form URL rewrites most of the other code will get executed.
- Check XML for blocks placed in default handle and if those blocks not on specific page then move those XML values from default handle to specific handles.It has been observed that lots of blocks are executed that are not displayed.
- If using FPC make sure your containers are cached and repeat request for container is delivered via cache.Improper placeholder definition results in container cache not being used but each time new container content getting generated.
- Analyze page blocks and variables and if possible add those variables/blocks to cache.
- Switch off Logs writing in Magento.
- Remove Admin notification module.
- Use of image sprites.
- Use some web test tool to analyse number of requests and other html related parameters responsible for download time and act accordingly.
- Remove attributes if not needed.With proper care we can even remove system attributes if not in use.
42: If on enterprise make sure partial indexing is effectively used.
- Write your own solr search populate to bypass Magento search indexing.
- Clean _cl tables or reduce _cl table rows.
- I would add into list: try to avoid file cache if possible, replace it by apc / redis / memcache( As suggested by Jaro)
- Remove system attributes not in use( Be careful,do a thorough check before removing).
- There are some cron tab jobs that are not applicable to all stores so depending on your store features those can be removed.
- Optimization by proper attribute management like setting required attribute to yes or is searchable or required in listing etc.
- Some observers are not required for all stores so in case those observers are not applicable to a specific Magento site then they should be removed.
- Make sure FPC is applicable to most of the site pages. Specially when you added some new controllers to delivering a page.
- Magento has lots of features.For this it has many events and associated observers.There are few features that are not used by a store so any observer related to that feature should be removed.e.g : If you check enterprise version there is category permission concept which if not used, then its recommended that on save after events permission related observers to be removed.
- If a specific attribute is to be save for a product then instead of call $product->save call a function that will save specific attribute.
- In EE version that has partial indexing and triggers modify triggers to avoid multiple entries to_cl tables.
- No.phtml files bypasses blocks and use modules or resources directly.As this will result in no caching which in-turn means more work for Magento.
- Delivering images depending on device in use.
- Some of the FPC recommended for community : Lesti( Free as on date ),Amasty( commercial),extender(commercial ) and Bolt(commercial).
- Warming Cache.
- Controlling bots by .htaccess during peak hrs.
- Pre-populating values in a custom table for Layered Navigation via a custom script that executes daily by cron.
- Making sure to avoid unwanted Keys to reduce cache size.
- Using a higher PHP version 5.4+
- Using a higher Mysql version( 5.5 +)
- Reduce number of Dom elements.
- Move all js out from html pages.
- Remove commented html.
- Modify triggers if on enterprise version(1.13 or higher) so as to reduct _cl table entries.As these entries results in cache flushing which in turn results in lower cache hit,hence more TTFB time.
- Use Magmi to import products.
- Some important links :
--http://www.oscprofessionals.com/blog/understanding-full-page-cache-concept-magento/ and
--http://dionbeetson.blogspot.com.au/2014/11/magento-performance-tips-for-scalability.html