Best practice to process ALL records of a table

Currently I’m using something like




$records = table::model()->findAll($criteria);

foreach ($records as $record) {

  // do the processing here

}



But with a table having millions of records it’s simply impossible or inappropriate to load the whole table into memory. What’s the best practice to process all the records from the table but at a given time having just several records in the memory? (Let’s assume, processing time doesn’t matter)

I would go this way:




$dataReader = Yii::app()->db->createCommand( 'SELECT * FROM model_table' )->query();

foreach( $dataReader as $row ) {

   $obj = new Model();

   $obj->populateRecord( $row );


   //..process $obj


   $obj->__destruct(); //destroy object according to http://www.php.net/manual/en/function.unset.php#98692

   unset( $obj );

}