从PHP强制MysqlQuery超时 [英] Force timeout on a MysqlQuery from PHP

查看:94
本文介绍了从PHP强制MysqlQuery超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在一定时间内未返回查询,是否可以通过PHP强制Mysql终止查询?

Is there a way to force Mysql from PHP to kill a query if it didn't return within a certain time frame?

有时我会看到昂贵的查询运行了几个小时(很明显,这一次HTTP连接超时或用户离开了).这样的查询一旦积累起来,就会开始严重影响整体性能.

I sometimes see expensive queries running for hours (obviously by this time HTTP connection timed out or the user has left). Once enough such queries accumulate it starts to affect the overall performance badly.

推荐答案

长时间运行的查询表明设计不佳.最好检查有问题的查询以及如何对其进行优化.这只会忽略问题.

Long running queries are a sign of poor design. It is best to inspect the queries in question and how they could be optimised. This would be just ignoring the problem.

如果仍然希望这样做,可以使用SHOW PROCESSESLIST命令获取所有正在运行的进程.然后使用KILL x终止客户端连接.但是要使此功能起作用,您必须从另一个PHP脚本中进行检查,因为尚无法进行多线程处理.另外,也不建议让被PHP授权使用的用户弄乱服务器设置.

If still want this you could use the SHOW PROCESSESLIST command to get all running processes. And then use KILL x to kill a client connection. But for this to work you have to check this from another PHP script since multithreading is not yet possible. Also it is not advisable to give users utilized by PHP grants to mess with the server settings.

这篇关于从PHP强制MysqlQuery超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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