資料庫大量Waiting for table flush 狀態SQL問題排查
阿新 • • 發佈:2021-10-14
環境:rds for mysql5.7
因特殊原因,業務側賬號有reload許可權
今天收到活躍會話數過多的告警,登入伺服器檢視有大量SQL語句狀態處於【Waiting for table flush】
下圖是復現之後的場景,只有一個大查詢再跑,其它的查詢語句都是【Waiting for table flush】狀態,
mysql> show processlist; +----+-------------+-----------------+-------+---------+-------+--------------------------------------------------------+------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------------+-------+---------+-------+--------------------------------------------------------+------------------------------------------------+ | 1| system user | | NULL | Connect | 21234 | Connecting to master | NULL | | 2 | system user | | NULL | Connect | 21234 | Slave has read all relay log; waiting for more updates | NULL | | 8| root | 127.0.0.1:39070 | ceshi | Sleep | 10 | | NULL | | 9 | root | 127.0.0.1:39120 | ceshi | Query | 65 | User sleep | select * from tt whereid=(select sleep(3000)) | | 11 | root | 127.0.0.1:39124 | ceshi | Query | 45 | Waiting for table flush | select * from tt | | 12 | root | 127.0.0.1:39126 | NULL | Query | 0 | starting | show processlist | | 15 | root | 127.0.0.1:39132 | NULL | Sleep | 24 | | NULL | +----+-------------+-----------------+-------+---------+-------+--------------------------------------------------------+------------------------------------------------+
第一反應想到了高鵬的一篇文章,
https://blog.csdn.net/n88Lpo/article/details/113749804
在有大查詢時,對查詢的表執行一次analyze table ,後面所有關於這張表的查詢都會被阻塞,狀態並處於【Waiting for table flush】,同事將大查詢KILL掉以後,例項恢復正常。
當時懷疑有人執行了analyze table 或程式碼框架執行過 analyze table,或者其它操作,檢視RDS慢日誌有一些線索,看到有過幾次flush table with read lock;
這個操作非常重,聯絡開發檢視程式碼,為了獲取binlog位點會鎖一次,但似乎這個命令會併發執行,研發將flush table with read lock命令遮蔽掉後,例項一直執行正常。
正常情況下flush table with lock 後,SQL狀態應該是等待全域性鎖,但不知道為什麼會是等待flush table。
嘗試後復現該問題
從整個過程看,第二個flush table with lock 我停掉後,後續的SQL並沒有正常執執行,還是【Waiting for table flush】
個人猜測原因和高鵬的那篇相似。