How can I stop a MySQL query if it takes too long?

后端 未结 9 1606
野趣味
野趣味 2020-12-02 09:22

Is it possible to timeout a query in MySQL?

That is, if any query exceeds the time I specify, it will be killed by MySQL and it will return an error instead of waiti

9条回答
  •  广开言路
    2020-12-02 09:57

    I think this old question needs an updated answer.

    You can set a GLOBAL timeout for all your read-only SELECT queries like this:

    SET GLOBAL MAX_EXECUTION_TIME=1000;

    The time specified is in milliseconds.

    If you want the timeout only for a specific query, you can set it inline like this:

    SELECT /*+ MAX_EXECUTION_TIME(1000) */ my_column FROM my_table WHERE ...

    MySQL returns an error instead of waiting for eternity.

    Note that this method only works for read-only SELECTs. If a SELECT statement is determined not to be read-only, then any timer set for it is cancelled and the following NOTE message is reported to the user:

    Note 1908 Select is not a read only statement, disabling timer

    For statements with subqueries, it limits the top SELECT only. It does not apply to SELECT statements within stored programs. Using the MAX_EXECUTION_TIME hint in SELECT statements within a stored program will be ignored.

提交回复
热议问题