Iteration in a Loop
Count may be the fastest form of code but, when it is called within a loop it reduces the efficiency of the code. You need to understand that PHP interpreters work slightly different. It does not perform the loop invariant code motion automatically which simply means the code will erupt a lot of issues if you use count within which needs to be called automatically. Simple solution is to use count outside the loop.
If you have an array with less than 25,000 items calling count outside the loop won’t make a major difference. But, consider an array code with more than 25000 items, calling the count code outside the loop will optimize the site.
Executing SQL Queries in a Loop
Executing SQL queries can be very tedious for the system. When you begin operating SQL queries in a loop it is even more time consuming. What are the common mistakes by a developer in case of SQL queries? They tend to iterate over array of product IDs and load product model and process in that loop thus slowing the system.
What can be done? Get a Magento data collection model that helps load the model efficiently. When you use collection, the model becomes faster compared to when you use models in loops. Make sure that you don’t go beyond allocated memory when dealing with large data sets. You will have better control over the loaded data fields.
Several times developers tend to call the same model several times in the code thus increasing the load time. Do you really believe that it is necessary to call the model so many times? It’s not cached internally and every time you call it, several queries run in the database to have that model staged. If you wish to improve the performance of your site, make sure you call each model only once.
Also, sometimes you really don’t need the model. You can make do with the native product as you are not really working with the model. In these cases use the local product load entity to reduce the methods.
$res[‘id’] = Mage::getModel(‘catalog/product’)->getIdBySku($sku);
Collections are often misused by inexperienced developers. Collections define retrieving single item in one go: $collection->getFirstItem() or in returning the first item as soon as the first loop is iterated. Now what most developers do is they forget to apply a limitation to the collection’s query results. This does not happen automatically actually. When you don’t place an restrictions, the moment before you call the collection, it will begin loading all the items and display everything. What follows is a real slow site that will take ages to load.
Use limits whenever you are defining collections: $collection->getSelect()->limit()
When you wish to load items from a particular collection, you would most naturally use $collection->count(). But, the right way of doing it is $collection->getSize() as it places the necessary limits.
Inefficiency in Memory Utilized
You use fetchAll() to fetch results but, while you use this code you fetch a lot of data thus causing heavy load on your site. This is how codes are normally developed to fetch results. What happens is that PHP runs out of memory when you begin fetching such large amounts of data. Instead fetch the database row once. The database buffer will run once to fetch the data and overall the speed will be good.
Whatever be the situation, make sure you as a developer do not load large amounts of data at a time as it may slow your system.
E-Commerce Consultant | www.mconnectmedia.com