Update
I'm looking for the fastest and reliable method for mass attribute update
"Mass attribute update" for attributes or products?
Think updating multiple attributes is alread answered, but for products this can be usefull ...
If you want to update products from collection, you should not do this ...
foreach ($collection as $product) {
$product->setSomeData(...);
# not here
$product->save();
}
This will dispatch events, rebuild pricerules and indices. With this no events (and some other things) are skipped and is lot faster.
foreach ($collection as $product) {
$product->setSomeData(...);
}
$collection->save();
To avoid pricerule updates, you can add ...
$product->setIsMassupdate(true);
To disable/enable reindex on the fly take a look at this ... https://github.com/Flagbit/Magento-ChangeAttributeSet/commit/676f3af77fec880bc64333403675d183e8639fae
/**
* Set indexer modes to manual
*/
private function _storeRealtimeIndexer()
{
$collection = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($collection as $process) {
if($process->getMode() != Mage_Index_Model_Process::MODE_MANUAL){
$this->_index[] = $process->getIndexerCode();
$process->setData('mode', Mage_Index_Model_Process::MODE_MANUAL)->save();
}
}
}
/**
* Restore indexer modes to realtime an reindex product data
*/
private function _restoreRealtimeIndexer()
{
$reindexCodes = array(
'catalog_product_attribute',
'catalog_product_flat'
);
$indexer = Mage::getSingleton('index/indexer');
foreach ($this->_index as $code) {
$process = $indexer->getProcessByCode($code);
if (in_array($code, $reindexCodes)) {
$process->reindexAll();
}
$process->setData('mode', Mage_Index_Model_Process::MODE_REAL_TIME)->save();
}
}
And also flushing cache before mass (product) update can increase performance ...
Mage::app()->getCacheInstance()->flush();
Some numbers from debugging here: https://github.com/Flagbit/Magento-ChangeAttributeSet/issues/16
Mage::getSingleton('catalog/product_action')->updateAttributes(...)
seems not to be the fastest method ... at least not with mutlistore setup and flat tables turned on ...
saveAttribute()
$product = Mage::getModel('catalog/product')->load($productId);
$resource = $product->getResource();
$product->setData($attributeCode, $attributeValue);
$resource->saveAttribute($product, $attributeCode);
- Total Incl. Wall Time (microsec): 437,787 microsecs
- Total Incl. CPU (microsecs): 423,600 microsecs
- Total Incl. MemUse (bytes): 4,433,848 bytes
- Total Incl. PeakMemUse (bytes): 4,395,128 bytes
- Number of Function Calls: 25,711
updateAttributes()
Mage::getSingleton('catalog/product_action')->updateAttributes(
array($productId),
array($attributeCode => $attributeValue),
$storeId
);
- Total Incl. Wall Time (microsec): 3,676,950 microsecs
- Total Incl. CPU (microsecs): 3,122,064 microsecs
- Total Incl. MemUse (bytes): 8,174,792 bytes
- Total Incl. PeakMemUse (bytes): 8,199,192 bytes
- Number of Function Calls: 150,132
updateAttributes()
(resource singleton)
Mage::getResourceSingleton('catalog/product_action')->updateAttributes(
array($productId),
array( $attributeCode => $attributeValue),
$storeId
);
- Total Incl. Wall Time (microsec): 94,155 microsecs
- Total Incl. CPU (microsecs): 48,568 microsecs
- Total Incl. MemUse (bytes): 1,426,304 bytes
- Total Incl. PeakMemUse (bytes): 1,370,456 bytes
- Number of Function Calls: 2,221