DataReader requires a lot of memory

Hello,

My task is pretty simple - get from MySQL 1 mln records and return it to user as CSV file.


	...

        $command = Yii::app()->db->createCommand("SELECT * FROM contacts");

	$contacts = $command->query();

        $output = fopen("php://output",'w') or die("Can't open php://output");

//	foreach ($contacts as $c) {

	while($c = $contacts->read()){

	      fputcsv($output, $c, ';');

	      ob_flush();

	      flush();

	}

	fclose($output);

It shows me Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) in /home/aaa/app.production/framework/db/CDbCommand.php on line 506

It works pretty good on my development machine (Ubuntu 13.10+php 5.5.3) with memory_limit=128Mb

Any recommendations will be highly appreciated.

It’s a bit weird but I fixed the issue on the production server by

Yii::app()->db->pdoInstance->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);