cancel
Showing results for 
Search instead for 
Did you mean: 

Large Collection Exhausting Memory

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Large Collection Exhausting Memory

I am writing an admin module. I created a collection and if the number of rows in the table are small it loads fine. Ultimately the table will have 250k rows in it and when I attempt to load the collection it exhausts memory. I was under the impression that Magento would lazy load collections. I also will probably need to know how to use the resource iterator in mage 2 as well. I can provide code samples but there is little more than a basic module skeleton at this point. I would greatly appreciate a code example of how to handle and iterate through a large collection.

Note: the collection is pulling from a custom table

1 REPLY 1

Re: Large Collection Exhausting Memory

You can page through a large collection like this:

 

$collection->setPageSize(15);
$pages = $collection->getLastPageNumber();
for ($pageNum = 1; $pageNum<=$pages; $pageNum++) {
$collection->setCurPage($pageNum);
foreach ($collection as $item) {
//Do something.
}
$collection->clear();
}