关于shell调用MySQL存储过程的问题

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 前天收到一个任务,要统计每天ETL的运行情况,感觉每天从给的SQL语句一个一个的查询影响效率。 就行用写一个存储过程来实现,然后通过shell调用这个存储过程来自动跑数据,写到相应的文件里~ 思路就是这样,废话不说,上一下写好的代码。 DELIMITER // create procedure sp_etl_info_day (in statis_date int)begin

前天收到一个任务,要统计每天ETL的运行情况,感觉每天从给的SQL语句一个一个的查询影响效率。

就行用写一个存储过程来实现,然后通过shell调用这个存储过程来自动跑数据,写到相应的文件里~

思路就是这样,废话不说,上一下写好的代码。

DELIMITER // 
create procedure sp_etl_info_day (in statis_date int)
begin
declare num_day int default 0;
declare num_month int default 0;
declare num_hour int default 0;
declare num_total int default 0;
declare num_add_lastday int default 0;
declare num_schedule_run int default 0;
declare num_run int default 0;
declare num_succeed int default 0;
declare num_failed int default 0;
declare num_failed1 int default 0;
declare num_killed int default 0;
declare num_waited int default 0;
declare num_norun int default 0;
declare num_man_run int default 0;
declare num_norun_lastday int default 0;
declare rate_wz_etl FLOAT default 0;
declare rate_js_etl FLOAT default 0;
select count(*) into num_day from schedule where isOnline=1 and scheduleOption like "%1#day%";
select count(*) into num_month from schedule where isOnline=1 and scheduleOption like "%1#month%";
select count(*) into num_hour from schedule where isOnline=1 and scheduleOption like "%1#hour%";
set num_total=num_day+num_month+num_hour;
select count(distinct scheduleId) into num_schedule_run from schedule_execution where DATE_FORMAT(startTime,'%Y%m%d')=statis_date and scheduleId in ( select id from schedule where isOnline=1);
select count(*) into num_run from (select count(*) as count, scheduleId,period from schedule_execution where DATE_FORMAT(startTime,'%Y%m%d')=statis_date and scheduleId in ( select id from schedule where isOnline=1) group by scheduleId) a;
select count(*) into num_failed from (select count(*) as count, scheduleId from schedule_execution where DATE_FORMAT(startTime,'%Y%m%d')=statis_date and scheduleId in ( select id from schedule where isOnline=1) and status="Failed" group by scheduleId) a where a.scheduleId not in (select scheduleId from schedule_execution where DATE_FORMAT(startTime,'%Y%m%d')=statis_date and scheduleId in ( select id from schedule where isOnline=1) and status="Succeed" group by scheduleId);
select count(*) into num_killed from (select count(*) as count, scheduleId from schedule_execution where DATE_FORMAT(startTime,'%Y%m%d')=statis_date and scheduleId in ( select id from schedule where isOnline=1) and status="Killed" group by scheduleId) a where a.scheduleId not in (select scheduleId from schedule_execution where DATE_FORMAT(startTime,'%Y%m%d')=statis_date and scheduleId in ( select id from schedule where isOnline=1) and status="Succeed" group by scheduleId);
set num_succeed=num_run-num_failed-num_killed;
set rate_wz_etl=round(num_succeed/num_run,4);
set num_failed1=num_failed+num_killed;
select CONCAT("日:",num_day,"个",",月:",num_month,"个",",小时:",num_hour,"个",",计划运行控制流",num_schedule_run,"个",",共",num_total,"个"
,',昨天共启动控制流:',num_run,'个',',正常执行成功',num_succeed,'个',',报错',num_failed1,'个',',昨天调度运行完整率:',rate_wz_etl) as "ETL日报";
select reserved2, flowName,source from flow where id in (
select originalFlowId from schedule where id in (
select distinct scheduleId from schedule_execution where DATE_FORMAT(startTime,'%Y%m%d')=statis_date and scheduleId in ( select id from schedule where isOnline=1) and status="Failed"
and scheduleId not in(
select scheduleId from schedule_execution where DATE_FORMAT(startTime,'%Y%m%d')=statis_date and scheduleId in ( select id from schedule where isOnline=1) and status="Succeed" )));

end ;//
DELIMITER ;

以上是mysql存储过程~很简单的~

过程写好,下面就开始编写shell调用该存储过程了~

#!/bin/sh  
  
##  用来统计ETL日运行情况  
##  the script is executed at 10 o'clock by every day.  
DATE=$(date +%Y%m%d --date '1 days ago')  
USER=user  
PASSWD=****** 
DATABASE=user  
time=$(date +%Y%m%d --date '1 days ago') 
FILE=/home/mysql/etl_log/log/ETL_$time.txt  

  
echo "the date:"${DATE}  
  
SQL="mysql -u${USER} -p${PASSWD} -D ${DATABASE} -e \"call sp_etl_info_day(${DATE})\""  
echo ${SQL}
echo $SQL>>${FILE} 
 
mysql -u${USER} -p${PASSWD} -D ${DATABASE} -e "call sp_etl_info_day(${DATE})">>$FILE

exit

然后设置crontab就可以了~

以后每天只要去对应的目录下查看文件就行啦~~

省去了不少时间~

我想这大概就是思路,在工作期间,尽量避免重复的工作量,把程序能做的让程序自动做~这样可以更高效的工作~

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
16天前
|
存储 SQL 关系型数据库
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
|
1月前
|
存储 关系型数据库 MySQL
Mysql基础第二十六天,使用存储过程
Mysql基础第二十六天,使用存储过程
28 0
Mysql基础第二十六天,使用存储过程
|
1月前
|
存储 SQL 关系型数据库
【MySQL 数据库】9、存储过程
【MySQL 数据库】9、存储过程
204 0
|
10天前
|
存储 SQL 关系型数据库
mysql存储过程示例
mysql存储过程示例
11 0
|
2月前
|
存储 SQL 关系型数据库
[MySQL]存储过程
[MySQL]存储过程
74 0
[MySQL]存储过程
|
2月前
|
存储 关系型数据库 MySQL
MySQL 中的存储过程-3
MySQL 中的存储过程
20 0
|
2月前
|
存储 SQL 关系型数据库
MySQL 中的存储过程-2
MySQL 中的存储过程
18 0
|
3月前
|
存储 关系型数据库 MySQL
MySQL-调用存储过程
MySQL-调用存储过程
101 2
|
3月前
|
存储 关系型数据库 MySQL
MySQL 中创建存储过程
MySQL 中创建存储过程
24 1
|
3月前
|
存储 SQL 数据库
MySQL-存储过程概述
MySQL-存储过程概述
121 1