1. 程式人生 > 其它 >MySQL 如何批量kill 慢查詢

MySQL 如何批量kill 慢查詢

Slow query 慢查詢 是指 執行很慢的SQL語句。一般會設定一個閾值,例如,100ms,執行時間超過100ms的都會判定為慢查詢。 慢查詢是一種危險的訊號,MySQL 服務可能很快不可用。當大量出現的時候,應該立即kill。

下文主要介紹如何批量kill 慢查詢。

首先,檢視有哪些慢查詢的SQL:

select *  from information_schema.processlist where command='query' and time > 20;

其中,限定條件中加了commandtime

有時候,也不會限制command
因為,對於慢查詢,原因可能是空閒連線仍然佔有鎖,也需要注意。

例如,下面的語句,空閒連線佔用鎖,導致慢查一直在增加:

mysql >select *  from information_schema.processlist where time > 20;
+------------+---------------------+--------------------+--------------+---------+------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+-----------+---------------+
| ID         | USER                | HOST               | DB           | COMMAND | TIME | STATE    | INFO                                                                                                                                                                                                                                                       | TIME_MS | ROWS_SENT | ROWS_EXAMINED |
+------------+---------------------+--------------------+--------------+---------+------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+-----------+---------------+
| 1073085839 | orange_server | 10.88.6.26:46595  | orchestrator | Query   |   24 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   23850 |         0 |             0 |
| 1073085838 | orange_server | 10.88.6.26:46600  | orchestrator | Query   |   27 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   26850 |         0 |             0 |
| 1073084469 | orange_manager         | 10.1.10.68:32852 | orchestrator | Sleep   | 1806 |          | NULL                                                                                                                                                                                                                                                       | 1805814 |        16 |            16 |
| 1073085824 | orange_server | 10.88.6.26:46594  | orchestrator | Query   |   21 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   20850 |         0 |             0 |
| 1073085828 | orange_server | 10.88.6.26:46615  | orchestrator | Query   |   25 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   24850 |         0 |             0 |
| 1073085829 | orange_server | 10.88.6.26:46605  | orchestrator | Sleep   |   30 |          | NULL                                                                                                                                                                                                                                                       |   29850 |         0 |             0 |
| 1073085769 | orange_server | 10.88.6.26:46547  | orchestrator | Query   |   26 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   25850 |         0 |             0 |
| 1073085833 | orange_server | 10.88.6.26:46614  | orchestrator | Query   |   23 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   22850 |         0 |             0 |
| 1073085782 | orange_server | 10.88.6.26:46545  | orchestrator | Query   |   22 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   21850 |         0 |             0 |
| 1073085825 | orange_server | 10.88.6.26:46601  | orchestrator | Query   |   29 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   28851 |         0 |             0 |
| 1073085785 | orange_server | 10.88.6.26:46553  | orchestrator | Query   |   28 | updating | 
			delete from
				database_instance_maintenance
			where
				maintenance_active is null
				and end_timestamp < NOW() - INTERVAL 7 DAY |   27851 |         0 |             0 |
+------------+---------------------+--------------------+--------------+---------+------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+-----------+---------------+
11 rows in set (0.02 sec)

當出現大量慢查的時候,很可能將MySQL 服務拖垮。當機立斷,應該趕緊kill 慢查詢。

批量kill 慢查的方式如下,首先找到慢查詢id,然後進行kill。

以下SQL,可以組合成kill 語句,直接拷貝執行即可。

mysql >select concat('kill ', id,';') from information_schema.processlist where time > 20;
+-------------------------+
| concat('kill ', id,';') |
+-------------------------+
| kill 1073080655;        |
| kill 1073085823;        |
| kill 1073084469;        |
| kill 1073085837;        |
| kill 1073085778;        |
| kill 1073085869;        |
| kill 1073085772;        |
| kill 1073085787;        |
| kill 1073085786;        |
| kill 1073085780;        |
| kill 1073085779;        |
| kill 1073085858;        |
+-------------------------+
12 rows in set (0.03 sec)

以上,介紹了批量kill 慢查詢的方式。

Just try, don't shy.