[英]Does PHP7's PDO ext read the entire result set into memory?
I have noticed since I upgraded to PHP7 that some SQL statements no longer work and instead run out of memory. 自从升级到PHP7以来,我已经注意到某些SQL语句不再起作用,而是用尽了内存。
I have this code: 我有以下代码:
$query = Yii::$app->db->createCommand('select * from tbl_title')->query();
while ($row = $reader->read()) {
var_dump($row);
exit();
}
And Yii2's database abstraction is just an extremely thin layer over PDO's and does not do anything extra. Yii2的数据库抽象只是PDO上的极薄层,没有做任何额外的事情。 query()
does nothing extra except add a line to a log file (Yii2's) for profiling and reader->read()
just calls the PDO stream's fetch()
function. query()
除了在日志文件(Yii2)中添加一行以进行概要分析外,没有做任何额外的事情,而reader- reader->read()
仅调用PDO流的fetch()
函数。
But it runs out of memory quoting the size (space used) of my table, ie trying to allocate 385 MB of process memory: 但是它引用了我的表的大小(已使用的空间),导致内存不足,即尝试分配385 MB的进程内存:
Allowed memory size of 134217728 bytes exhausted (tried to allocate 385883840 bytes) 允许的内存大小为134217728字节已用尽(尝试分配385883840字节)
As a spanner, if I use a query whose result set fits entirely in the 128 MB limit of the PHP process works. 作为一个扳手,如果我使用一个查询的结果集完全适合PHP过程的128 MB限制,则可以使用。
So, has PHP7 changed and can I change it back? 那么,PHP7是否已更改,我可以将其更改回吗?
It is not directly PHP7-related. 它与PHP7不直接相关。 The issue is due to new mysqlnd driver, so you can experience the the same problem even with PHP 5.x as well. 该问题是由于新的mysqlnd驱动程序引起的,因此即使使用PHP 5.x,您也可能遇到相同的问题。 It is actually a bugfix, because even before the memory was still allocated, but it didn't count towards memory_limit . 它实际上是一个错误修正,因为即使在尚未分配内存之前,但它并未计入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. 对于进一步的阅读,由于Nikic的宝贵反馈非常宝贵,因此我在PDO教程中得到了不错的解释 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.