資料統計儲存過程規範(mysql)

2021-07-03 11:26:03 字數 2273 閱讀 8436

在乙個需要做大量資料統計分析的專案裡,免不了需要寫儲存過程。如果過程不多不會有問題,但多了以後,維護和監控會產生很多問題。這時候,最好是對儲存過程進行統一規範,對異常進行處理和對運**況做日誌記錄。也使不同的人編寫的過程格式一致,方便維護。

1、建立日誌表

create table `tb_proc_log` (

`id` int(11) not null auto_increment comment 'id',

`proc_name` varchar(50) null default null comment '過程名稱',

`statu` varchar(20) null default null comment '狀態',

`start_time` datetime null default current_timestamp comment '開始時間',

`end_time` datetime null default null comment '結束時間',

`cost_time` int(11) null default null comment '耗時',

`error_code` varchar(50) null default null comment 'error_code',

`error_msg` varchar(200) null default null comment 'error_msg',

`step` int(11) null default null comment 'step',

primary key (`id`)

)comment='儲存過程執行日誌'

collate='utf8_general_ci'

engine=innodb

auto_increment=347

;

2、建立儲存過程

delimiter //

create definer=`root`@`%` function `proc_temp`() returns int(11)

begin

#宣告異常,當出現錯誤時退出執行,並記錄日誌。

declare _error_code, _error_msg, _proc_name varchar(200);

declare _proc_log_id, _step int default 0;

declare exit handler for sqlexception

begin

get diagnostics condition 1 _error_code=returned_sqlstate, _error_msg = message_text;

update tb_proc_log set statu='失敗', end_time=now(), cost_time=(unix_timestamp(now()) - unix_timestamp(start_time)), error_code=_error_code, error_msg=_error_msg, step=_step where id=_proc_log_id;

return(1);

end;

set _proc_name='proc_temp';

#記錄日誌

insert into tb_proc_log(proc_name, statu, step) values (_proc_name, '執行中', _step);

set _proc_log_id = (select last_insert_id());

/************************************業務**start****************************************/

#***x資料統計

set _step = 1;

#***x資料統計

set _step = 2;

/************************************業務**end****************************************/

#記錄日誌

update tb_proc_log set statu='成功', end_time=now(), cost_time=(unix_timestamp(now()) - unix_timestamp(start_time)), step=_step where id=_proc_log_id;

return(0);

end//

delimiter ;

資料統計頁面

麵包屑導航區 class el icon arrow right 首頁 el breadcrumb item 資料統計 el breadcrumb item 資料包表 el breadcrumb item el breadcrumb 卡片檢視區域 為echarts準備乙個具備大小 寬高 的dom m...

tensorflow資料統計

本篇內容包括,tf.norm 張量的範數 tf.reduce min max 最大最小值 tf.argmax argmin 最大最小值的位置 tf.equal 張量的比較 tf.unique 張量的獨特值 1.tf.norm 二範數 x 2 xk 2 1 2 一範數 x 1 xk 無窮範數 x ma...

表元資料統計 統計MySQL資料表大小

需要統計表的元資料基本資訊 如統計表的容量大小 行數等,可引數如下語句 select table name,data length 1024 1024 as datam index length 1024 1024 as indexm,data length index length 1024 10...