当前位置:Gxlcms > 数据库问题 > mysql之存储过程(四)--批量更新操作

mysql之存储过程(四)--批量更新操作

时间:2021-07-01 10:21:17 帮助过:126人阅读

=b.busi_id and b.pid=1242343324),modified=now() where pid=21343   因此,写一个存储过程解决,存储过程一条条处理,太慢且消耗本地IO,所以写了个批量更新的存储过程。 如下:   DELIMITER &&
CREATE PROCEDURE updateTimeV1(IN comCount bigint)
BEGIN
    #Routine body goes here...

  declare i int;
  set i=0;
  set @sum=(SELECT count(DISTINCT order_no) FROM `B` WHERE `pid`=1242343324 and status=9 );
  set @log = "log query ....";
  select CONCAT(@log,@sum," 条");
 
  while i<=@sum do
   set i=i+1;
   set @busi_id =( SELECT DISTINCT busi_id FROM `A` WHERE `pid`=1242343324  limit i,1);
   set @other_col =(SELECT other_col FROM `B` where `pid`=1242343324 and yn =1 and `busi_id` = @busi_id  limit 1);
   if @busi_id is NULL THEN
    select CONCAT(@log," busi_id is null");
   elseif @other_col is NULL THEN
    select CONCAT(@log," other_col is null");
   else
      #START TRANSACTION;
      /** 关闭事务的自动提交 */  
      SET autocommit = 0;
      update A set new_column =@other_col,modified = now() where  `pid`=1242343324  and busi_id=@busi_id  and status =15;  
   ##   if mod(i,comCount)=0 then commit;
    if mod(i,1000)=0 then commit;
   end if;
   
   end if;
     
   end while;
   commit; #最后不足1000条时提交
   SET autocommit = 1;

END&&
delimiter ;
  备注:       1、测试了一下一条一条执行,一小时大概20W行更新       2、mod(i,1000)这块得注意 ,如果把set i=i+1;放到最后,在上面的存储过程批量就不会生效。

mysql之存储过程(四)--批量更新操作

标签:color   关联   proc   family   upd   生产环境   操作   ica   datetime   

人气教程排行