Magento 1 – speed up product save
In this tutorial I show you how to speed up product save for Magento 1. You may have noticed, that if you call a save() method on a product object it may take some time. If you have many attributes and a lot of products in you database this can last some seconds. If you write a import script it may runs a long time. We will now speed up you product save. See also my Magento 2 speed up product save article.
Magento 1 – speed up product save
I wrote a simple benchmark.php script which you can run from Magento root. It demonstrates the difference between a normal $product->save() and an optimized version. The output looks as follows:
The full code of my benchmark file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php require_once "app/Mage.php"; Mage::app(); $product_id = 4; $start = microtime(true); $action = Mage::getModel('catalog/resource_product_action'); $action->updateAttributes(array($product_id), array( 'name' => 'new product name 1' ), 0); echo "Save attribute duration: ".(microtime(true) - $start)." seconds\n"; $starttime = microtime(true); $product = Mage::getModel('catalog/product')->load($product_id); $product->setName('new product name 2'); $product->save(); unset($product); echo "Save product duration: ".(microtime(true) - $start)." seconds\n"; |
If you want to use it in your own store please change $product_id to a valid id.
The difference is enormously. A normal product save lasts over 10 seconds. This shop has about 100 additional product attributes, more than 10000 products an my benchmark is from an development system. So what’s the difference in code? You my change a product like this:
1 2 3 | $product = Mage::getModel('catalog/product')->load($product_id); $product->setName('new product name 2'); $product->save(); |
You first load a product by id or sku, then you set attribute data by your setData() method. Finally you call save method. But what’s happen now? Magento saves the entire product. That means, that all product attributes are saved in their catalog_product_entity tables. That is a huge load of SQL statements to your database. Finally, a product save also reindex all product specific indexes if your product indexer is set to automatically.
There are 2 methods to speed up this code.
Method 1
This is the most used case. You already loaded a product (or any other object) and you know which attribute you want to change. You set data as always with your setData method (or each specialized setAttribute method). Instead of calling product->save() you need to call saveAttribute() on a resource object. This method needs two params, the product object itself and your attribute code (for example ‘name’).
1 2 3 4 5 | $product = Mage::getModel('catalog/product')->load($product_id); $resource = $product->getResource(); $product->setData($attribue_code, $value); $resource->saveAttribute($product, $attribute_code); |
Depending on how mush attributes you product has, this code is more than 100 times faster! Keep in mind, that a automatic reindex is not done.
Method 2
This is a better option if you want to bulk update product attributes without loading products. This method triggers mass action event from Magento and call reindex. updateAttributes() use three params: an array of product ids, an array of attribute code and value pairs and finally the store id (0 for default).
1 2 3 4 | $action = Mage::getModel('catalog/resource_product_action'); $action->updateAttributes(array($product_id), array( 'name' => 'new product name 1' ), 0); |
Conclusion
It is quite simple to speed up product save in Magento 1. I showed you two different approaches. My benchmark script is useful to check the difference between both solutions. So whenever it is possible, only update single attributes and do not save the complete product.
Thanks for sharing this article, I have tried to optimize it manually but, while updating attributes I got an error, I applied Cloudways full page cache extension. Its working good now and load time is improved.
Page cache is not a solution to speed up product save. I can not recommend most cache extensions, because they are not usable for dynamic content. Most of them lead to displaying wrong data in frontend!
how can we speed things up when saving an image?
after using the addImageToMediaGallery() method ?
Hi! How to save Group Prices using your code? Simple price attribute saves ok, but not group price data.