免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1422 | 回复: 2
打印 上一主题 下一主题

[其他] bash脚本运行越来越慢 [复制链接]

论坛徽章:
1
15-16赛季CBA联赛之吉林
日期:2018-05-23 14:31:12
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-04-09 21:50 |只看该作者 |倒序浏览
本帖最后由 gtuiw 于 2017-04-09 21:52 编辑

各位好!

  有shell一个脚本,目的是将远程数据库的数据拉下来写到本地文件。需要拉下来的数据大概有几百万条

  运行了3个小时有6万条数据,之后就放着不管了,2天过去了,登录看了下数据只有30万条。觉得是脚本有效率问题,导致越来越慢。

  下面是运行的脚本,麻烦看下这脚本是不是有问题,还有优化的空间吗?

  先谢了!
  1. #!/usr/bin/env bash

  2. local_mysql='mysql --login-path=local --host=127.0.0.1 -sN -e '
  3. meizu_mysql='mysql --login-path=meizu --host=192.3.XX.XX -sN -e '


  4. declare -a db_name=(“db_1” “db2” “db3” “db4”)
  5. for (( dbn=0; dbn<${#db_name[@]}; dbn++ ))
  6. do
  7.         declare db_in_temp="tr.${db_name[dbn]}_in_temp"
  8.         declare db_out_temp="tr.${db_name[dbn]}_out_temp"

  9.         business_list=`${meizu_mysql}"select distinct business from netflow_${db_name[dbn]}_in_business_cache where clock >= unix_timestamp(subdate(NOW(), INTERVAL 30 DAY)) and clock < unix_timestamp(subdate(NOW(), 1)) and carriers = \"1\";"`
  10.         for p_business in `echo ${business_list}`
  11.         do
  12.                 for((carrier=1; carrier<=4; carrier++))
  13.                 do
  14.                         count_in_temp=`${meizu_mysql}"SELECT COUNT(*) from (SELECT SUM(value) value, business, carriers, from_unixtime(600*ROUND(clock/(10*60)),'%Y-%m-%d %H:%i') time_stamp, clock FROM netflow_${db_name[dbn]}_in_business_cache WHERE clock < UNIX_TIMESTAMP() and clock >= UNIX_TIMESTAMP(subdate(current_date, 60)) and business = \"$p_business\" and carriers = \"$carrier\" GROUP BY time_stamp)c;"`
  15.                         count_in=`echo | awk -v count_i=${count_in_temp} '{print int(count_i * 0.05)}'`

  16.                         business_in_temp=`${meizu_mysql}"SELECT SUM(value) value, business, carriers, from_unixtime(600*ROUND(clock/(10*60)),'%Y-%m-%d %H:%i') time_stamp, clock FROM netflow_${db_name[dbn]}_in_business_cache WHERE clock < UNIX_TIMESTAMP() and clock >= UNIX_TIMESTAMP(subdate(current_date, 60)) and business = \"$p_business\" and carriers = \"$carrier\" GROUP BY time_stamp order by value DESC LIMIT ${count_in},${count_in_temp};"`

  17.                         count_out_temp=`${meizu_mysql}"SELECT COUNT(*) from (SELECT SUM(value) value, business, carriers, from_unixtime(600*ROUND(clock/(10*60)),'%Y-%m-%d %H:%i') time_stamp,  clock FROM netflow_${db_name[dbn]}_out_business_cache WHERE clock < UNIX_TIMESTAMP() and clock >= UNIX_TIMESTAMP(subdate(current_date, 60)) and business = \"$p_business\" and carriers = \"$carrier\" GROUP BY time_stamp)c;"`
  18.                         count_out=`echo | awk -v count_i=${count_out_temp} '{print int(count_i * 0.05)}'`

  19.                         business_out_temp=`${meizu_mysql}"SELECT SUM(value) value, business, carriers, from_unixtime(600*ROUND(clock/(10*60)),'%Y-%m-%d %H:%i') time_stamp,  clock FROM netflow_${db_name[dbn]}_out_business_cache WHERE clock < UNIX_TIMESTAMP() and clock >= UNIX_TIMESTAMP(subdate(current_date, 60)) and business = \"$p_business\" and carriers = \"$carrier\" GROUP BY time_stamp order by value DESC LIMIT ${count_out},${count_out_temp};"`

  20.                         echo "${business_in_temp}" | while read in_line
  21.                         do
  22.                                 business_n=`echo ${in_line} | awk '{print $2}'`
  23.                                 bindwidth=`echo ${in_line} | awk '{printf("%.2f", $1/1024/1024)}'`
  24.                                 time_n=`echo ${in_line} |awk '{print $4, $5}'`

  25.                                 echo "${business_n},${bindwidth},${time_n},${carrier}" >> /tmp/${db_in_temp}
  26.                                
  27.                         done
  28.                         echo "${business_out_temp}" | while read out_line
  29.                         do
  30.                                 business_out=`echo ${out_line} | awk '{print $2}'`
  31.                                 bindwidth_out=`echo ${out_line} | awk '{printf("%.2f", $1/1024/1024)}'`
  32.                                 time_out=`echo ${out_line} |awk '{print $4, $5}'`
  33.                                 echo "${business_out},${bindwidth_out},${time_out},${carrier}" >> /tmp/${db_out_temp}

  34.                         done
  35.                 done
  36.         done
  37. done
复制代码



论坛徽章:
1
未羊
日期:2014-06-16 09:14:34
2 [报告]
发表于 2017-04-10 11:08 |只看该作者
感觉不像是shell有问题,像是sql查询慢...

不过,你可以偿试,将第二个for循环写成函数,丢后台,多进程并发执行

论坛徽章:
39
辰龙
日期:2013-08-21 15:45:192015亚冠之广州富力
日期:2015-05-12 16:34:52亥猪
日期:2015-03-03 17:22:00申猴
日期:2015-03-03 17:21:37未羊
日期:2014-10-10 13:45:41戌狗
日期:2014-06-17 09:53:29巨蟹座
日期:2014-06-12 23:17:17双鱼座
日期:2014-06-10 12:42:44寅虎
日期:2014-06-09 12:52:172015亚冠之卡尔希纳萨夫
日期:2015-05-24 15:24:35黄金圣斗士
日期:2015-12-02 17:25:0815-16赛季CBA联赛之吉林
日期:2017-06-24 16:43:52
3 [报告]
发表于 2017-04-10 11:15 |只看该作者
先定位一下慢在哪
sql语句执行慢还是快?
百万行+四层循环嵌套, 实现方式上是不是可以优化?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP