• ADADADADAD

    MySQL NDB如何进行批量更新100万行数据[ mysql数据库 ]

    mysql数据库 时间:2024-11-26 22:17:49

    作者:文/会员上传

    简介:

    生产NDB数据库中的一张800多万行的大表需要更新部分字段,为减少对数据库性能的影响,编写了一个存储过程来实现:1. 导出这张表的主键字段,数据量有100多万条select MSISDN from T

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    生产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万行数据.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: NDBmysql