0

I having a custom table with 5lakhs records, i am writing query and get count of row using $collection->getCount() method, but it throw an error

Fatal error: Allowed memory size of 268435456 bytes exhausted.

Table (id,firstname,lastname,address) 

this table having 5lakhs records

$collection = $this->getCollection();
            echo $collection->getCount(); exit;
            if($collection->getCount()) {
                foreach($collection as $_collection) {
                    $this->email[$_collection->getEmail()] = $_collection->getId();
                    $this->f_name[$_collection->getFirstname()] = $_collection->getId();
                    $this->l_name[$_collection->getLastname()] = $_collection->getId();
                }
            }

Here i wrote this query in that corresponding table model file,that why use the $this object.

i also increase my max_memory limit to 1024M in my php.ini file.

1

You are running into a common issue in dealing with large catalogs. The method you are using ->getCount() will eventually lead to:

public function count()
{
   $this->load();
   return count($this->_items);
}

And here you can see the issue: you are essentially trying to load 500 000 records. (I had to google what a lakh measurement was, something newly learnt)

Instead of using count, use ->getSize()

$collection->getSize();

  • Hmm its working,but when i print the $_collection->getFirstname(), the same issue is again repeating.may i know what is the problem behind in that. – senthil Aug 13 '15 at 6:34
  • I gather that you are doing this inside the foreach($collection as $_collection) { .... } ? The same issue, the foreach will load the entire collection into an array. you need to work with your collection in pages. There are a few ways to solve this. simply google 'working with large collections in magento' – ProxiBlue Aug 13 '15 at 6:51
  • @ProxiBlue foreach will load the collection ? will you please explain it more? or you have any link that clearly shows the "loading" effect of foreach ? – Rajeev K Tomy Aug 13 '15 at 6:56
  • 1
    foreach will iterate an array ... a collection is really just an array of objects. In order for the foreach language construct to work, it needs to load the collection, so it can become an array (of objects) to iterate over. Read this for a more detailed explanation: inchoo.net/magento/working-with-large-magento-collections – ProxiBlue Aug 13 '15 at 7:04
  • thanks for your answer, i study how to handle the large-collection in magento. and implemented. now its working without any issue – senthil Aug 13 '15 at 10:24

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.