How to set a maximum execution time for a mysql query?

microry picture microry · Jan 6, 2009 · Viewed 106k times · Source

I would like to set a maximum execution time for sql queries like set_time_limit() in php. How can I do ?

Answer

Westy92 picture Westy92 · Apr 27, 2014

I thought it has been around a little longer, but according to this,

MySQL 5.7.4 introduces the ability to set server side execution time limits, specified in milliseconds, for top level read-only SELECT statements.

SELECT 
/*+ MAX_EXECUTION_TIME = 1000 */ --in milliseconds
* 
FROM table;

Note that this only works for read-only SELECT statements.

Update: This variable was added in MySQL 5.7.4 and renamed to max_execution_time in MySQL 5.7.8. (source)