分析时间段内对表的操作次数

分析某个时间段内,表的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
时间: 2024-08-02 07:01:37

分析时间段内对表的操作次数的相关文章

python分析mysql-binlog,统计时间段内各表的操作次数_2016041301

小鹏鹏装逼课堂之:统计指定时间段数据库表的操作次数.处女作,需要大神帮忙优化.#####注:::以下脚本中路径可能需要各位自行进行修改 实现原理:   1.shell脚本:通过mysqlbinlog将binlog日志文件格式化输出        定制crontab任务,定时通过向mysql传递show master status查看binlog是否变化,若发生变化则格式化输出已经切换掉的binlog文件   2.shell脚本:定制crontab任务,通过比对md5码,查看格式化后的binlog

hadoop中的hive查询cdn访问日志指定时间段内url访问次数最多的前10位(结合python语言)

hadoop环境描述: master节点:node1 slave节点:node2,node3,node4 远端服务器(python连接hive):node29 需求:通过hive查询到cdn日志中指定时间段内url访问次数最多的前10个url ps:用pig查询可以查询文章: http://shineforever.blog.51cto.com/1429204/1571124 说明:python操作远程操作需要使用Thrift接口: hive源码包下面自带Thrift插件: [[email pr

求质数两个方法的好坏分析(是否易懂,操作次数,运算复杂度时间)

方法1: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <malloc.h> 4 #include <stdbool.h> 5 6 int main() 7 { 8 long i,j,n,ans=0; 9 //vis[x]若为true,则代表质数,若为false,则不是质数 10 bool *vis=(bool *) malloc (sizeof(bool)*100000001); 11 long

slq获取某一时间段内的全部数据

条件:根据数据库原有某一时间段数据,获取在筛选时间段内的全部数据信息: 通过分析在这筛选时间段的数据可大体分为以下3种情况: 数据库某字段的开始时间在筛选的开始时间与结束时间范围内 数据库某字段的结束时间在筛选的开始时间与结束时间范围内 数据库某字段的开始时间小于筛选的开始时间,结束时间大于筛选的结束时间 转化为sql语句为: ((startDate between '筛选开始时间' and '筛选结束时间') or (endDate between '筛选开始时间' and '筛选结束时间')

1、对一个正整数算到1需要的最少操作次数

题1:实现一个函数,对一个正整数n,算得到1需要的最少操作次数.操作规则为:如果n为偶数,将其除以2:如果n为奇数,可以加1或减1:一直处理下去:例子:func(7) = 4,可以证明最少需要4次运算n = 7n-1 6n/2 3n-1 2n/2 1 要求:实现函数(实现尽可能高效) int func(unsign int n):n为输入,返回最小的运算次数.给出思路(文字描述),完成代码,并分析你算法的时间复杂度. java源程序: package bfgy.lab.work; import 

mysql对表的操作

mysql对表的操作 表的概念 表示包含数据库中所有数据的数据库对象.表中的数据库对象包含列.索引.触发器.其中触发器是指用户定义的事务命令集合,当对一个表中的数据进行插入.更新或者删除时,这组命令就会自动执行,可以确保数据的安全性和完整性. 创建表 create table tableName( 属性名 字段类型, 属性名 字段类型, -- 属性名 字段类型 ); 最后一个字段类型后面没有逗号,整个语句的小括号后边有分号. 例如:创建一个数据库,并且创建一张表: create database

用redis实现动态时间段内统计排序

问题描述 需要根据某类数据在动态时间段内的统计值对这些数据进行排名.例如按过去24小时内点赞数排名的帖子,每隔一小时计算一次结果.以下描述均针对这个例子展开. 解决思路 针对这种问题,我的第一反应是直接通过 mysql一张数据表记录所有数据的每一条统计值改变的行为,例如记下每个帖子在哪个时间点被谁点赞.排序结果直接通过 select + where + order_by + limit.简单粗暴,但效率低下,扩展性差,而且当数据量很多时,会导致数据库查询效率低下. 那么为了提高效率, mysql

正整数n,算得到1需要的最少操作次数

实现一个函数,对一个正整数n,算得到1需要的最少操作次数:如果n为偶数,将其除以2:如果n为奇数,可以加1或减1:一直处理下去.例子:ret=func(7);ret=4,可以证明最少需要4次运算n=7n--6n/2 3n/2 2n++1要求:实现函数(实现尽可能高效)int func(unsign int n):n为输入,返回最小的运算次数.给出思路(文字描述),完成代码,并分析你算法的时间复杂度.请列举测试方法和思路. -------------------------------------

Java判断一个时间是否在另一个时间段内

需求:当时间在凌晨0点至0点5分之间程序不执行. 也就是实现判断当前时间点是否在00:00:00至00:05:00之间 方法: Java代码 : /** * 判断时间是否在时间段内 * * @param date * 当前时间 yyyy-MM-dd HH:mm:ss * @param strDateBegin * 开始时间 00:00:00 * @param strDateEnd * 结束时间 00:05:00 * @return */ public static boolean isInDat