问题描述
我正在编写批处理脚本,并出现Allowed memory size of 134217728 bytes exhausted
错误.
I am writing a batch script and get a Allowed memory size of 134217728 bytes exhausted
error.
我不明白为什么内存已满.我尝试取消设置$row
变量,但这并没有改变任何事情.这是我的代码:
I don't understand why the memory is filling up. I tried unsetting the $row
variable, but that didn't change a thing. Here is my code:
// ... (sql connection)
$result = mysql_query("SELECT * FROM large_table");
while ($row = mysql_fetch_array($result)) {
echo $row['id'] . PHP_EOL;
unset($row);
}
(简化代码)
为什么内存已满,如何避免呢?
Why does the memory fill up, and how can I avoid it?
注意:这是一个批处理脚本.我必须处理这样的数据(遍历一百万行),这是正常的.
Note: this is a batch script. This is normal that I have to handle data like that (go through 1 million lines).
更新:内存不足发生在第40万行附近,所以这一定是在循环中吗?我希望尽可能避免实施分页.
推荐答案
尝试使用 http://www.php.net/manual/zh/function.mysql-unbuffered-query.php (mysql_unbuffered_query()),以防止将整个表加载到内存中,但仍避免分页
Try using http://www.php.net/manual/en/function.mysql-unbuffered-query.php (mysql_unbuffered_query()) to prevent the whole table being loaded into memory, but still avoiding pagination.
这篇关于为什么“允许的内存大小用尽"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!