Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize MySQL periodic sub-table by pt-archiver

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章给大家分享的是有关pt-archiver如何实现MySQL定期分表的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

pt-archiver使用的场景

1、清理线上过期数据

2、清理过期数据,并把数据归档到本地归档表中,或者远端归档服务器

3、两张表之间的数据不完全相同,希望合并。此时加上-ignore或-replace选项,可以轻松实现

4、导出线上数据,到线下数据作处理

导出数据的基本原理:

通过"过滤条件",得在此范围内的PK的最大值和最小值 , 然后通过PK索引, 过滤条件 + pk (此范围内pk的最小值+每次增长limit指定的数) + limit. 这样直到处理完成。

关于分表,定期建表有一个pt-archiver可以很好的实现. db_source.py_out_export和db_source.py_out_export_result做定期清理,每周三0:00归档到历史库db_hist.py_out_export_his${TABLE_DATE}和db_hist.py_out_export_result_his${TABLE_DATE}表. 具体实现可参考 如下/pydata/software/percona-toolkit-3.0.2/pt-archiver.sh /pydata/software/percona-toolkit-3.0.2/pt-archiver.def [root@py-test percona-toolkit-3.0.2]# more pt-archiver.sh #-------------------------------------------------------------------------------- #脚本名称: pt-archiver.sh #脚本参数: pt-archiver.def文件 #脚本功能: 归档大表到历史表 #编 写 人: zj #编写日期: 2017年6月19日14:20 #更 新 人: #更新日期: #-------------------------------------------------------------------------------- EXEC_DATE=`date +%Y-%m-%d` TABLE_DATE=`date +%Y%m%d` #EXEC_DATE="2017-05-13" #TABLE_DATE="20170513" FILE_DIR=`dirname $0` FILE_NAME=`basename $0 .sh` USER="root" PASSWORD="dbpasswd" sed '/^#.*\|^$/d' ${FILE_DIR}/${FILE_NAME}.def >${FILE_DIR}/${FILE_NAME}.tmp for i in `cat ${FILE_DIR}/${FILE_NAME}.tmp` do DEF_DATA_TMP="`echo ${i} |sed s/\ //g`" #去空格,得到一行数据 #得到数据文件和源表名值 SOURCE_DB=`echo ${i} | cut -d "," -f1 | tr "[A-Z]" "[a-z]"` TABLE_NAME=`echo ${i} | cut -d "," -f2 | tr "[A-Z]" "[a-z]"` FLITER_FIELD=`echo ${i} | cut -d "," -f3 | tr "[A-Z]" "[a-z]"` DEST_DB=`echo ${i} | cut -d "," -f4 | tr "[A-Z]" "[a-z]"` HISTABLE_NAME="${TABLE_NAME}_his${TABLE_DATE}" WHERE_SQL=`echo "'exec_time

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report