Josh Pennington
Josh Pennington

Reputation: 6408

Saving In Magento Taking A Very Very Long Time

In Magento I write a number of small command line scripts to do things like set a new attribute on a number of products. I am finding that the time it takes to update 900 products takes about 6 hours to complete.

The time it takes to load the individual products goes as fast as I would except, but the act of saving once I have made the change takes a very long time.

I am attaching how I am loading the products in case there is something I can do to better optimize the process. Any help here would be greatly appreciated.

$product = Mage::getModel('catalog/product')->load($magento_id);
$product->setMadeInUsa(1);
try {
    $product->save();
} catch(Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
}

The code runs without error, but it takes forever.

Upvotes: 3

Views: 3856

Answers (2)

Josh Pennington
Josh Pennington

Reputation: 6408

Mage::getSingleton('catalog/product_action')
            ->updateAttributes(array($product->getId()), $attributesData, $storeId);

This code only updates the attributes you want to change. The first paramater is an array of product IDs, the second is an array of attribute names and values, and then the third is the store ID you wish to update.

This is MUCH faster than saving the entire model.

Upvotes: 4

macki
macki

Reputation: 365

Try first seting indexing to Manual and then reindex after update is done. This should improve the performance. However the ultimate solution, if you are going to do the import often, is to follow the code ideas you can find in update attributes mass action, which is optimized for saving many products at once.

Upvotes: 1

Related Questions