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

后端 未结 4 980
[愿得一人]
[愿得一人] 2020-11-27 19:39

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\

相关标签:
4条回答
  • 2020-11-27 20:13

    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

    0 讨论(0)
  • 2020-11-27 20:14

    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

    0 讨论(0)
  • 2020-11-27 20:21

    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
    0 讨论(0)
  • 2020-11-27 20:23

    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;
    
    0 讨论(0)
提交回复
热议问题