Back in Magento 1, it was possible to use the collection iterator to walk through the results and avoid looping through them.
It was a huge improvement in terms of performance when dealing with massive collections.
Here is some sample code of what could be done.
$customers = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect(array('firstname'), 'inner');
// call iterator walk method with collection query string and callback method as parameters
Mage::getSingleton('core/resource_iterator')->walk($customers->getSelect(), array(array($this, 'customerCallback')));
Then you could define a callback function customerCallback
to process the results one by one.
Is that still possible in Magento 2? If so how can I achieve that?
Best Answer
it still seems to be there: https://github.com/magento/magento2/blob/2.0/lib/internal/Magento/Framework/Model/ResourceModel/Iterator.php
Usage should be the same as in M1