MySQL数据库出现慢查询的危害

发布时间:2020-08-04 06:32:22 作者:会说话的鱼
来源:网络 阅读:1655

1、MySQL数据库当出现慢查询,是比较危险的,一旦有其他的DDL操作,可能会造成整个数据库的等待

可以分以下几种情况:

当表是MyiSAM表,对表有慢查询,不阻塞Select,对该表的其他DML,DDL操作都会被阻塞,比如出现Wating for table level lock,数据库中一定不能还存在MyiSAM表

当表是Innodb表,当表上有慢查询,不阻塞Select 和DML,其他的DDL操作都会被阻塞,比如出现waiting for table metadata lock


综上,当数据库中存在慢查询时,是比较危险的,当执行备份,create index ,alter  table , flush table 等操作时就会造成数据库的等待


解决办法:

1、对数据库中执行时间较长的Select进行监控,并及时报警

2、如果允许的话,写脚本,发现较长的select语句,直接kill,并记录日志中




-B, --batch         Don't use history file. Disable interactive behavior.  

-s, --silent        Be more silent. Print results with a tab as separator,each row on new line.

-e, --execute=name  Execute command and quit. (Disables --force and historyfile.)


#如果数据库中当前有大量的select,可以过滤掉,只kill waiting的

cat killWaitSession.sh

  

#!/bin/bash
for i in `mysql -Bse 'show full processlist' | grep -i select |grep -i "Waiting | awk  '{print $1}'`
do
        mysql -Bse "kill  $i"
done



show processlist的command的状态有很多,其中Query代表正在执行的命令


Query  : The thread is executing a statement.


cat killLongQuerySession.sh

#!/bin/bash
executetime=(`mysql -Bse 'show  processlist'| grep 'Query'|awk  '{print $6 " " $1}'|sort -rn|head -1`)  #第6列是运行时间,第一列为session id
time=${executetime[0]}
id=${executetime[1]}
while :
do
    maxtime=300
    if [ $time  -gt  $maxtime  ] ; then
        echo $time $id >> /tmp/killqueryid.log
        mysql -Bse "kill  $id"
    #else
    #   echo $time $id
    fi
    sleep 10 #睡眠10s
done



按MySQL中执行时间反向排序

 mysqladmin processlist --verbose |grep 'Query'|awk -F "|" '{print $7 $2 $9}'|sort -rn -k1


参考:

https://blog.51cto.com/jim123/1836712

https://dev.mysql.com/doc/refman/5.7/en/show-processlist.html

https://dev.mysql.com/doc/refman/5.7/en/thread-commands.html


推荐阅读:
  1. DNS劫持的发生会打开什么危害?
  2. DNS劫持的危害严重吗?

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

mysql 慢查询 l数据库

上一篇:ubuntu搭建SVN服务器

下一篇:12c ADG 延迟报警

相关阅读

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

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