= 2 -- and user = 業(yè)務(wù)賬號 and command..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL 管理長時間運行查詢

發(fā)布時間:2020-07-21 20:05:09 來源:網(wǎng)絡(luò) 閱讀:479 作者:會說話的魚 欄目:MySQL數(shù)據(jù)庫

最常用的查詢

select concat ('kill ',id,';') from 
    information_schema.processlist
    where time >= 2
    -- and user = '業(yè)務(wù)賬號'
    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中的參數(shù)

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 = '業(yè)務(wù)賬號' 
    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問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI