MySQL 管理长时间运行查询

最常用的查询

select concat ('kill ',id,';') from 
    information_schema.processlist
    where time >= 2
    -- and user = '业务账号'
    and command not in ('sleep','Connect')
    and state not like ('waiting for table%lock');
    and info like '%Metabase%'

mysql -uroot -s -N -p -h  -e "select concat ('kill ',id,';') from information_schema.processlist where INFO like 'SELECT  xxx  FROM%' " > kill.sql

RDS提供的存储过程:

create event my_long_running_query_monitor
on schedule every 5 minute
starts '2015-09-15 11:00:00'
on completion preserve enable do
begin
  declare v_sql varchar(500);
  declare no_more_long_running_query integer default 0;
  declare c_tid cursor for
    select concat ('kill ',id,';') from 
    information_schema.processlist
    where time >= 3600
    and user = substring(current_user(),1,instr(current_user(),'@')-1)
    and command not in ('sleep')
    and state not like ('waiting for table%lock');
  declare continue handler for not found
    set no_more_long_running_query=1;

  open c_tid;
  repeat
    fetch c_tid into v_sql;
    set @v_sql=v_sql;
    prepare stmt from @v_sql;
    execute stmt;
    deallocate prepare stmt;
  until no_more_long_running_query end repeat;
  close c_tid;
end;

参考:https://help.aliyun.com/knowledge_detail/41735.html?spm=a2c4g.11186631.2.20.51106998SvntYb

RDS中的参数

loose_max_statement_time

管理长查询的shell脚本

#!/bin/bash

password=xxxxxx

mysql -uroot -p$password -N -s -e "select concat ('kill ',id,';') from  
    information_schema.processlist 
    where time >= 300 
    -- and user = '业务账号' 
    and command not in ('sleep','Connect')
    and state not like ('waiting for table%lock');" > killmysqlsession.txt

#cat killmysqlsession.txt | while read line
#do
#echo $line
#mysql -uroot -p$password -e "$line"
#done

mysql -uroot -p$password < killmysqlsession.txt

#或者登陆实例source killmysqlsession.txt

转载于:https://blog.51cto.com/395469372/2347264

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值