Load product attributes more performant

Magento - Load product attributes more performant
Recently I had performance issues when importing several ten tausend products. The problem was, that I always used the “normal” product entity model to load attribute values. The following snippet shows, how to load product attributes directly instead. Therefore I use the product resource singleton.

<?php
$iProductId = $aChange['pid'];
$oItem = new Varien_Object;

/**
 * The old way: Baaaaaad and slow!!!
 **/
$oProduct = Mage::getModel('catalog/product')->load($iProductId); // loads all attributes and initializes model completely

# simple text attributes
$oItem->setName($oProduct->getName());
$oItem->getSku($oProduct->getSku());
$oItem->setSheldonArticlenbr($oProduct->getSheldonArticlenbr());
$oItem->setSheldonColor($oProduct->getSheldonColor());

# option value attributes (select boxes)
$oItem->setSheldonSize($oProduct->getAttributeText('sheldon_size'));

/**
 * The old way END
 **/

/**
 * The new way!!! Gooooood! Fast! Queries database directly and selects only necessary attributes
 **/
/* @var Mage_Catalog_Model_Resource_Product $rProduct */
$rProduct = Mage::getResourceSingleton('catalog/product');

# simple text attributes
$oItem->setName($rProduct->getAttributeRawValue($iProductId, 'name', Mage::app()->getStore()));
$oItem->getSku($rProduct->getAttributeRawValue($iProductId, 'sku', Mage::app()->getStore()));
$oItem->setSheldonArticlenbr($rProduct->getAttributeRawValue($iProductId, 'sheldon_articlenbr', Mage::app()->getStore()));
$oItem->setSheldonColor($rProduct->getAttributeRawValue($iProductId, 'sheldon_color', Mage::app()->getStore()));


# option value attributes (select boxes)
$iRawSize = $rProduct->getAttributeRawValue($iProductId, 'sns_size', Mage::app()->getStore());
$oItem->setSheldonSize($rProduct->getAttribute('sheldon_size')->getSource()->getOptionText($iRawSize));

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.