分析时间段内对表的操作次数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分析时间段内对表的操作次数相关的知识,希望对你有一定的参考价值。
分析某个时间段内,表的select、insert、update、delete次数。需要用到percona-toolkit包中的一个工具pt-query-digest,脚本如下:
[[email protected] ~]# cat get_list.sh #!/bin/bash set -x #slow_file=ai-db1-slow.log slow_file=$1 if [ $# != 1 ] ; then echo "USAGE: $0 slow.log" echo " e.g.: $0 api-db1-slow.log" exit 1; fi pt-query-digest --limit 10000 $slow_file > /tmp/tmp_file bn=`grep -n "# 1 0x" /tmp/tmp_file|awk -F ‘:‘ ‘{print $1}‘` tn=`grep -n "# Query 1:" /tmp/tmp_file |awk -F ‘:‘ ‘{print $1}‘` en=`expr $tn - 2` sed -n "$bn,$en"p /tmp/tmp_file > /tmp/table_file cat /tmp/table_file|awk ‘{print $6","$9","$10}‘ > /tmp/table_source /usr/local/mysql/bin/mysql -uroot -pxxxxxxxx -S /tmp/mysql_3308.sock <<EOF use sykdb; drop table slow_log; create table slow_log ( cnt varchar(30), type varchar(30), tname varchar(30) ); drop table slow_table; create table slow_table ( tname varchar(30), select_cnt varchar(30), insert_cnt varchar(30), update_cnt varchar(30), delete_cnt varchar(30) ); load data infile ‘/tmp/table_source‘ into table slow_log FIELDS TERMINATED BY ‘,‘; delete from slow_log where type=‘‘; insert into slow_table(tname) select distinct(tname) from slow_log; update slow_table t set t.select_cnt=(select sum(cnt) from slow_log l where l.type=‘select‘ and l.tname=t.tname group by l.tname); update slow_table t set t.insert_cnt=(select sum(cnt) from slow_log l where l.type=‘insert‘ and l.tname=t.tname group by l.tname); update slow_table t set t.update_cnt=(select sum(cnt) from slow_log l where l.type=‘update‘ and l.tname=t.tname group by l.tname); update slow_table t set t.delete_cnt=(select sum(cnt) from slow_log l where l.type=‘delete‘ and l.tname=t.tname group by l.tname); select * from slow_table; EOF #end of script
本文出自 “刚刚出壳的小鸟” 博客,请务必保留此出处http://qhd2004.blog.51cto.com/629417/1940512
以上是关于分析时间段内对表的操作次数的主要内容,如果未能解决你的问题,请参考以下文章