Fastest way to update an attribute in all products

Hi Magento provide an attribute by below code

$product->setAttributeCode($newValue)
$ProductObject->getResource()->saveAttribute($product, 'attribute_Code');

Example:

$product=Mage::getModel('catalog/product')->load($id);
$product->setSpecialFromDate('2010-10-28');
// below code use for time format 
$product->setSpecialFromDateIsFormated(true);
$product->getResource()->saveAttribute($product, 'special_from_date');

Using Product Collection:

$productIds = array(1,3,2);
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $productIds));

foreach($products as $product)
{
    $product->setSpecialFromDate('2010-10-28');
    // below code use for time format 
    $product->setSpecialFromDateIsFormated(true);
    $product->getResource()->saveAttribute($product, 'special_from_date');


}

Write an SQL Query.

Second best way (and the one I recommend): \Mage_Catalog_Model_Resource_Product_Action::updateAttributes

Code example by u_maxx:

$allProductIds = Mage::getModel('catalog/product')->getCollection()->getAllIds();
$attributeCode = 'some_eav_attribute';
$attributeValue = NULL;
$attrData = array( $attributeCode => $attributeValue );
$storeId = 0;
// no reindex:<
Mage::getSingleton('catalog/resource_product_action')->updateAttributes($allPro‌​ductIds, $attrData, $storeId);
// reindex:
Mage::getModel('catalog/product_action')->updateAttributes($allPro‌​ductIds, $attrData, $storeId);

How would this work with multiselect values? Does it 'add' the value or overwrite other pre-selected values

Multiselects are normally saved as comma seperated integers, like 27,42,4711. Therefore if you change the value to say 1, the other values are lost. But what you can do is something like CONCAT(value, ',1') means adding the new value to the end, seperated by a comma. I think even if you add a value two times, this is no problem, because Magento filters it the next time the item is saved and ignores the second value.


10k products, use SQL. EAV just muddies the waters. Per question is Fastest way.

Find attribute_id

SELECT * FROM eav_attribute where entity_type_id = 4 and attribute_code = 'YOUR_ATTRIBUTE_CODE_HERE'

Update with found attribute_id from above query.

UPDATE catalog_product_entity_int SET value = 1 WHERE attribute_id = FOUND_ATTRIBUTE_ID

I suppose you could do a subselect with update but for simplicities sake two SQL queries is easiest to grasp.

NOTE: entity_type_id = 4 is most always the product EAV entries. In case you are needing to mass update category or customer attributes this will differ as well as the table that is updated based on what type of attribute it is your updating. Also if you have multi-store setup be sure and note and condition store_id