6

MySQL中2种方法限制查询超时时间

 2 years ago
source link: https://blog.51cto.com/lee90/5279507
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

MySQL中2种方法限制查询超时时间

原创

场景:

某个复杂查询虽然前端YearningSQL的窗口关闭了,但实际SQL已经下发到数据库层去执行了,这种任然会对MySQL造成过大的压力。

解决方法:

方法1、在MySQL8中,可在select后面添加   /*+ MAX_EXECUTION_TIME(5000) */  (单位毫秒) 这种注解的方式,限制查询超时自动熔断。 

此外,在springboot的jdbc连接串里面可以设置全局超时时间(在mybatis 纯sql中可按sql粒度去定义超时时间)。具体看业务方是否有这方面的需求场景。

YearningSQL查询示例如下:

MySQL中2种方法限制查询超时时间_查询超时

方法2、后台通过定时任务启动pt-kill去遍历相关的MySQL,将凡是由yearningsql平台发起的select操作,超过阈值就自动kill掉。

两种方法,都可以达到目的。 方法1的适用面更广,可以推广到业务侧去自行控制sql查询超时阈值。方法2一般只能供DBA用用。

Ref:

 ​https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-connp-props-statements.html​

 ​https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-connp-props-performance-extensions.html​

 ​https://www.cnblogs.com/yankang/p/11053168.html​

 ​https://www.shuzhiduo.com/A/pRdBqm3a5n/​

  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK