温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL NDB如何进行批量更新100万行数据

发布时间:2021-10-25 15:58:03 来源:亿速云 阅读:1087 作者:柒染 栏目:MySQL数据库

本篇文章为大家展示了MySQL NDB如何进行批量更新100万行数据,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

生产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);

上述内容就是MySQL NDB如何进行批量更新100万行数据,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI