Menu Close
    Searching for an experienced Magento Development Company ?

    Optimisation Tips For Magento and Magento2 – Part 1

    Optimisation is the most critical part of any application. There are some common mistakes we do in magento or magento2.
    There are some useful tips that will help you to optimise your code and application.

    Collection Size

    If you need to check the collection size, don’t use $collection->count() or count($collection). Instead of these use $collection->getSize() method. If you are using $collection->count() or count($collection) , it will load the whole collection and then give you the count of items in collection. But if you are using $collection ->getSize() method it will simply execute a count sql query into database and give you the count.

    It is considered as the best way to find the count of a collection.

    Bad Practice

    if ($collection->count()) {
        //custom code
    }
    if (count($collection)) {
        //custom code
    }

    Good Practice

    if ($collection->getSize()) {
        //custom code
    }

    Model Loading in Loop

    Its a bad practice to load model in loop. This is very common mistake. We shouldn’t load model inside the loop. Instead of this we should use collection or join on collection.

    Bad Practice

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

    Good Practice

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

    Suppose we have a custom table in which we have order ids. and we need order details.

    Bad Practice

    $objManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customCollection = $objManager->create('Vendor\Custom\Model\ResourceModel\Custom\Collection');
    foreach ($customCollection as $item) {
        $orderId = $item->getOrderId();
        $order = $objManager->create('Magento\Sales\Model\Order')->load($orderId);
        echo $order->getIncrementId();
    }

    Good Practice

    $objManager = \Magento\Framework\App\ObjectManager::getInstance();
    
    $customCollection = $objManager->create('Vendor\Custom\Model\ResourceModel\Custom\Collection');
    $resource = $objManager->create("Magento\Framework\App\ResourceConnection");
    $joinTable = $resource->getTableName('sales_order');
    $fields = ['*'];
    $customCollection->getSelect()->join($joinTable, 'main_table.order_id = sales_order.entity_id', $fields);
    foreach ($customCollection as $item) {
        echo $item->getIncrementId();
    }
    

     

    Product Attribute Value

    In Magento or Magento2 if we need to get any attribute value of Product, we simply load the product and then we get value of attribute.

    There is a method in magento which is more faster.

    Normal Practice

    //Magento 1
    $productId = 1;
    $product = Mage::getModel("catalog\product")->load($productId);
    echo $product->getName();
    
    //Magento 2
    $objManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productId = 1;
    $product = $objManager->create('Magento\Catalog\Model\Product')->load($productId);
    echo $product->getName();
    
    

    Good Practice

    //Magento 1
    $productId = 1;
    $attributeCode = "name";
    $storeId = 1;
    $name = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, $attributeCode, $storeId);
    echo $name;
    
    //Magento 2
    $objManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productId = 1;
    $attributeCode = "name";
    $storeId = 1;
    $name = $objManager->create('Magento\Catalog\Model\ResourceModel\Product\Action')->getAttributeRawValue($productId, $attributeCode, $storeId);
    echo $name;
    

    Fields in Collection

    It happens most of the times. Suppose we need all increment ids of orders, then we will get the collection of order and then iterate it to get the increment ids. This is what we do most of the times. When we are loading collection that means we are selecting all fields from table. But instead of selecting all the fields or columns in collection, we should select only desired field or column from collection. We don’t need to select unnecessary fields or columns.

    Bad Practice

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

    Good Practice

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

    You can use all these methods according to your need.

    I faced many optimisation issues in application and then i found these solutions.
    If you have a large number of records then these methods will surely help you.

    Next time i will try to include more methods and approaches for optimisation.
    If you have any doubt or query please comment below.
    Thanks

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top