? ? ? ? 表结构如下:
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),其它删除。
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));"
-- 在从库执行查询
select userid, avid into outfile '/data/del.txt' from space_visit_av where updatetime < '2023-10-01';
cd /data
split -l 100000 -d -a 6 del.txt
# 删除原文件
rm del.txt
# 后台执行
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;"
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;"
# 后台执行
nohup ~/shrink.sh > ~/shrink.log 2>&1 &
? ? ? ? shrink.sh 脚本文件内容如下:
#!/bin/bash
source ~/.bashrc
# 连接主库执行
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=10s \
--max-lag=60s \
--nocheck-replication-filters \
--critical-load="Threads_running=512" \
--max-load="Threads_running=256" \
D="space",t="space_visit_av" \
--progress=time,30 \
--execute