我能限制查询运行的最大时间吗?

时间:2022-03-11 07:15:28

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.

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

4 个解决方案

#1


13  

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:

然而,在数据库服务器上每一秒都运行cron作业,连接并执行以下操作并不少见:

  1. SHOW PROCESSLIST
  2. 显示PROCESSLIST
  3. Find all connections with a query time larger than your maximum desired time
  4. 查找所有连接,查询时间大于最大期望时间
  5. Run KILL [process id] for each of those processes
  6. 为每个进程运行KILL[进程id]。

#2


5  

You could use a query as follows:

您可以使用以下查询:

SELECT MAX_STATEMENT_TIME=1000 * FROM table;

UPDATE: You should use max_execution_time instead.

更新:应该使用max_execution_time。

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

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

#3


4  

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

与此同时,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.

-通过改进的服务器端语句超时支持减少不必要的工作。这允许服务器主动取消运行时间超过毫秒级超时的查询。

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

看到http://engineering.twitter.com/2012/04/mysql-at-twitter.html和https://github.com/twitter/mysql/wiki/Statement-Timeout

#4


2  

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

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

“MySQL 5.7.4引入了为*只读选择语句设置服务器端执行时间限制(以毫秒为单位)的能力”。

SET GLOBAL MAX_STATEMENT_TIME=1000;
SET SESSION MAX_STATEMENT_TIME=2000;
SELECT MAX_STATEMENT_TIME=1000 * FROM table;

#1


13  

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:

然而,在数据库服务器上每一秒都运行cron作业,连接并执行以下操作并不少见:

  1. SHOW PROCESSLIST
  2. 显示PROCESSLIST
  3. Find all connections with a query time larger than your maximum desired time
  4. 查找所有连接,查询时间大于最大期望时间
  5. Run KILL [process id] for each of those processes
  6. 为每个进程运行KILL[进程id]。

#2


5  

You could use a query as follows:

您可以使用以下查询:

SELECT MAX_STATEMENT_TIME=1000 * FROM table;

UPDATE: You should use max_execution_time instead.

更新:应该使用max_execution_time。

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

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

#3


4  

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

与此同时,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.

-通过改进的服务器端语句超时支持减少不必要的工作。这允许服务器主动取消运行时间超过毫秒级超时的查询。

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

看到http://engineering.twitter.com/2012/04/mysql-at-twitter.html和https://github.com/twitter/mysql/wiki/Statement-Timeout

#4


2  

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

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

“MySQL 5.7.4引入了为*只读选择语句设置服务器端执行时间限制(以毫秒为单位)的能力”。

SET GLOBAL MAX_STATEMENT_TIME=1000;
SET SESSION MAX_STATEMENT_TIME=2000;
SELECT MAX_STATEMENT_TIME=1000 * FROM table;