0


安全快速地删除 MySQL 大表数据并释放空间

一、需求

  1. 按业务逻辑删除大量表数据

  2. 操作不卡库,不能影响正常业务操作

  3. 操作不能造成 60 秒以上的复制延迟

  4. 满足以上条件的前提下,尽快删除数据并释放所占空间

     表结构如下:
    
create table `space_visit_av` (
  `userid` bigint(20) not null comment '用户id',
  `avid` bigint(20) not null comment '作品id',
  `touserid` bigint(20) not null comment '被访问用户d',
  `createtime` timestamp not null default current_timestamp comment '创建时间',
  `updatetime` timestamp not null default current_timestamp on update current_timestamp comment '收藏时间',
  primary key (`userid`,`avid`),
  key `index_1` (`touserid`,`updatetime`) using btree,
  key `index_2` (`avid`,`updatetime`) using btree,
  key `idx_updatetime` (`updatetime`)
) engine=innodb default charset=utf8 comment='用户访问作品表';
    表中现有约 50 亿条数据,只保留 2023-10-01 以后的数据(约占总量的 1/10),其它删除。

二、实现

1. 主库按原表创建删除关联表,只保留原表的主键

mysql -uwxy -p123456 -h10.10.10.1 -P18251 -Dspace -e "
create table del (
  userid bigint(20) not null comment '用户id',
  avid bigint(20) not null comment '作品id',
  primary key (userid,avid));"

2. 导出需要删除数据的主键到文件

-- 在从库执行查询
select userid, avid into outfile '/data/del.txt' from space_visit_av where updatetime < '2023-10-01';

3. 将文件分割成 10 万行一个的小文件

cd /data
split -l 100000 -d -a 6 del.txt

# 删除原文件
rm del.txt

4. 遍历文件执行删除

# 后台执行
nohup ~/del.sh > ~/del.log 2>&1 &
    del.sh 脚本文件内容如下:
#!/bin/bash
source ~/.bashrc

dir="/data/"
ls $dir | while read line
do
    file=${dir}${line}

    # 表关联删除数据
    mysql -wxy -p123456 -h10.10.10.1 -P18251 -Dspace --local-infile -e "
        delete from del;
        load data local infile '$file' into table del;
        analyze table del; analyze table space_visit_av;
        delete t1 from space_visit_av t1, del t2 where t1.userid=t2.userid and t1.avid=t2.avid;" -vvv

    echo ${line}
    
    # 取得所有从库的延迟秒数
    s1=`mysql -wxy -p123456 -h10.10.10.2 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`
    s2=`mysql -wxy -p123456 -h10.10.10.3 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`
    s3=`mysql -wxy -p123456 -h10.10.10.4 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`

    # 只有所有从库延迟小于等于 1 秒时继续执行删除,否则等待从库追赶
    while ((s1 > 1)) || ((s2 > 1)) || ((s3 > 1))
    do
        sleep 1;
        s1=`mysql -wxy -p123456 -h10.10.10.2 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`
        s2=`mysql -wxy -p123456 -h10.10.10.3 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`
        s3=`mysql -wxy -p123456 -h10.10.10.4 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`

        echo "$s1 $s2 $s3"
    done

done

# 删除完成后,分析原表,删除关联表
mysql -wxy -p123456 -h10.10.10.1 -P18251 -Dspace -e "
    analyze table space_visit_av;
    drop table del;"
    如果只是简单的按时间字段删除历史数据,可以不用导出文件而是直接利用 limit 子句进行删除,例如:
#!/bin/bash
source ~/.bashrc

matched=""

while [ -z "$matched" ]; do
    r=`mysql -wxy -p123456 -h10.10.10.1 -P18251 -e "
         delete from test.user_task_record 
          where TaskDate < 20231201 
          limit 10000;" -vv 2>/dev/null`

    matched=$(echo "$r" | grep "Query OK, 0 rows affected")
    
    s1=`mysql -uwxy -p123456 -h10.10.10.2 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`
    s2=`mysql -uwxy -p123456 -h10.10.10.3 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`
    s3=`mysql -uwxy -p123456 -h10.10.10.4 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`

    while ((s1 > 1)) || ((s2 > 1)) || ((s3 > 1))
    do
        sleep 1;
        s1=`mysql -uwxy -p123456 -h10.10.10.2 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`
        s2=`mysql -uwxy -p123456 -h10.10.10.3 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`
        s3=`mysql -uwxy -p123456 -h10.10.10.4 -P18251 -e "show slave status\G" 2>/dev/null | egrep 'Seconds_Behind_Master' | awk -F": " '{print $2}'`

        echo "$s1 $s2 $s3"

    done
done
    where 条件中使用的时间字段最好有索引,否则大表执行 delete 会非常慢。联机给大表加索引也可以使用第6步引入的 pt-online-schema-change 工具,在不卡库、复制不延迟的约束下执行操作。

5. 所有从库分析表

mysql -wxy -p123456 -h10.10.10.2 -P18251 -Dspace -e "analyze table space_visit_av;"
mysql -wxy -p123456 -h10.10.10.3 -P18251 -Dspace -e "analyze table space_visit_av;"
mysql -wxy -p123456 -h10.10.10.4 -P18251 -Dspace -e "analyze table space_visit_av;"

6. 使用 pt-online-schema-change 释放删除数据所占空间

# 后台执行
nohup ~/shrink.sh > ~/shrink.log 2>&1 &
    shrink.sh 脚本文件内容如下:
#!/bin/bash
source ~/.bashrc

# 延迟复制实例停止复制,避免 pt-online-schema-change 陷入等待
mysql -uwxy -p123456 -h172.18.10.5 -P18251 -e "stop slave;" -vvv 2>/dev/null

sleep 60

# 连接主库执行
pt-online-schema-change \
--host="10.10.10.1" \
--port=18251 \
--user="wxy" \
--password="123456" \
--charset="utf8mb4" \
--chunk-size=10000 \
--recursion-method="processlist" \
--check-interval=1s \
--max-lag=10s \
--nocheck-replication-filters \
--critical-load="Threads_running=512" \
--max-load="Threads_running=256" \
D="space",t="space_visit_av" \
--progress=time,30 \
--execute

sleep 60

# 从库执行表分析
mysql -uwxy -p123456 -h10.10.10.2 -P18251 -Dspace -e "analyze table space_visit_av;" -vvv 2>/dev/null
mysql -uwxy -p123456 -h10.10.10.3 -P18251 -Dspace -e "analyze table space_visit_av;" -vvv 2>/dev/null
mysql -uwxy -p123456 -h10.10.10.4 -P18251 -Dspace -e "analyze table space_visit_av;" -vvv 2>/dev/null

# 延迟复制实例开启复制
mysql -uwxy -p123456 -h10.10.10.5 -P18251 -e "start slave;" -vvv 2>/dev/null
标签: mysql

本文转载自: https://blog.csdn.net/wzy0623/article/details/134861405
版权归原作者 wzy0623 所有, 如有侵权,请联系我们删除。

“安全快速地删除 MySQL 大表数据并释放空间”的评论:

还没有评论