MySQL 管理长时间运行查询
admin
2023-04-19 13:43:31
0

最常用的查询

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

相关内容

热门资讯

【今日要闻】“毛豆大厅拼三张到... 【今日要闻】“毛豆大厅拼三张到底有挂吗?”(必胜开挂神器)您好,毛豆大厅拼三张这个游戏其实有挂的,确...
单膝跪地勇救脑梗登山客 黄河科... “要不是这位同学,后果不堪设想!”12月24日上午,在黄河科技学院职业技术学院会议室内,被救者王叔的...
俄年度热词聚焦“胜利”“人工智... 新华社莫斯科12月28日电2025年即将结束,俄罗斯多个机构和团体分别推出本国年度热词榜单,上榜热词...
关于微信占存储空间的几个误解 (来源:JSTV荔枝视频) 转自:JSTV荔枝视频 #教你1招快速深度清理手机缓存#【#关于微信占存...
今日重大通报“天天爱麻将究竟有... 今日重大通报“天天爱麻将究竟有挂吗?”(必胜开挂神器)您好,天天爱麻将这个游戏其实有挂的,确实是有挂...
【第一资讯】“阿拉斗牌辅助器?... 网上科普关于“阿拉斗牌有没有挂”话题很是火热,小编也是针对阿拉斗牌作*弊开挂的方法以及开挂对应的知识...
玩家攻略科普“牌乐门到底是不是... 家人们!今天小编来为大家解答牌乐门透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很多...
【第一资讯】“决胜奕福是不是有... 您好:决胜奕福这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏...
今日重大消息“新版九哥牛牛究竟... 网上科普关于“新版九哥牛牛有没有挂”话题很是火热,小编也是针对新版九哥牛牛作*弊开挂的方法以及开挂对...
我来教教您“可乐麻将开挂神器?... 网上科普关于“可乐麻将有没有挂”话题很是火热,小编也是针对可乐麻将作*弊开挂的方法以及开挂对应的知识...