Tuesday, March 15, 2022

[FIXED] Yii findAll() causes memory issue

Issue

The no of records i have is just 15000. and the memory php is configured to use is 128 mb. so i get this error.

Allowed memory size of 134217728 bytes exhausted

there are 2 ways to handle this.

  1. DAO http://www.yiiframework.com/doc/guide/1.1/en/database.dao
  2. Increasing the memory allowed in PHP

What i am confused at is, if i increase the memory allowed to 256, one day when the number of data becomes 30 000 this error will come again.

so should i not use Yii cactiverecord findAll() when i am developing large scale applications ? or i should keep increasing the memory size as more data comes in.

what is the best approach ?


Solution

Try to retrive data with batches:

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#data-in-batches

// fetch 10 customers at a time
foreach (Customer::find()->batch(10) as $customers) {
    // $customers is an array of 10 or fewer Customer objects
}

// fetch 10 customers at a time and iterate them one by one
foreach (Customer::find()->each(10) as $customer) {
    // $customer is a Customer object
}

// batch query with eager loading
foreach (Customer::find()->with('orders')->each() as $customer) {
    // $customer is a Customer object with the 'orders' relation populated
}


Answered By - Fabrizio Caldarelli

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.