温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL 管理长时间运行查询

发布时间:2020-07-21 20:05:09 来源:网络 阅读:476 作者:会说话的鱼 栏目: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
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI