Home >Database >Mysql Tutorial >mysql使用游标进行删除操作的存储过程_MySQL

mysql使用游标进行删除操作的存储过程_MySQL

WBOY
WBOYOriginal
2016-06-01 12:59:561164browse

BEGIN
DECLARE hprocessInstanceId bigint DEFAULT 0; -- 历史流程实例id
DECLARE hprocessInstanceIdStarttime CHAR default ''; -- 历史流程实例启动时间
DECLARE hprocessInstanceIdEndtime CHAR default ''; -- 历史流程实例结束时间

DECLARE hactinstId BIGINT default 0; -- 历史活动实例id

DECLARE htaskId BIGINT default 0; -- 历史人工任务id

DECLARE hvarId BIGINT default 0; -- 历史流程变量id

DECLARE rexecutionId bigint default 0; -- 正在执行流程实例id

DECLARE rvarId bigint default 0; -- 正在执行流程变量id

DECLARE rtaskId bigint default 0; -- 正在执行人工任务id

DECLARE rswinmlanceId bigint DEFAULT 0; -- 泳道id,为了删除partation表记录,本项目无记录

DECLARE processCompleteFlag int default 0; -- 流程是否结束标识
DECLARE taskCompleteFlag int default 0; -- 任务是否结束标识
DECLARE doneFlag INT DEFAULT 0; -- 完成标识,0:未完成;1:已完成
DECLARE notfound INT DEFAULT 0;-- 是否未找到数据 标记
-- 启动事物
-- START TRANSACTION;


/* 声明历史流程实例的游标 */
DECLARE hprocessInstanceRS CURSOR FOR SELECT dbid_,START_,END_ FROM jbpm4_hist_procinst where START_>=&#39;2014-0-0 0:0:0&#39; and START_<&#39;2015-0-0 0:0:0&#39;;

/* 声明历史活动实例的游标 */
DECLARE hactinstRS CURSOR FOR SELECT dbid_,HTASK_ FROM jbpm4_hist_actinst where HPROCI_=hprocessInstanceId;

/* 声明历史活动实例的游标 */
DECLARE htaskRS CURSOR FOR SELECT dbid_ FROM jbpm4_hist_task where DBID_=hactinstId;

/* 声明历史活动实例的游标 */
DECLARE hvarRS CURSOR FOR SELECT dbid_ FROM jbpm4_hist_var where HTASK_=rtaskId;

/** 声明正在执行流程实例的游标(历史表中因为各种原因未完成的) **/
DECLARE rexecutionRS CURSOR FOR SELECT dbid_ FROM jbpm4_execution where DBID_=hprocessInstanceId;

/** 声明正在执行流程变量的游标(只删除2014年条件下由于各种原因未完成的流程实例所对应的流程变量) */
DECLARE rvarRS CURSOR FOR SELECT dbid_ FROM jbpm4_variable where EXECUTION_=hprocessInstanceId;

/** 声明正在执行的人工任务的游标(只是2014年开始的流程实例所对应的) **/
DECLARE rtaskRS CURSOR FOR SELECT dbid_ FROM jbpm4_task where DBID_=rtaskId;

/** 声明泳道的结果集游标,为了删除paritation表,该项目没有记录,实际删除条数为0 **/
DECLARE rswinmlanceRS CURSOR FOR SELECT dbid_ FROM jbpm4_swimlane where dbid_=rswinmlanceId;

/* 异常处理 */
DECLARE CONTINUE HANDLER FOR SQLSTATE &#39;02000&#39; SET doneFlag = 1;

/** 删除s,使用嵌套循环..... **/

OPEN hprocessInstanceRS;
FETCH hprocessInstanceRS INTO hprocessInstanceId,hprocessInstanceIdStarttime,hprocessInstanceIdEndtime;-- 获取历史流程实例表的数据数据

REPEAT
IF hprocessInstanceIdEndtime=&#39;&#39; THEN
-- 没有结束,执行删除正在执行的流程实例表
/** 1.查询正在执行的流程实例记录s **/
SET rexecutionId=hprocessInstanceId; -- 未完成的流程实例与正在执行的流程实例id做对应
OPEN rexecutionRS;
FETCH rexecutionRS INTO rexecutionId;
REPEAT
/** 2.查询该流程实例下的所有正在执行的流程变量记录s 2**/
OPEN hvarRS;
FETCH hvarRS INTO rvarId;
REPEAT
/** 3.删除正在执行的流程变量所对应的人工任务记录s 3**/
delete from jbpm4_task where dbid_=rvarId;
/** 3.删除正在执行的流程变量所对应的人工任务记录e 3**/
delete from jbpm4_variable where dbid_=rvarId; -- 单条删除流程变量记录
FETCH hvarRS INTO rvarId;
UNTIL doneFlag END REPEAT;
CLOSE hvarRS;
/** 2.查询该流程实例下的所有正在执行的流程变量记录e 2**/
delete from jbpm4_execution where dbid_=rexecutionId; -- 单条删除流程对象记录
FETCH rexecutionRS INTO rexecutionId;
UNTIL doneFlag END REPEAT;
CLOSE rexecutionRS;

END IF;
/*** ======删除历史流程记录表相关数据===== **/
/** 1.查询活动实例表 s **/
OPEN hactinstRS;
FETCH hactinstRS INTO hactinstId,htaskId;
REPEAT
/** 2.查询历史人工活动表记录s **/
OPEN htaskRS;
FETCH htaskRS INTO htaskId;
REPEAT
/** 3.删除历史人工任务 **/
delete from jbpm4_hist_task where dbid_=htaskId;
FETCH htaskRS INTO htaskId;
UNTIL doneFlag END REPEAT;
CLOSE htaskRS;
/** 2.查询历史人工活动表记录s **/
FETCH hactinstRS INTO hactinstId,htaskId;
UNTIL doneFlag END REPEAT;
CLOSE hactinstRS;

/** 1.查询活动实例表 e **/
/*** ======删除历史流程记录表相关数据===== **/
/** 删除历史活动实例表 **/
delete from jbpm4_hist_actinst where HPROCI_=hprocessInstanceId;

SET doneFlag=0;
FETCH hprocessInstanceRS INTO hprocessInstanceId,hprocessInstanceIdStarttime,hprocessInstanceIdEndtime;-- 获取历史流程实例表的数据数据
UNTIL doneFlag END REPEAT;
CLOSE hprocessInstanceRS;
END

使用嵌套之后,10万-百万条数据量删除非常慢,有什么解决方法没有?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn