mysql更新减100数值_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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值