-- CONSTANTS: SET @alert_query_duration:=10; SET @alert_queries_max_count:=6; SET @dangerous_query_duration:=15; SET @dangerous_queries_max_count:=30; SET @dangerous_queries_affect_duration_bottom:=5; -- Check long queries: SELECT @alert_queries_count:=COUNT(0) FROM information_schema.processlist WHERE COMMAND != 'Sleep' AND TIME>@alert_query_duration ORDER BY TIME DESC; IF (@alert_queries_count >= @alert_queries_max_count) THEN 1. Report to error_log: module = Query killer error = Maximum amount of long queries reached context = List of all long queries (which longer than @alert_query_duration) 2. Take actions: -- Check if there is a lot of dangerous queries already -- (which may cause each other locks): SELECT @dangerous_queries_count:=COUNT(0) FROM information_schema.processlist WHERE COMMAND != 'Sleep' AND TIME>@dangerous_query_duration ORDER BY TIME DESC; IF (@dangerous_queries_count >= @dangerous_queries_max_count) THEN -- KILL dangerous queries and queries, which probably might be already affected by them -- (so kill all queryes, where TIME>@dangerous_queries_affect_duration_bottom) ... ELSE -- KILL one query (the longes one) which probably locks all others ... END END
97669965 root localhost gtf 289 Query CREATE TABLE gtf.cache_vw_... 99057101 root localhost:33092 gtf 284 Query UPDATE media INNER JOIN image_file USING(media_id) INNER JOIN i 99057467 root localhost:51863 gtf 276 Query UPDATE media INNER JOIN image_file USING(media_id) INNER JOIN i 99057499 root localhost:51868 gtf 275 Query UPDATE media INNER JOIN image_file USING(media_id) INNER JOIN i 99057840 root localhost:33164 gtf 267 Query UPDATE media INNER JOIN image_file USING(media_id) INNER JOIN i 99057907 root localhost:54313 gtf 266 Query UPDATE media INNER JOIN image_file USING(media_id) INNER JOIN i 99057987 root localhost:59942 gtf 264 Query UPDATE media INNER JOIN image_file USING(media_id) INNER JOIN i 99059528 root localhost:52062 gtf 229 Query UPDATE media INNER JOIN image_file USING(media_id) INNER JOIN i
+------+-------------+-------+--------+----------------------------------+----------------------------------+---------+-----------------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+-------+--------+----------------------------------+----------------------------------+---------+-----------------+------+----------------------------------------------+ | 1 | SIMPLE | vw | ref | fk_cache_vw_video_website1 | fk_cache_vw_video_website1 | 4 | const | 8643 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | mtc | eq_ref | PRIMARY,content_id | PRIMARY | 4 | gtf.vw.media_id | 1 | Using where | +------+-------------+-------+--------+----------------------------------+----------------------------------+---------+-----------------+------+----------------------------------------------+
Source: https://habr.com/ru/post/151418/