目錄mysql
12月5日開始,某線上實例天天報警從庫延遲。sql
經排查發現,延遲時段的 tps 並不高,只是對 matomo_log_link_visit_action 表有 1k多的 DML,但該表文件大小已達 60G,遠超單表最大 10G 的規範要求。數據庫
該實例爲點擊流數據庫,該表記錄的主要記錄用戶的連接訪問行爲。bash
通過與開發負責人溝通,須要對該表進行歸檔歷史數據操做,並保留1個月內的數據。socket
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';
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;
# 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 &
--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;
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
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 &
#!/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
因有歸檔數據的要求,故選用 pt-archiver 工具進行操做。工具
pt-archiver 是 percona toolkit 工具集中的工具,主要用於數據刪除、數據歸檔、數據遷移等場景。fetch
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 |
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 | 操做結束後,優化表空間 |
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 &
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 &
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 &
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 &