MySQL - 我可以限制查询运行所允许的最长时间吗?

sa1*_*125 15 mysql long-running-processes

我正在寻找一种方法来限制mysql服务器上查询的最大运行时间.我认为这可以通过my.cnf配置文件完成,但无法在文档中找到任何相关内容.有谁知道这可以做到吗?谢谢.

Teh*_*ike 14

将查询发送到服务器以运行时,无法指定最长运行时间.

但是,在数据库服务器上每秒运行一次cron作业并连接并执行以下操作并不罕见:

  1. 显示进程列表
  2. 查找查询时间大于最大所需时间的所有连接
  3. 为每个进程运行KILL [进程ID]


Ale*_*pov 10

您可以使用如下查询:

SELECT MAX_STATEMENT_TIME=1000 * FROM table;
Run Code Online (Sandbox Code Playgroud)

更新:您应该使用max_execution_time.

SELECT /*+ MAX_EXECUTION_TIME(1000)*/ * FROM table;
Run Code Online (Sandbox Code Playgroud)

在MySQL 5.7.8中,MAX_STATEMENT_TIME被重命名为max_execution_time. http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_execution_time

  • 尝试改进你的答案 (2认同)

fak*_*ker 5

同时,Twitter团队发布了对MySQL的更改,该更改实现了此功能:

- 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.

参见http://engineering.twitter.com/2012/04/mysql-at-twitter.htmlhttps://github.com/twitter/mysql/wiki/Statement-Timeout