Magento Performance – why you should care about it?
This week we want to teach you how to make your shop performance on a high level. We have prepared a short presentation and description of changes you can make in the code to achieve expected performance results.
Magento is a platform that allows rapid implementation of online stores and delivers reach number of tools. However, the quality of the implementation depends on the developer. He has 2 choices: use of simple and low cost solutions or application of quality, performance and code scalability.
For us improvement of quality, efficiency and scalability is a habit. We believe in work in progress and growth. We made mistakes in the past but we did our homework and we have learnt. Right now we have a bunch of tips and tricks for effective Magento implementations. We have solved many problem and it is the time to share it with you.
Resources and inspiration
The description is based on the analysis of errors founded in ongoing performance projects and the document created by Magento Expert Consulting Group (ECG) called “Conquer The 5 Most Common Coding Issues Magento is Optimize Your Site for Performance”. It describes the 5 most common performance mistakes made by developers.
For more experienced developers this description can be a good recovery of knowledge. For others it is a presentation of most typical error with examples of change. The goal here is to show, how to write code and change Magento to more scalable and effective e-commerce machine.
Top 5 Magento PHP code performance
Creators of Magento (ECG) after many code tests and audits distinguished 5 most common errors. Explained mistakes covered 85 % of all errors and 28 % of them influenced the performance and scalability of Magento codes.
Below we will show you every error starting from the most common. Of course, for every case, we have prepared a useful solution.
SQL queries inside a loop
Making a database query in a loop is the most common mistake made by the developers of Magento. It is one of the most expensive operation (in terms of calculation).
Example of the wrong implementation of code:
In the above example, when processing a loop array containing ids of products we loading every time the product model. Each loading of the model executes multiple queries to the database because the load method retrieves all the attributes of the product.
As we know, Magento database works by default with EAV model, which results in a distribution of attributes of the product in several tables.
So if we summarize that in each iteration of the loop we repeatedly refer to the base, trying to get all the values of the product, then we obtain a strong reduction in code performance.
The most obvious solution to the problem is to use a collection mechanism in Magento. The collection is a set of models, but its major advantage is that it aligns queries to the database creating a collective mark. It helps to improve the performance of the code. An additional advantage is that using collection we control the list of active components.
The above performance problem can be improved by using the following code:
In this case, products are filtered based on ids. Then we declare what attributes we want to loading and finally we processes resulting collection in a loop. You have to remember to declare all the attributes needed, because the collection will loading just the basic ones.
The difference in performance of these examples shows the following graph:
We see that with increasing amounts of processed products in the case of load in the loop code execution time increases linearly, as opposed to the collection where the time of code execution practically remained at the same level.