Magento 2 – Speed up product save
In my last article about Magento 1 speed up product save, I showed you methods to optimize product save. My new tutorial is about the same topic, but for Magento 2. Magento 2 is a bit different as I showed you different methods on how to get and save products. This is my first attempt to accelerate my import scripts.
Magento 2 – Speed up product save
As I told you in my Magento 1 post, you can speed up save method a 100 times or more. It makes a huge difference if you save the whole product with every single attribute and run an indexer or if you only change the value of some attributes. All periodic scripts should be optimized to extend user experience. I created a benchmark2.php script file that can be run from Magento 2 root directory. This scripts produces the following output:
Here is the full source code of this benchmark script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $product_id = 4; $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id); $start = microtime(true); $productResource = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product'); $product->setName('new product name 1'); $productResource->saveAttribute($product, 'name'); echo "Save attribute duration: ".(microtime(true) - $start)." seconds\n"; $starttime = microtime(true); $product->setName('new product name 2'); $product->save(); echo "Save product duration: ".(microtime(true) - $start)." seconds\n"; |
You can see that are at least two different possibilities how to save a product. The first six lines of code are needed if you want to run a simple PHP script with Magento 2 connection. We include bootstrap.php to get an object manager. In addition we set area code to frontend. This tasks are required for a standalone script.
Before we can test product saving, we have to load a product by id. You need to change this id to a valid one of your shop.
Method 1
This is the optimized version. We set product attributes as already known with setATTRIBUTE() method or with setData() function. But we do not save the whole product. You can get a \Magento\Catalog\Model\ResourceModel\Product object. With it we can call saveAttribute() to only save this changed attribute.
Method 2
This is the “normal” method to save a product. We simply call save() method on our product object. This will save the product with all attached attributes to database and do tasks like create indexes. This is the slowest possible solution, but it is guaranteed, that every changed attribute value is saved.
Conclusion
This article shows how to speed up product save. It is possible to only save single attribute values. This is up to 100 times faster as saving the whole product.
This is a very interesting post about the performance of _updating_ products. Thank you for publishing it, it helped me already. But how about _creating_ products? Is there a shortcut to _create_ products fast and quick?
Yes, it is called FastSimpleImport and is a wrapper for the CSV product import. You can create thousands of products in minutes.
Thanks for the article. But the problem is it still loads the whole product:
$product = $objectManager->get(‘Magento\Catalog\Model\Product’)->load($product_id);