Update
As of MySQL 5.7, you can include a MAX_EXECUTION_TIME
optimizer hint in your SELECT
queries to instruct the server to terminate it after the specified time.
As far as I know, if you want to enforce a server-wide timeout, or if you care about queries besides SELECT
s, the original answer is still your only option.
Original answer
There is no way to specify a maximum run time when sending a query to the server to run.
However, it is not uncommon to have a cron job that runs every second on your database server, connecting and doing something like this:
- SHOW PROCESSLIST
- Find all connections with a query time larger than your maximum desired time
- Run KILL [process id] for each of those processes
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…