Does PHP7's PDO ext read the entire result set into memory?

后端 未结 1 1790
轮回少年
轮回少年 2021-01-01 23:04

I have noticed since I upgraded to PHP7 that some SQL statements no longer work and instead run out of memory.

I have this code:

$query = Yii::$app-&         


        
1条回答
  •  伪装坚强ぢ
    2021-01-01 23:28

    It is not directly PHP7-related. The issue is due to new mysqlnd driver, so you can experience the the same problem even with PHP 5.x as well. It is actually a bugfix, because even before the memory was still allocated, but it didn't count towards memory_limit.

    To avoid a memory issue you have to use unbuffered queries for the large resultsets.

    So, for the query that is expecting a large dataset, set the proper setting like this:

    $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
    

    For the further reading, I've got a decent explanation in my PDO tutorial, thanks to Nikic, whose critical feedback was invaluable.

    0 讨论(0)
提交回复
热议问题