i have this custom collection which is taking much time to load, so want to change into sql query how can I achieve it?

$delayedcollectioncount = Mage::getModel("customer/customer")
        ->addAttributeToSelect('firstname, entity_id')
        ->addAttributeToFilter('grisk_college_new_id', array('in' =>$college))
        ->addFieldToFilter('group_id', array('in' => array(4,5)))
        ->addAttributeToFilter('assigned_campus_manager', array('null' => true), 'left')
        ->addAttributeToFilter('physical_verify', array('null' => true), 'left')
        ->addFieldToFilter('grisk_pv_upload_date', array('lt' => date("Y-m-d H:i:s", strtotime($configValue))))
        ->addAttributeToSort('grisk_pv_upload_date', 'DESC')

Since you only need a count and there are no limits on your query, you can just replace count with getSize.
It should be way faster.
Here are some details about the difference: Difference between getSize() and count() on collection

| improve this answer | |
  • wow !! it worked reduce my loading time !! Thanks man!!! :) – supriya mishra Apr 24 '17 at 14:17
  • but still i want to try with query if it can reduce more . – supriya mishra Apr 24 '17 at 14:22
  • it may incorrect my counts also ? – supriya mishra Apr 24 '17 at 14:29
  • I don't think it will reduce the time much more. Because getSize actually runs one single query. and retrieve one single result. the only time you would save would be from bypassing the query builder. But honestly you don't want to do that since the customer model is and EAV model. This means it joins some tables in order to filter by attributes. Writing it as a query increases your error chances significantly. – Marius Apr 24 '17 at 14:31
  • loading time i m not able to check in test server perfectly but still, i m getting significant difference , any way just worried about correct count as a result . – supriya mishra Apr 24 '17 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.