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

前天收到一个任务,要统计每天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就可以了~

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

省去了不少时间~

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

时间: 2024-08-30 21:51:44

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

java调用mysql存储过程实例分析

  本文实例讲述了java调用mysql存储过程的方法.分享给大家供大家参考.具体如下: 数据库的测试代码如下 : 1.新建表test ? 1 2 3 4 5 create table test( field1 int not null ) TYPE=MyISAM ; insert into test(field1) values(1); 2.删除已存在的存储过程: ? 1 2 3 -- 删除储存过程 delimiter // -- 定义结束符号 drop procedure p_test; 3

php调用MySQL存储过程的方法集合

本篇文章是对php调用MySQL存储过程的方法进行了集合与汇总,需要的朋友参考下   类型一:调用带输入.输出类型参数的方法 复制代码 代码如下: $returnValue = ''; try {  mysql_query ( "set @Return" );  $spname = 'P__Test_GetInfo1';  mysql_query ( "call $spname(@Return, '{$userId}', '{$pwd}')" ) or die (

关于mybatis 怎么调用mysql存储过程

问题描述 关于mybatis 怎么调用mysql存储过程 这个过程用到2个表 我想问一下dao层该怎么写 controller又该怎么写 大神能不能详细点 我脑袋有点笨笨的 解决方案 建立存储过程:DELIMITER $$ USE test$$ DROP PROCEDURE IF EXISTS user_user_selectUserByProAndCity_sp$$ CREATE DEFINER=demao@% PROCEDURE user_user_selectUserByProAndCit

mysql c# 存储过程-C#调用MySql存储过程报错,请各位帮忙解答一下

问题描述 C#调用MySql存储过程报错,请各位帮忙解答一下 public DataSet GetPagerList(string tableName, string primaryKey, string columns, string whereSql, string sortField, int? pageSize, int? currPage, bool isDesc) { MySqlParameter[] param = new MySqlParameter[]{ new MySqlPa

Mybatis调用MySQL存储过程的简单实现

1.存储过程的简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储过程带有参数)来调用执行它. 一个存储过程是一个可编程的函数,它在数据库中创建并保存.它可以有SQL语句和一些特殊的控制结构组成.当希望在不同的应用程序或平台上执行相同的函数,或者封装特定功能时,存储过程是非常有用的.数据库中的存储过程可以看做是对编程中

mysql-ODBC调用MYSQL存储过程

问题描述 ODBC调用MYSQL存储过程 ODBC访问MySQL地带OUT参数的存储过程时出错,提示信息为:OUT or INOUT argument 1 for routine test.ProcTest2 is not a variable or NEW pseudo-variable in BEFORE trigger 查了资料,貌似是MySQL的bug,是这样的吗?求各位大侠帮帮忙,谢谢了! 解决方案 http://blog.sina.com.cn/s/blog_4b3ad3b70100

存储过程 带参数-java调用mysql存储过程输出参数

问题描述 java调用mysql存储过程输出参数 CREATE PROCEDURE T_WEATHER_PRO (IN tmdn VARCHAR(12),OUT tmsg VARCHAR(12)) BEGIN DECLARE states,beginTime,endTime VARCHAR(2) ; SELECT T_STATES,T_BEGINTIME,T_ENDTIME INTO states,beginTime,endTime FROM T_USER_WEATHER WHERE T_USE

Java实现调用MySQL存储过程详解_java

前言 存储过程(Stored Procedure)是存储在数据库中经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它. Java调用MySQL的存储过程,需要用JDBC连接,环境eclipse 首先查看MySQL中的数据库的存储过程,接着编写代码调用 mysql> show procedure status; +------+-------------+-----------+----------------+-------------

Java调用MySQL存储过程并获得返回值的方法_java

本文实例讲述了Java调用MySQL存储过程并获得返回值的方法.分享给大家供大家参考.具体如下: private void empsInDept(Connection myConnect, int deptId) throws SQLException { CallableStatement cStmt = myConnect.prepareCall("{CALL sp_emps_in_dept(?)}"); cStmt.setInt(1, deptId); cStmt.execute