4

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());
7

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

  • Thanks for edit my question and for answer. You helped me like always :) thanks!! – Wojciech Ka Mar 30 '16 at 8:45
3

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

Yes. But to understand that, you should first understand the difference in functionality.

This code:

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

does not have the same result as this:

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

Difference for EAV models:

Load

  • The entity is loaded from the main table (for products: catalog_product_entity)
  • All EAV attributes for this entity are loaded from the respective tables
  • The _afterLoad method and load_after events are triggered (for products: custom options and inventory data is loaded, other additional data from extensions)

Get From Collection

  • For products and categories: If the flat catalog is enabled, the attributes are loaded in a single query from the flat table. But that means, only attributes that are configured as "used in product listing", i.e. in the flat index, are loaded
  • Additional data may be joined from other index tables (for products: stock, price, category), but you have to specify that explicitly.
  • The collection_load_after events are triggered

Conclusion

That means, you have more flexibility in collections what to load, but it's never the same as a full load. Understand what you need and decide which option works for you.

A lesser known fact is that you can also restrict the loaded attributes in load() for EAV models:

$product = Mage::getModel('catalog/product')->load($productId, ['sku', 'name', 'description']);

This way you can:

  • force using the EAV tables instead of flat table
  • have the load_after events triggered
  • load only what you need and notably increase performance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.