Assuming I have
$db is an instance of Zend_Db_Adapter_Abstract and
$sql = 'SELECT blah blah FROM table' will return a huge number of records.
There are two code fragments to process the returned data as follows.
// Code fragment 1 (let's call it C1).
$results = $db->fetchAll($sql);
foreach ($results as $row) {
// Process $row
}
// Code fragment 2 (let's call it C2).
$stmt = $db->query($sql);
while ($row = $stmt->fetch()) {
// Process $row
}
My understanding is that C1 will load all returned data to $results. So, a huge data is loaded to PHP memory. Below are my questions.
- Does C2 load all data to PHP memory or does it process one by one like prepare/execute?
- Assuming there is no other option, is C1 or C2 a better option?
Thanks!