ITPub博客

首页 > 数据库 > MySQL > MySQL NDB 批量更新100万行数据

MySQL NDB 批量更新100万行数据

原创 MySQL 作者:feelpurple 时间:2016-08-05 14:19:44 0 删除 编辑
生产NDB数据库中的一张800多万行的大表需要更新部分字段,为减少对数据库性能的影响,编写了一个存储过程来实现:
1. 导出这张表的主键字段,数据量有100多万条
select MSISDN from TEST where LastAccessTimeStamp=0
into outfile '/tmp/TEST_out.txt' fields terminated by ',' ;

2. 在其中一个SQL节点,创建两张临时表
--创建第一张临时表,用于和生产数据库的源表主键进行关联,以更新部分字段
create table tmp_Subscribers_01(id int not null auto_increment primary key,MSISDN char(20)) engine=innodb;
--向第一张临时表导入之前的导出数据
load data infile '/tmp/Subscribers_out.txt' into table tmp_Subscribers_01 fields terminated by ',' (MSISDN);
--创建第二张临时表,用于记录数据更新的进度
create table tmp_Subscribers_02(id int, MSISDN char(20),cdate datetime) engine=innodb;

3. 编写数据更新的存储过程
drop procedure proc_Subscribers_update;
delimiter $$
create procedure proc_Subscribers_update(IN v_fetch_cnt INT, IN v_sleep_secs INT)
begin
DECLARE v_count INT;
DECLARE v_times INT DEFAULT 1;
DECLARE v_max_value INT;
  /*compute the times that the loop runs*/
  select ceil(count(MSISDN)/v_fetch_cnt) into v_count from tmp_Subscribers_01;
  /*compute the maximum rows that have been already updated*/
  WHILE v_times < v_count DO
    select ifnull(max(id),0) into v_max_value from tmp_Subscribers_02;
    if v_max_value < v_fetch_cnt * v_count then
      SET v_times = 1 + floor(v_max_value/v_fetch_cnt);
      update TEST s,tmp_Subscribers_01 t set s.LastAccessTimeStamp=1420066800
      where s.MSISDN=t.MSISDN and t.id > v_max_value and t.id <= v_fetch_cnt * v_times;
      /*record the processing rows*/
      insert into tmp_Subscribers_02 select id, MSISDN, now() from tmp_Subscribers_01 where id = v_fetch_cnt * v_times;
      select concat('The job',' has already updated ', v_fetch_cnt * v_times, ' rows..') as Info;
      select sleep(v_sleep_secs);
    end if;
    commit;
  END WHILE;
  select concat('The job',' is ','finished!') as Info;
  commit;
end$$
delimiter ;

--执行存储过程
--其中第一个传入参数为每次更新的行数,第二个参数为每次更新后的休眠时间
call proc_Subscribers_update(10000, 2);

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26506993/viewspace-2123040/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-08-01

  • 博文量
    404
  • 访问量
    1673977