MySQL - can I limit the maximum time allowed for a query to run?
Asked Answered
K

4

17

I'm looking for a way to limit the max running time of a query on mysql server. I figured this could be done through the my.cnf configuration file, but couldn't find anything relevant in the docs. Anyone knows if this could be done? thanks.

Kamerman answered 25/1, 2011 at 14:39 Comment(2)
possible duplicate of How to set a maximum execution time for a mysql query ?Charlatanism
this points to a lock_wait_timeout, which is not what I need. I want an expiration timeout on the query, regardless if a lock was acquired or not.Kamerman
F
16

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 SELECTs, 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:

  1. SHOW PROCESSLIST
  2. Find all connections with a query time larger than your maximum desired time
  3. Run KILL [process id] for each of those processes
Foucault answered 25/1, 2011 at 15:20 Comment(2)
every second may be a bit of overkill depending on your situation. You should put some thought into the need to kill processes in order to keep your max_connections from being hit - all this depends on the load on your specific machine.Schriever
This is no longer true. Please see the posts about using max_execution_time.Jaymie
C
16

You could use a query as follows:

SELECT MAX_STATEMENT_TIME=1000 * FROM table;

UPDATE: You should use max_execution_time instead.

SELECT /*+ MAX_EXECUTION_TIME(1000)*/ * FROM table;

MAX_STATEMENT_TIME was renamed to max_execution_time in MySQL 5.7.8. http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_execution_time

Conceit answered 18/2, 2016 at 16:31 Comment(1)
Try to improve your answerFisch
P
5

In the meantime the Twitter team released their changes to MySQL which implements this:

- Reduce unnecessary work through improved server-side statement timeout support. This allows the server to proactively cancel queries that run longer than a millisecond-granularity timeout.

See http://engineering.twitter.com/2012/04/mysql-at-twitter.html and https://github.com/twitter/mysql/wiki/Statement-Timeout

Passerby answered 6/5, 2012 at 12:24 Comment(0)
T
4

http://mysqlserverteam.com/server-side-select-statement-timeouts/

Interesting upgrade. I will check it:

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

SET GLOBAL MAX_STATEMENT_TIME=1000;
SET SESSION MAX_STATEMENT_TIME=2000;
SELECT MAX_STATEMENT_TIME=1000 * FROM table;
Tinsmith answered 1/12, 2016 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.