optimize-magento2-collection

In this blog, we will provide some useful Optimisation tips when Dealing with Collection to help you optimize your application.
Optimization is a critical part of any application, here we will drive your attention to common mistakes we make in Magento 2 when dealing with any collection.

Collection Size:

Many times we are required to check the size of the collection, there are two methods to check the collection size namely $collection->count() or count($collection) and $collection->getSize() and it will help to Optimisation collection.

If you need to check collection size, don’t use $collection->count() or count($collection). Instead of use $collection->getSize() method.

If you use $collection->count() or count($collection), it will load the collection and then give you the count of items in the collection. Whereas if you are using $collection->getSize() method it will simply execute sql count query in the database and give the result.

Bad Practice:

If ($collection->getSize()) {
    // you code here
}

Good Practice:

If ($collection->getSize()) {
    // you code here
}

Load Model inside a loop:

Sometimes we are loading the Model within looping it is a bad practice and this is a very common mistake. This is an impactful method for Optimisation with Collection We shouldn’t load the model inside the loop instead we should use the collection or join on collection.

Let’s see an example

Bad Practice:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach ($productIds as $productId) {
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
    echo $product->getName();
}

Here we are loading the product model within a loop to fetch the product name only.

Good Practice:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$collection = $objectManager->create('Magento\Catalog\Model\Product')
                        ->getCollection()
                        ->addAttributeToSelect("name")
                        ->addFieldToFilter("entity_id", ["in" => $productIds]);
foreach ($collection as $product) {
    echo $product->getName();
}

We have fetched the product collection outside the loop by the product IDs we want to load.

 Fields in Collection:

In a situation when we require to load collection, we have to select the fields that we require in the collection result.
Suppose we need all increment Ids from orders, then we will get the collection of order and then iterate it to get the increment ids. When we are loading collection that means are selecting all the fields from table regardless of usage. But instead of selecting all the columns from the table or collection, we should select only the desired fields from the collection.

Bad Practice:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->create('Magento\Sales\Model\Order')->getCollection();
foreach ($collection as $order) {
    echo $order->getIncrementId();
}

It will select all the columns from the table, which usually will a time to load the collection as compared to selecting desired fields.

Good Practice:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->addFieldToSelect("increment_id");

foreach ($collection as $order) {
    echo $order->getIncrementId();
}

This will select only the increment_id column from the table rather than select unnecessary columns. If you have a large number of records then these methods will surely help you.
I hope you have enjoyed these Optimisation Tips when Dealing with Collection.

If you have any doubt or query please comment below.

Thanks

Rate this post