問題描述
某MySQL服務器出現大量慢日志,慢日志中SQL語句部分為COMMIT,執行時間在100秒至400秒。
由於定期對該服務器進行SHOW PROCESSLIST快照, 通過PROCESSLIST日志查看服務器執行情況。
在1:02至1:32期間,業務進行數據歸檔操作,歸檔SQL支持超過1800秒:
Id User Host db Command Time State Info
8487369 xxx_rw 172.16.16.26:28148 xxx Query 1807 Sending data INSERT INTO xxx_async_task_20200301 SELECT * FROM xxx_async_task_backTable_17533 WHERE created_at < "20200301"
在1:33,歸檔SQL狀態從Sending data切換為query end:
Id User Host db Command Time State Info
8487369 xxx_rw 172.16.16.26:28148 xxx Query 1807 query end INSERT INTO xxx_async_task_20200301 SELECT * FROM xxx_async_task_backTable_17533 WHERE created_at < "20200301"
在1:33至1:36, 該SQL執行狀態仍為:query end,開始出現COMMIT命令等待(starting),活躍連接數從1暴漲至200+:
Id User Host db Command Time State Info
8487369 xxx_rw 172.16.16.26:28148 xxx Query 1807 query end INSERT INTO xxx_async_task_20200301 SELECT * FROM xxx_async_task_backTable_17533 WHERE created_at < "20200301"
8487410 xxx_rw 172.16.16.89:31590 xxx Query 7 starting commit
8487465 xxx_rw 172.16.16.91:58612 xxx Query 10 starting commit
3942982 aurora 11.223.143.134:42889 NULL Query 130 query end /* rds internal mark */ CREATE TABLE IF NOT EXISTS mysql.ha_health_check (\n id BIGINT DEFAULT 0,\n type CHAR(1) DEFAULT '0',\n PRIMARY KEY (type)\n)\n ENGINE = InnoDB
PS: 阿里雲的內部賬號進行ha_health_check的CREATE TABLE 命令也被阻塞
在1:36,歸檔SQL執行完成,處於starting狀態的COMMIT命令也隨之消失,MySQL生成單個文件超過20G的BINLOG,MySQL慢日志中記錄大量包含COMMIT操作的慢日志。
在1:37至1:40,網絡流出量暴漲,大量BINLOG數據被推送至從庫進行數據同步。
線程執行狀態
*Sending data
The thread is reading and processing rows for a SELECT statement, and sending data to the client.
Because operations occurring during this state tend to perform large amounts of disk access (reads), it is often the longest-running state over the lifetime of a given query.
*query end
This state occurs after processing a query but before the freeing items state.
*starting
未在MySQL官網找到解釋
問題原因
導致COMMIT操作出現在慢日志原因:
1、在ROW復制模式下,事務修改大量數據,在COMMIT時需要生成BINLOG事件並刷新至BINLOG文件中,由於BINLOG文件只能順序寫入,導致其他事務COMMIT時被阻塞。
2、當存儲系統出現IO瓶頸或出現故障時,如當RAID卡充放電過程中,RAID系統從Write Back切換為Write Through后,存儲IO使用率暴漲,通用會出現COMMIT操作"長時間執行"情況。