When working with large product collections (50,000 products +) in Magento 1 I ended up using something similar to the following.

public function getProductCollection()
    $productsCollection = Mage::getModel('catalog/product')
        ->addAttributeToSelect(array('name', 'price', 'visibility'));


    $pages = $productsCollection->getLastPageNumber();
    $currentPage = 1;

    do {

        foreach ($productsCollection as $_product) {

            echo $_product->getId() . "\n";
            echo $_product->getSku() . "\n";
            echo $_product->getName() . "\n";

            /* do stuff */


        //clear collection and free memory
    } while ($currentPage <= $pages);

Does anyone have experience on working with very large product collections in Magento2? What is the recommended approach?


Someone on the magento forum suggested something like this:

public function __construct( 

   \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
) {
    $this->collectionFactory = $collectionFactory;

public function yourMethod()
    $collection = $this->collectionFactory->create();
    $pages = $collection->getLastPageNumber();
    for ($pageNum = 1; $pageNum<=$pages; $pageNum++) {
        foreach ($collection as $item) {
            //Do something.

Also I've come across this:


In theory sound although I've not tested.

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.