tags:

views:

34

answers:

2

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

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.

+3  A: 

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.

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.

jpluijmers
I was thinking along the lines of setting some connection property.I'm well aware of SHOW PROCESSLIST and KILL. I was hoping for something nicer
Ghostrider
A: 

You probably want to create DAEMONS for this sort of thing but you could also use cron.

Just have a script looking at set intervals for queries above xyz time and kill them.

show processlist
this is obviously mysql

kill query 123
this is still mysql (and not system kill)

Apart from that. If the queries are running for such long times... they must be optimized. That is it. If you are running a server for other people of should warn them and then disable offensive scripts.

Frankie