PHP7 的 PDO ext 是否将整个结果集读入内存?

2023-10-16php开发问题
19

本文介绍了PHP7 的 PDO ext 是否将整个结果集读入内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

自从我升级到 PHP7 后,我注意到一些 SQL 语句不再起作用,而是内存不足.

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

我有这个代码:

$query = Yii::$app->db->createCommand('select * from tbl_title')->query();
while ($row = $reader->read()) {
    var_dump($row);
    exit();
}

而 Yii2 的数据库抽象只是 PDO 之上的一个非常薄的层,并没有做任何额外的事情.query() 除了在日志文件(Yii2 的)中添加一行用于分析和 reader->read() 只调用 PDO 流的 fetch() 函数.

And Yii2's database abstraction is just an extremely thin layer over PDO's and does not do anything extra. 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.

但是引用我的表的大小(使用的空间)时内存不足,即尝试分配 385 MB 的进程内存:

But it runs out of memory quoting the size (space used) of my table, i.e. trying to allocate 385 MB of process memory:

允许的内存大小为 134217728 字节耗尽(尝试分配 385883840 字节)

Allowed memory size of 134217728 bytes exhausted (tried to allocate 385883840 bytes)

作为扳手,如果我使用的查询的结果集完全符合 PHP 进程的 128 MB 限制.

As a spanner, if I use a query whose result set fits entirely in the 128 MB limit of the PHP process works.

那么,PHP7 是否发生了变化,我可以将其改回来吗?

So, has PHP7 changed and can I change it back?

推荐答案

它与 PHP7 没有直接关系.该问题是由于新的 mysqlnd 驱动程序造成的,因此即使使用 PHP 5.x,您也会遇到同样的问题.这实际上是一个错误修正,因为甚至在内存仍然分配之前,但它并不计入 memory_limit.

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);

为了进一步阅读,我在我的 PDO 教程中有一个不错的解释,感谢Nikic,他的批评反馈非常宝贵.

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

这篇关于PHP7 的 PDO ext 是否将整个结果集读入内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17