表记录清理注意事项

常规表清理见 pt-archiver 工具: [pt-archiver]/top-10-percona-toolkit-tools-%E4%B8%80/)

脚本清理记录举例如下:

(1) 按条件取出主键id的信息, 存入数组@list中;

select id from user where count <= ? and update_time < ?

(2) 分组执行清理操作, 每次的清理的数量为指定的chunk大小:

    foreach my $bound ( 0 .. int(@list/$chunk) ) { 
        my $id_list;
        if ( $bound != int(@list/$chunk) ) { 
           $id_list = join(', ', @list[ 0 + $chunk*$bound .. $chunk*(1+$bound) - 1 ]); 
        } else {
           $id_list = join(', ', @list[ $chunk*$bound .. $#list ]); 
        }

      ...
      ...
        $dbh_master->do("delete low_priority from user_login where id in ( $id_list )") or die $dbh_master->errstr;
        sleep 1;
    }