MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

Common Coding Issues with Magento Platform

Published on 21 April 15
708
0
2

Common Coding Issues with Magento Platform - Image 1
If you have been developing store after store on Magento, you would have noticed certain coding issues. Probably, you have gone through some of these issues yourself after noticing a dive in the performance of your store. As mentioned, there are several coding issues that affect the performance of your store. But, these five, that are mentioned here, are typically responsible for 84% of the performance related issues affecting your store. Let's talk about how they affect the store, and how they can be optimized

Coding within a Loop
You will realize that even the smallest error that occurs within a loop is magnified. Generally, loops are the biggest performance bugs, especially the ones that reiterate SQL queries or redundant calculations

Let's see how inefficiencies of redundant calculations and SQL queries within a loop can affect the performance of your Magento store

Computations within a Loop

If you want to calculate the size of an array using a loop, the one function that you might use is count()

for ($i = 0; $i < count($rows); $i++)

{

//some code

}
Count(), individually performs and calculates fast; it slows down only when used within a loop. When you start calculating the size of an array with a lot of data and use count to deliver the output, you will find the performance slowing down immensely. The reason being PHP does not offer loop-invariant code motion automatically.

The best way to avoid this performance issue is to call the function outside the loop

$rowNum = count($rows);

for ($i = 0; $i < $rowNum; $i++)

{

//some code

}
SQL Queries within a Loop
As it is computing an SQL query can lead to performance based issues; when you run it within a loop, there is a possibility of slowing down your website. Most often, you will see that Magento models are loaded in loops

foreach ($this->getProductIds() as $productId)

{ $product = Mage::getModel('catalog/product')->load($productId);

$this->processProduct($product);

}

An array of product IDs is loaded in order to process the model. As can be seen EAV is used to carry out this loop which might require you to run several queries. In this case, the number of queries that are being executed will be multiplied with the number of products, and the result is a slow store that gives out poor performance.

The best way to optimize would be to avoid loading the products in a loop. So, what can you do? Use Magento data collection which loads a set of products easily. Following is an example of how data collection models collect the products by creating an array of the different product Ids and keep adding the requested products to their fields
$collection = Mage::getResourceModel('catalog/product_collection')

->addFieldToFilter('entity_id', array($this->getProductIds()))

->addAttributeToSelect(array('name'));

foreach ($collection as $product)

{

$this->processProduct($product);

}
When you move out of the loops and into the model collection method, you will see that the performance of the store has indeed improved. Always remember to keep a check on the memory, so that when using data collection models, you don't load data that is heavier as compared to the memory size. You must perform mass saving and deleting of the queries within the models so as to maintain the performance of your store.

Loading Models
The whole process of loading a model into your store is not really cached, which is why each time you call the load() function, certain queries are run within the database, thus causing performance issues in your store

$name = Mage::getModel('catalog/product')->load($productId)->getName();

$sku = Mage::getModel('catalog/product')->load($productId)->getSku();

$attr = Mage::getModel('catalog/product')->load($productId)->getAttr();

This is a classic example of how queries are run on the database each time a particular model is called

The best way to tackle this issue is to ensure that each model is loaded only once

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

$name = $product->getName();

$sku = $product->getSku();

$attr = $product->getAttr();

Sometimes, you are only concerned with the product Id. At such a time, it does not make sense to load the whole model. With this code you can easily load the product Id for a particular product

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

$res['id'] = $product->getId();

Another popular method to derive product ID is to use the native product method getIdBySku()

$res['id'] = Mage::getModel('catalog/product')->getIdBySku($sku);

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

$res['id'] = $product->getId();

Another popular method to derive product ID is to use the native product method getIdBySku()

$res['id'] = Mage::getModel('catalog/product')->getIdBySku($sku);

Coding Collections
The other area in Magento coding that offers issues is collections. Collections are pretty efficient, but as they work with a huge data set, you will need to ensure that you are coding them with care, else you will face performance issues

Let's see how collections can cause coding issues

Calling Redundant Data Sets

You generally use collections to retrieve a single item especially the first item on the loop's first iteration. So, what could be an issue here? When you don't offer a limitation on the results, the loop will keep iterating and give out all the items within the collection in the result, instead of the original first item. The following is an example of how collections can give out all the items

public function getRandomItem()

{

$collection = Mage::getResourceModel('mymodule/my_collection')- >setRandomOrder();

return $collection->getFirstItem();

}

Applying the limitation

public function getRandomItem()

{

$collection = Mage::getResourceModel('mymodule/my_collection')

- >setRandomOrder() ->setPageSize(1);

return $collection->getFirstItem();

}

The functions $collection->setPageSize() and $collection->setCurPage() offer limit the number of items being retrieved from the collection

You can even modify $collection->getSelect()->limit() to restrict the number of items being loaded from the collection.

Note: Before making any changes, donât forget to take a backup of your existing database












































































































































Common Coding Issues with Magento Platform - Image 1

If you have been developing store after store on Magento, you would have noticed certain coding issues. Probably, you have gone through some of these issues yourself after noticing a dive in the performance of your store. As mentioned, there are several coding issues that affect the performance of your store. But, these five, that are mentioned here, are typically responsible for 84% of the performance related issues affecting your store. Let's talk about how they affect the store, and how they can be optimized

Coding within a Loop
You will realize that even the smallest error that occurs within a loop is magnified. Generally, loops are the biggest performance bugs, especially the ones that reiterate SQL queries or redundant calculations

Let's see how inefficiencies of redundant calculations and SQL queries within a loop can affect the performance of your Magento store

Computations within a Loop

If you want to calculate the size of an array using a loop, the one function that you might use is count()

for ($i = 0; $i
{

//some code

}

Count(), individually performs and calculates fast; it slows down only when used within a loop. When you start calculating the size of an array with a lot of data and use count to deliver the output, you will find the performance slowing down immensely. The reason being PHP does not offer loop-invariant code motion automatically.

The best way to avoid this performance issue is to call the function outside the loop

$rowNum = count($rows);

for ($i = 0; $i
{

//some code

}

SQL Queries within a Loop
As it is computing an SQL query can lead to performance based issues; when you run it within a loop, there is a possibility of slowing down your website. Most often, you will see that Magento models are loaded in loops

foreach ($this->getProductIds() as $productId)

{ $product = Mage::getModel('catalog/product')->load($productId);

$this->processProduct($product);

}

An array of product IDs is loaded in order to process the model. As can be seen EAV is used to carry out this loop which might require you to run several queries. In this case, the number of queries that are being executed will be multiplied with the number of products, and the result is a slow store that gives out poor performance.

The best way to optimize would be to avoid loading the products in a loop. So, what can you do? Use Magento data collection which loads a set of products easily. Following is an example of how data collection models collect the products by creating an array of the different product Ids and keep adding the requested products to their fields

$collection = Mage::getResourceModel('catalog/product_collection')

->addFieldToFilter('entity_id', array($this->getProductIds()))

->addAttributeToSelect(array('name'));

foreach ($collection as $product)

{

$this->processProduct($product);

}

When you move out of the loops and into the model collection method, you will see that the performance of the store has indeed improved. Always remember to keep a check on the memory, so that when using data collection models, you don't load data that is heavier as compared to the memory size. You must perform mass saving and deleting of the queries within the models so as to maintain the performance of your store.

Loading Models
The whole process of loading a model into your store is not really cached, which is why each time you call the load() function, certain queries are run within the database, thus causing performance issues in your store

$name = Mage::getModel('catalog/product')->load($productId)->getName();

$sku = Mage::getModel('catalog/product')->load($productId)->getSku();

$attr = Mage::getModel('catalog/product')->load($productId)->getAttr();

This is a classic example of how queries are run on the database each time a particular model is called

The best way to tackle this issue is to ensure that each model is loaded only once

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

$name = $product->getName();

$sku = $product->getSku();

$attr = $product->getAttr();

Sometimes, you are only concerned with the product Id. At such a time, it does not make sense to load the whole model. With this code you can easily load the product Id for a particular product

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

$res['id'] = $product->getId();

Another popular method to derive product ID is to use the native product method getIdBySku()

$res['id'] = Mage::getModel('catalog/product')->getIdBySku($sku);

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

$res['id'] = $product->getId();

Another popular method to derive product ID is to use the native product method getIdBySku()

$res['id'] = Mage::getModel('catalog/product')->getIdBySku($sku);

Coding Collections
The other area in Magento coding that offers issues is collections. Collections are pretty efficient, but as they work with a huge data set, you will need to ensure that you are coding them with care, else you will face performance issues

Let's see how collections can cause coding issues

Calling Redundant Data Sets

You generally use collections to retrieve a single item especially the first item on the loop's first iteration. So, what could be an issue here? When you don't offer a limitation on the results, the loop will keep iterating and give out all the items within the collection in the result, instead of the original first item. The following is an example of how collections can give out all the items

public function getRandomItem()

{

$collection = Mage::getResourceModel('mymodule/my_collection')- >setRandomOrder();

return $collection->getFirstItem();

}

Applying the limitation

public function getRandomItem()

{

$collection = Mage::getResourceModel('mymodule/my_collection')

- >setRandomOrder() ->setPageSize(1);

return $collection->getFirstItem();

}

The functions $collection->setPageSize() and $collection->setCurPage() offer limit the number of items being retrieved from the collection

You can even modify $collection->getSelect()->limit() to restrict the number of items being loaded from the collection.

Note: Before making any changes, donât forget to take a backup of your existing database

This blog is listed under Development & Implementations and E-Commerce Community

Related Posts:
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top