简体   繁体   English

如何限制sql执行时间

[英]How to limit the sql execution time

Some sql is not well written. 有些sql编写得不好。 Sometimes a search costs hours in applications. 有时,搜索会花费数小时申请。 When a application(maybe a website) submit a query which run long time, I have to restart the mysql. 当一个应用程序(可能是一个网站)提交一个运行时间很长的查询时,我必须重新启动mysql。 How can I limit a sql query's execution time in the database side? 如何在数据库端限制sql查询的执行时间?

To auto kill a query in MySQL after a long execution time: 要在很长的执行时间后自动终止MySQL中的查询:

  1. Create a stored procedure as: 创建存储过程:

     DECLARE CURSOR cur1 FOR SELECT ID FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND = 'Query' AND TIME > 120; 

    then inside curosr's loop do: 然后在curosr的循环中做:

     FETCH ID INTO @var_kill_id; KILL QUERY @var_kill_id; 
  2. Create EVENT FOR EVERY 5 SECONDS and just CALL the above procedure inside it. EVENT FOR EVERY 5 SECONDS创建EVENT FOR EVERY 5 SECONDS并在其中CALL上述过程。

Note: KILL QUERY just kills the query and MySQL connection is not broken. 注意:KILL QUERY只会终止查询,并且MySQL连接不会中断。 see here . 看到这里

Also if possible you can try Twitter's mysql fork that suport "max_statement_time" and kills a query exceding it, at a milisecond granularity. 此外,如果可能的话,你可以尝试使用Twitter的mysql fork来支持“max_statement_time”,并以毫秒的粒度杀死除此之外的查询。

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.htmlhttps://github.com/twitter/mysql/wiki/Statement-Timeout

Original source . 原始来源

The only choice is that to open another session, use SHOW PROCESSLIST and then KILL QUERY the one you want to terminate. 唯一的选择是打开另一个会话,使用SHOW PROCESSLIST然后KILL QUERY你要终止的那个。

You can use the mysqladmin command-line tool to issue these commands. 您可以使用mysqladmin命令行工具发出这些命令。

This may not be the exact solution of resticting SQL execution time, but I guess it will be of help. 这可能不是resticting SQL执行时间的确切解决方案,但我想它会有所帮助。 You will be not required to restart your MySQL server at all. 您根本不需要重新启动MySQL服务器。

Try using some MySQL GUI Tool like SQLyog . 尝试使用一些MySQL GUI工具,如SQLyog There you can manually stop executed query,(Check the X button in screen shot) if you think that it is taking longer time. 如果你认为它需要更长的时间,你可以手动停止执行查询(在屏幕截图中检查X按钮)。

And moreover you can kill the query process as well by accessing process list Check other two images in screen shot with steps to kill processes... 此外,您还可以通过访问进程列表来终止查询过程。在屏幕截图中检查其他两个图像,并执行杀死进程的步骤...

杀死流程屏幕截图

Hope it helps.... 希望能帮助到你....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM