1

I am trying to refactor get category data method. Initially, it was written with Collection filter. Later I refactored with the search builder interface.

When I tried to compare the querying time, I found the Collection query seems to be faster.

Here is a performance difference.

Can we assume that filtering through the collection factory is faster than a search criteria builder?

1st collection method

2nd search criteria enter image description here

Code snippet

\Magento\Framework\Profiler::start('category-collection-timer');
$collection = $this->_categoryCollectionFactory->create()->addFieldToFilter('is_feature',1)->addAttributeToSelect(['name', 'is_active', 'parent_id', 'image'])->setStoreId($this->_storeManager->getStore()->getId())->setPageSize(8)->setCurPage(1) ;
\Magento\Framework\Profiler::stop('category-collection-timer');

Code snippet

\Magento\Framework\Profiler::start('category-list-timer');
            $featuredFilter=$this->filterBuilder->setField('is_feature')->setValue('1')->setConditionType("eq")->create();
            $activeFilter=$this->filterBuilder->setField('is_active')->setValue('1')->setConditionType("eq")->create();
$group1=$this->filterGroupBuilder->addFilter($featuredFilter)->create();
$group2=$this->filterGroupBuilder->addFilter($activeFilter)->create();
            $searchCriteria=$this->searchCriteriaBuilder->setFilterGroups([$group1,$group2])->create();
$searchCriteria->setPageSize(8)->setCurrentPage(1);
$categorylistresults=$this->categoryList->getList($searchCriteria);
$categories=$categorylistresults->getItems();
\Magento\Framework\Profiler::start('category-list-timer');
3
  • two queries are not same
    – Sohel Rana
    Jun 13, 2020 at 17:32
  • @sohel I will verify and repost the queries again. I would like to know if there is difference in performance between search builder and collection factories
    – blakcaps
    Jun 14, 2020 at 1:25
  • The answer is no. But still, your both query are not the same. The first one returns count value, 2nd one returns all data. That's why you saw the difference.
    – Sohel Rana
    Jun 14, 2020 at 6:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.