分析時間段內對錶的操作次數

分析某個時間段內,表的select、insert、update、delete次數。需要用到percona-toolkit包中的一個工具pt-query-digest,腳本如下:

[root@syk ~]# 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章