如何在zend中清理/释放数据库查询内存? [英] how to cleanup / free database query memory in zend?

查看:37
本文介绍了如何在zend中清理/释放数据库查询内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行了这个简单的代码(对于MySQL数据库)之后,每次循环迭代我得到的内存减少了1kB,因此在第1000次迭代之后,我大约使用了1MB的内存.

After executing this simple code (for MySQL database) I get 1kB of memory less for each loop iteration, so after 1000'th iteration I have about 1MB memory used.

现在,如果必须循环运行一个长时间运行的脚本(大约1000000次迭代),我将很快内存不足

Now, if I have to loop in a long running script (about 1 000 000 iterations) I will be out of memory quickly

$_db = Zend_Db_Table::getDefaultAdapter();

$start_memory = memory_get_usage();

for ($i=0; $i<1000; $i++) {
    $update_query = "UPDATE table SET field='value'";
    $_db->query($update_query);
}

echo 'memory used: '.(memory_get_usage()-$start_memory);

有没有办法释放数据库查询使用的内存?

Is there a way to free memory used by database query?

我试图在一个函数中放置更新查询,因此在离开该函数使用的函数作用域后,应该自动释放该函数的资源:

I tried to put update query in a function so after leaving function scope resources used by this function should be freed automaticaly:

function update($_db) {
  $sql = "UPDATE table SET field='value'";
  $_db->query($sql);
}
...
for ($i=0; $i<1000; $i++) {
    update($_db);
}

但事实并非如此!

我对一口气尝试更新多个行"之类的建议不感兴趣;)

I'm not interested in advices like 'try updating mutliple rows in one go' ;)

推荐答案

很可能您有 Zend_Db_Profiler 已启用.

数据库探查器存储每个执行的查询,这对于调试和优化非常有用,但是如果您执行大量查询,则会导致相当快的内存耗尽.

The database profiler stores each executed query which is very useful for debugging and optimisation but leads to rather fast memory exhaustion if you execute a huge numbers of queries.

在您给出的示例中,禁用探查器应该可以解决问题:

In the example you gave, disabling the profiler should do the trick:

$_db = Zend_Db_Table::getDefaultAdapter();
$_db->getProfiler()->setEnabled(false);

$start_memory = memory_get_usage();

for ($i=0; $i<1000; $i++) {
    $update_query = "UPDATE table SET field='value'";
    $_db->query($update_query);
}

echo 'memory used: '.(memory_get_usage()-$start_memory);

这篇关于如何在zend中清理/释放数据库查询内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆