如何避免由于长时间的mysql查询而导致php脚本超时 [英] How to keep a php script from timing out because of a long mysql query

查看:302
本文介绍了如何避免由于长时间的mysql查询而导致php脚本超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在由超时的cron任务运行的更新查询.在navicat中执行查询时,平均需要执行五分钟.

I have an update query being run by a cron task that's timing out. The query takes, on average, five minutes to execute when executed in navicat.

代码大致如下所示.很简单:

The code looks roughly like this. It's quite simple:

// $db is a mysqli link
set_time_limit (0); // should keep the script from timing out
$query = "SLOW QUERY";
$result = $db->query($query);
if (!$result)
    echo "error";

即使脚本不应该超时,但等待sql调用所花费的时间似乎仍然会超时.

Even though the script shouldn't timeout, the time spent waiting on the sql call still seems to be subject to a timeout.

是否可以使用异步调用?还是调整超时时间?

Is there an asynchronous call that can be used? Or adjust the timeout?

超时是不同的,因为它是从命令行而不是通过Apache调用的吗?

Is the timeout different because it's being called from the command line rather than through Apache?

谢谢

推荐答案

我在某处遇到了同样的问题,并使用以下代码(文件的前两行)解决了"该问题:

I had the same problem somwhere, and "solved" it with the following code (first two lines of my file):

set_time_limit(0);
ignore_user_abort(1);

这篇关于如何避免由于长时间的mysql查询而导致php脚本超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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