More efficient way of updating an attribute on a product, Magento

More efficient way of updating an attribute on a product, Magento

October 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
More efficient way of updating an attribute on a product, Magento

Optimizing Product SKU Updates: A More Efficient Approach in Magento

Updating thousands of product SKUs in Magento can be a time-consuming task if not approached correctly. I recently encountered this challenge myself while working on a project for one of our clients at Laravel Company.

The Initial Script

My initial script looked something like this:

if($updates_handle) {
    while($sku_entry=fgetcsv($updates_handle, 1024, ",")) {

        /* get the old and new Product SKU values */
        list($old_sku, $new_sku) = $sku_entry;

        echo 'Setting: '.$old_sku.' SKU to: '.$new_sku.'<br />';

        $product = Mage::getModel('catalog/product')->loadByAttribute('skuref1', $old_sku);

        $product->setSku($new_sku);

//      $product->getResource()->saveAttribute($product, 'Sku');

//       $product->save();

    }
    echo "<br />DONE<br />";
}

The Performance Challenge

The issue with this approach lies in the save() and saveAttribute() functions. As you correctly pointed out, the save() function can take up to 3 seconds per product, which would make the process extremely slow for thousands of SKUs.

I also encountered an error when attempting to use saveAttribute(). This error occurs because Magento expects certain fields like value_id, which are missing in your select statement:

Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_entity.value_id' in 'field list'' in /var/www/website/httpdocs/lib/Zend/Db/Statement/Pdo.php:234

A More Efficient Approach

To optimize this process, we can use Magento’s built-in mechanisms for mass updates. Here are the steps I would recommend:

  1. Use the Mage::getSingleton('catalog/product_action')->updateAttributes() method.

This method allows you to update multiple product attributes in a batch, reducing the number of database queries significantly.

Here’s how you can use it:

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('sku', array('in' => $old_skus));

Mage::getSingleton('catalog/product_action')
    ->updateAttributes(
        $products->getAllIds(),
        array('sku' => $new_skus),
        0
    );

In this code, $old_skus and $new_skus are arrays containing the old and new SKUs respectively. The updateAttributes() method applies the changes in a single database transaction.

  1. Ensure your setup allows for bulk updates.

Before running the script, make sure you have configured Magento to allow mass updates. This setting can be found in System > Configuration > Catalog > Frontend > Allow Mass Actions.

Conclusion

Updating thousands of product SKUs efficiently in Magento requires leveraging the platform’s built-in batch update functionality. The Mage::getSingleton('catalog/product_action')->updateAttributes() method provides a significant performance improvement over individual product saving.

Remember, optimizing database queries and using Magento’s core features is key to maintaining site performance and stability. If you find yourself needing more advanced customizations or integrations, don’t hesitate to reach out to the experts at Laravel Company for professional assistance.

Let me know if you need any further clarification on this process!

Catalog Database Performance Configuration

Our network