目錄
- 1. 背景
- 2. 操作步驟
- 2.1. 確認數據歸檔條件,此次操作開發按照非主鍵列 server_time 按時間進行刪除並保存,需要轉化為主鍵列條件。
- 2.2. 由於歷史表文件較大,按月歸檔、刪除,便於操作及后期查詢數據,在目標庫中,每個月份創建一張 duplicate 表。
- 2.3. 參照 pt-archiver 工具參數及使用示例,預先編輯好歸檔命令(參照“5. 場景示例” 中的 “A.”)。
- 2.4. 執行歸檔命令按月歸檔歷史數據。
- 2.5. 歸檔完畢后,使用 where 條件去源數據,對比原表與歸檔表數據行是否一致。
- 2.6. 確認歸檔數據一致后,對歸檔表備份導出。
- 2.7. 參照 pt-archiver 工具參數及使用示例,預先編輯好刪除歷史數據命令(參照“5. 場景示例” 中的 “B.”)。
- 2.8. 或者使用腳本進行刪除操作。
- 2.9. 備份完成后,執行刪除歷史數據操作。
1. 背景
12月5日開始,某線上實例每天報警從庫延遲。
經排查發現,延遲時段的 tps 並不高,只是對 matomo_log_link_visit_action 表有 1k多的 DML,但該表文件大小已達 60G,遠超單表最大 10G 的規范要求。
該實例為點擊流數據庫,該表記錄的主要記錄用戶的鏈接訪問行為。
經過與開發負責人溝通,需要對該表進行歸檔歷史數據操作,並保留1個月內的數據。
2. 操作步驟
2.1. 確認數據歸檔條件,此次操作開發按照非主鍵列 server_time 按時間進行刪除並保存,需要轉化為主鍵列條件。
show create table matomo_log_link_visit_action\G
select max(idlink_va) from matomo_log_link_visit_action where server_time='2018-04-30 23:59:59';
select max(idlink_va) from matomo_log_link_visit_action where server_time='2018-05-31 23:59:59';
select max(idlink_va) from matomo_log_link_visit_action where server_time='2018-06-30 23:59:59';
select max(idlink_va) from matomo_log_link_visit_action where server_time='2018-07-31 23:59:59';
select max(idlink_va) from matomo_log_link_visit_action where server_time='2018-08-31 23:59:59';
select max(idlink_va) from matomo_log_link_visit_action where server_time='2018-09-30 23:59:59';
select max(idlink_va) from matomo_log_link_visit_action where server_time='2018-10-31 23:59:59';
2.2. 由於歷史表文件較大,按月歸檔、刪除,便於操作及后期查詢數據,在目標庫中,每個月份創建一張 duplicate 表。
create table visit_action_4 like matomo_log_link_visit_action;
create table visit_action_5 like matomo_log_link_visit_action;
create table visit_action_6 like matomo_log_link_visit_action;
create table visit_action_7 like matomo_log_link_visit_action;
create table visit_action_8 like matomo_log_link_visit_action;
create table visit_action_9 like matomo_log_link_visit_action;
create table visit_action_10 like matomo_log_link_visit_action;
2.3. 參照 pt-archiver 工具參數及使用示例,預先編輯好歸檔命令(參照“5. 場景示例” 中的 “A.”)。
# 4月歸檔
pt-archiver --source h=127.0.0.1,P=3306,u=superadmin,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset 'UTF8' --dest h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=visit_action_4 --no-version-check --where "idlink_va <= 4383363" --statistics --no-delete --bulk-insert --progress 5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_4.log &
# 5月歸檔
pt-archiver --source h=127.0.0.1,P=3306,u=superadmin,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset 'UTF8' --dest h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=visit_action_5 --no-version-check --where "idlink_va <= 26473975 and idlink_va > 4383363" --statistics --no-delete --bulk-insert --progress 5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_5.log &
# 6月歸檔
pt-archiver --source h=127.0.0.1,P=3306,u=superadmin,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset 'UTF8' --dest h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=visit_action_6 --no-version-check --where "idlink_va <= 51504119 and idlink_va > 26473975" --statistics --no-delete --bulk-insert --progress 5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_6.log &
# 7月歸檔
pt-archiver --source h=127.0.0.1,P=3306,u=superadmin,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset 'UTF8' --dest h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=visit_action_7 --no-version-check --where "idlink_va <= 75811899 and idlink_va > 51504119" --statistics --no-delete --bulk-insert --progress 5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_7.log &
# 8月歸檔
pt-archiver --source h=127.0.0.1,P=3306,u=superadmin,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset 'UTF8' --dest h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=visit_action_8 --no-version-check --where "idlink_va <= 121711398 and idlink_va > 75811899" --statistics --no-delete --bulk-insert --progress 5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_8.log &
# 9月歸檔
pt-archiver --source h=127.0.0.1,P=3306,u=superadmin,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset 'UTF8' --dest h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=visit_action_9 --no-version-check --where "idlink_va <= 150953368 and idlink_va > 121711398" --statistics --no-delete --bulk-insert --progress 5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_9.log &
# 10月歸檔
pt-archiver --source h=127.0.0.1,P=3306,u=superadmin,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset 'UTF8' --dest h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=visit_action_10 --no-version-check --where "idlink_va <= 206555065 and idlink_va > 150953368" --statistics --no-delete --bulk-insert --progress 5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_10.log &
2.4. 執行歸檔命令按月歸檔歷史數據。
2.5. 歸檔完畢后,使用 where 條件去源數據,對比原表與歸檔表數據行是否一致。
--4月
select count(*) from matomo_log_link_visit_action where idlink_va <= 4383363;
select count(*) from visit_action_4 where idlink_va <= 4383363;
--5月
select count(*) from matomo_log_link_visit_action where idlink_va <= 4383363;
select count(*) from visit_action_4 where idlink_va <= 4383363;
--6月
select count(*) from matomo_log_link_visit_action where idlink_va <= 4383363;
select count(*) from visit_action_4 where idlink_va <= 4383363;
--7月
select count(*) from matomo_log_link_visit_action where idlink_va <= 4383363;
select count(*) from visit_action_4 where idlink_va <= 4383363;
--8月
select count(*) from matomo_log_link_visit_action where idlink_va <= 4383363;
select count(*) from visit_action_4 where idlink_va <= 4383363;
--9月
select count(*) from matomo_log_link_visit_action where idlink_va <= 4383363;
select count(*) from visit_action_4 where idlink_va <= 4383363;
--10月
select count(*) from matomo_log_link_visit_action where idlink_va <= 4383363;
select count(*) from visit_action_4 where idlink_va <= 4383363;
2.6. 確認歸檔數據一致后,對歸檔表備份導出。
mysqldump -S xxx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF --no-create-info xxx visit_action_4 > /data/backup/xxx_history/xxx--visit_action_4.sql.bak
mysqldump -S xxx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF --no-create-info xxx visit_action_5 > /data/backup/xxx_history/xxx--visit_action_5.sql.bak
mysqldump -S xxx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF --no-create-info xxx visit_action_6 > /data/backup/xxx_history/xxx--visit_action_6.sql.bak
mysqldump -S xxx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF --no-create-info xxx visit_action_7 > /data/backup/xxx_history/xxx--visit_action_7.sql.bak
mysqldump -S xxx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF --no-create-info xxx visit_action_8 > /data/backup/xxx_history/xxx--visit_action_8.sql.bak
mysqldump -S xxx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF --no-create-info xxx visit_action_9 > /data/backup/xxx_history/xxx--visit_action_9.sql.bak
mysqldump -S xxx/mysql.sock --single-transaction --master-data=2 --set-gtid-purged=OFF --no-create-info xxx visit_action_10 > /data/backup/xxx_history/xxx--visit_action_10.sql.bak
2.7. 參照 pt-archiver 工具參數及使用示例,預先編輯好刪除歷史數據命令(參照“5. 場景示例” 中的 “B.”)。
pt-archiver --source h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset UTF8 --no-version-check --where="idlink_va <= 206555065" --progress=5000 --limit=500 --purge --bulk-delete --commit-each --sleep=1 --statistics >> xxx--matomo_log_link_visit_action--archive_10.log &
2.8. 或者使用腳本進行刪除操作。
#!/bin/bash
USER=xxx
PWD="xxx"
SOCK=xxx/mysql.sock
DB=xxx
TB=matomo_log_link_visit_action
MAX_ID=206555065
NUM=1000
PK=idlink_va
MY_CLI="/usr/local/bin/mysql -u${USER} -p${PWD} -S${SOCK} ${DB}"
for ((i=i;i<206556;i++));do
${MY_CLI} -e "delete from ${TB} where ${PK} <= ${MAX_ID} limit ${NUM};"
sleep 0.2;
echo $i;
done
2.9. 備份完成后,執行刪除歷史數據操作。
3. 工具介紹
-
因有歸檔數據的要求,故選用 pt-archiver 工具進行操作。
-
pt-archiver 是 percona toolkit 工具集中的工具,主要用於數據刪除、數據歸檔、數據遷移等場景。
4. 工具參數
4.1. 連接數據庫的參數
varable | Meaning |
---|---|
--source=d | DSN specifying the table to archive from (required) |
--dest=d | DSN specifying the table to archive to |
-h , --host=s | Connect to host |
-P , --port=i | Port number to use for connection |
-S , --socket=s | Socket file to use for connection |
-u , --user=s | User for login if not current user |
-p , --password=s | Password to use when connecting |
-D , --database=s | Database that contains the table |
t | Table to archive from/to |
-A , --charset=s | Default character set |
F | Only read default options from the given file |
L | Explicitly enable LOAD DATA LOCAL INFILE |
a | Database to USE when executing queries |
b | If true, disable binlog with SQL_LOG_BIN |
i | Index to use |
m | Plugin module name |
4.2. 常用參數
variable | meaning | example |
---|---|---|
--[no]version-check | Check for the latest version of Percona Toolkit, MySQL, and other programs (default yes) WHERE clause to limit which rows to archive (required) | --no-version-check目前為止,發現部分pt工具對阿里雲RDS操作必須加這個參數 |
--where=s | WHERE clause to limit which rows to archive (required) | 設置操作條件 |
--statistics | Collect and print timing statistics | 輸出執行過程及最后的操作統計 |
--no-delete | Do not delete archived rows | 不刪除已經歸檔的 rows |
--bulk-insert | Insert each chunk with LOAD DATA INFILE (implies --bulk-delete --commit-each) | 批量插入數據到dest主機(看 dest 的 general log 發現它是通過在dest主機上 LOAD DATA LOCAL INFILE 插入數據的) |
--progress=i | Print progress information every X rows | --progress=5000 每處理 5000 rows 輸出一次處理信息 |
--limit=i | Number of rows to fetch and archive per statement (default 1) | --limit=500每次取 500 rows 數據給 pt-archiver 處理 |
--txn-size=i | Number of rows per transaction (default 1) | --txn-size=100 設置 100 rows 為一個事務提交一次 |
--sleep=i | Sleep time between fetches | --sleep=1每次歸檔了 limit 個行記錄后的休眠1秒(單位為秒) |
--bulk-delete | Delete each chunk with a single statement (implies --commit-each) | 批量刪除 source 上的舊數據(例如每次 1000 rows 的批量刪除操作) |
--replace | Causes INSERTs into --dest to be written as REPLACE | 將insert into 語句改成 replace 寫入到 dest 庫 |
--file=s | File to archive to, with DATE_FORMAT()-like formatting | --file '/root/test.txt' 導出文件的路徑 |
--purge | Purge instead of archiving; allows omitting --file | 刪除 source 數據庫的相關匹配記錄 |
--header | Print column header at top of --file | 輸入列名稱到首行(和--file一起使用) |
--[no]check-columns | Ensure --source and --dest have same columns (default yes) | 檢驗 dest 和 source 的表結構是否一致,不一致自動拒絕執行(不加這個參數也行。默認就是執行檢查的) |
--chekc-interval=m | If --check-slave-lag is given, this defines how long the tool pauses each time it discovers that a slave is lagging (default 1s). Optional suffix s=seconds, m=minutes, h=hours, d=days; if no suffix, s is used. | 默認1s檢查一次 |
--local | Do not write OPTIMIZE or ANALYZE queries to binlog | 不把 optimize 或 analyze 操作寫入到 binlog 里面(防止造成主從延遲巨大) |
--retries | Number of retries per timeout or deadlock (default 1) | 超時或者出現死鎖的話,pt-archiver 進行重試的間隔(默認1s) |
--analyze=s | Run ANALYZE TABLE afterwards on --source and/or --dest | 操作結束后,優化表空間 |
5、場景示例
A. 復制數據到其他(實例、庫、表),且不刪除source的數據(指定字符集):
pt-archiver --source h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=matomo_log_link_visit_action --charset 'UTF8' \
--dest h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=visit_action_4 --no-version-check \
--where="idlink_va <= 4383363" \
--statistics --no-delete --bulk-insert --progress=5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_4.log &
B. 刪除舊數據:
pt-archiver --source h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=matomo_log_link_visit_action \
--charset 'UTF8' --no-version-check --where "idlink_va <= 4383363" \
--progress=5000 --limit=500 --purge --bulk-delete --commit-each --sleep=1 --statistics >> xxx--matomo_log_link_visit_action--archive_4.log &
C. 導出數據到文件
pt-archiver --source h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=matomo_log_link_visit_action \
--where "idlink_va <= 4383363" \
--file ‘xxx/2018-04.txt‘ \
--statistics --no-delete --bulk-insert --progress=5000 --limit=500 --txn-size=100 >> xxx--matomo_log_link_visit_action--visit_action_4.log &
D. 導出數據到文件並刪除數據庫的相關行:
pt-archiver --source h=127.0.0.1,P=3306,u=xxx,p='xxx',D=xxx,t=matomo_log_link_visit_action \
--charset 'UTF8' --no-version-check --where "idlink_va <= 4383363" \
--file ‘xxx/2018-04.txt‘ \
--progress=5000 --limit=500 --purge --bulk-delete --commit-each --sleep=1 --statistics >> xxx--matomo_log_link_visit_action--archive_4.log &