批量执行SQL文件,统计每个文件的执行时间

本文介绍了一种批量执行SQL文件的方法,并记录每个文件的执行时间,通过使用shell命令和sed编辑器来自动化整个过程。

     当有一批sql文件需要实行的时候,每个文件执行的时间不一样, 上线之前需要记录运行时间,如果sql文件不能并行执行必须按照顺序执行(假设已经通过测试不会出错),那么就得守在终端前面等待一个sql执行完之后再执行下一个。将所有文件名集中在一个sql里面执行可以省下大把的时光。并且还能记录每个文件执行的时间点。

  

 1、将所有sql文件名写到一个sql文件里,用 ls  >  all.sql ,打开文件需要删除 all.sql 行 ,如下:

 

2、利用sed编辑文件

sed 's/\([^;]*\.sql\)/set @start:=unix_timestamp(),@sqlname="\1";source \1/; s/$/; select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime /g ' all.sql

 

 3、结果如下,执行该脚本即可:

 1 select @start:=unix_timestamp(),@sqlname:="optimize-1-role.sql";source optimize-1-role.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
 2 select @start:=unix_timestamp(),@sqlname:="optimize-2-couple.sql";source optimize-2-couple.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
 3 select @start:=unix_timestamp(),@sqlname:="optimize-2-dance-group.sql";source optimize-2-dance-group.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
 4 select @start:=unix_timestamp(),@sqlname:="optimize-2-event.sql";source optimize-2-event.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
 5 select @start:=unix_timestamp(),@sqlname:="optimize-2-friend.sql";source optimize-2-friend.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
 6 select @start:=unix_timestamp(),@sqlname:="optimize-2-item.sql";source optimize-2-item.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
 7 select @start:=unix_timestamp(),@sqlname:="optimize-2-magic-lamp.sql";source optimize-2-magic-lamp.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
 8 select @start:=unix_timestamp(),@sqlname:="optimize-2-mail.sql";source optimize-2-mail.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
 9 select @start:=unix_timestamp(),@sqlname:="optimize-2-medal-progress.sql";source optimize-2-medal-progress.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
10 select @start:=unix_timestamp(),@sqlname:="optimize-2-medal.sql";source optimize-2-medal.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
11 select @start:=unix_timestamp(),@sqlname:="optimize-2-other-2.sql";source optimize-2-other-2.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
12 select @start:=unix_timestamp(),@sqlname:="optimize-2-other.sql";source optimize-2-other.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
13 select @start:=unix_timestamp(),@sqlname:="optimize-2-player.sql";source optimize-2-player.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
14 select @start:=unix_timestamp(),@sqlname:="optimize-2-quest.sql";source optimize-2-quest.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;
15 select @start:=unix_timestamp(),@sqlname:="optimize-3-rank.sql";source optimize-3-rank.sql;select @sqlname,sec_to_time(@end:=unix_timestamp() - @start) as costtime;

 

转载于:https://www.cnblogs.com/huangjinbin/p/8557187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值