Magento 1.9 Performance – Difference Between Load and Using Collections

magento-1.9performance

Is there any difference in performance between load and the use of collections?

I was told that it is better to use this:

$product = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id', $productId)
            ->getFirstItem();

instead of this:

$product = Mage::getModel('catalog/product')->load($item->getId());

Best Answer

Well, the real question here is "what's the difference between load and using collections ?"

Using collections is a huge performance boost for the following reasons, especially for EAV entities:

Loading an entity attribute value (EAV) model requires several heavy queries to execute. As the number of executed queries is multiplied with the number of products, we get extremely inefficient and slow code.

When you use the load method you load every single attributes assigned to the entity.

Let's say you only need to know the value of one attribute of your entity, loading is definitely not the right way to go and should use some code like this:

$product = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('my_attribute') ->addAttributeToFilter('entity_id', $productId) ->getFirstItem();

Worst case scenario is when you use the load method in a loop, it can result in a disaster in terms of performances:

enter image description here

I suggest you have a look at the following official guide: http://info2.magento.com/rs/magentosoftware/images/Conquer_the_5_Most_Common_Magento_Coding_Issues_to_Optimize_Your_Site_for_Performance.pdf

Related Topic