偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

MySQL 存儲(chǔ)過程中的只讀語句超時(shí)怎么辦?

數(shù)據(jù)庫 MySQL
MySQL 有一個(gè)參數(shù)叫 max_execution_time ,用來設(shè)置只讀語句執(zhí)行的超時(shí)時(shí)間,但是僅對(duì)單獨(dú)執(zhí)行的 select 語句有效;對(duì)于非單獨(dú)執(zhí)行的 select 語句,比如包含在存儲(chǔ)過程、觸發(fā)器等內(nèi)置事務(wù)塊里則不生效。

?MySQL 有一個(gè)參數(shù)叫 max_execution_time ,用來設(shè)置只讀語句執(zhí)行的超時(shí)時(shí)間,但是僅對(duì)單獨(dú)執(zhí)行的 select 語句有效;對(duì)于非單獨(dú)執(zhí)行的 select 語句,比如包含在存儲(chǔ)過程、觸發(fā)器等內(nèi)置事務(wù)塊里則不生效。官方手冊(cè)上對(duì)這個(gè)參數(shù)解釋如下:

max_execution_time applies as follows:

The global max_execution_time value provides the default for the session value for new connections. The session value applies to SELECT executions executed within the session that include no MAX_EXECUTION_TIME(*N*) optimizer hint or for which N is 0.

max_execution_time applies to read-only SELECT statements. Statements that are not read only are those that invoke a stored function that modifies data as a side effect.

max_execution_time is ignored for SELECT statements in stored programs.

那對(duì)這種非單獨(dú)出現(xiàn)的 select 語句,該如何控制超時(shí)時(shí)間呢?

先來看下參數(shù) max_execution_time 設(shè)置后的效果。此參數(shù)設(shè)置后,select 語句如果執(zhí)行時(shí)間過長,會(huì)直接被 cancel 掉,并且報(bào)錯(cuò),如下所示:

mysql> set @@max_execution_time=1000;
Query OK, 0 rows affected (0.00 sec)

mysql> select sleep(2) from t1 limit 1;
ERROR 3024 (HY000): Query execution was interrupted, maximum statement execution time exceeded

或者是采用直接加 Hint 的方式,也能限制 select 語句的執(zhí)行時(shí)間: 下面兩種方式都能起到限制 select 語句執(zhí)行時(shí)間的作用。

mysql> select /*+ max_execution_time(1000) */ sleep(2) from t1 limit 2;
ERROR 3024 (HY000): Query execution was interrupted, maximum statement execution time exceeded

mysql> select /*+ set_var(max_execution_time=1000) */ sleep(2) from t1 limit 2;
ERROR 3024 (HY000): Query execution was interrupted, maximum statement execution time exceeded

那如果把這條 select 語句封裝在存儲(chǔ)過程內(nèi)部,按照手冊(cè)上對(duì)參數(shù) max_execution_time 的解釋,則不生效。比如新建一個(gè)存儲(chǔ)過程 sp_test :

DELIMITER $$

USE `ytt`$$

DROP PROCEDURE IF EXISTS `sp_test`$$

CREATE DEFINER=`admin`@`%` PROCEDURE `sp_test`()
BEGIN
select sleep(2) from t1 limit 1;
END$$

DELIMITER ;

重新設(shè)置 max_execution_time 值為1秒:調(diào)用存儲(chǔ)過程 sp_test , 可以正常執(zhí)行,select 語句并沒有被 cancel 掉!

mysql> call sp_test;
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 rows in set (2.01 sec)

Query OK, 0 rows affected (2.01 sec)

那如何解決這個(gè)問題呢?

為了更方便大家測試,把語句 select sleep(2) from t1 limit 1 改為 select sleep(2000) from t1 limit 1 。既然 MySQL 層面有這樣的限制,那只能從非 MySQL 層面來想辦法。最直接有效的就是寫個(gè)腳本來主動(dòng) cancel 掉 select 語句。腳本如下:

root@ytt-normal:/home/ytt/script# cat kill_query 
#!/bin/sh
QUERY_ID=`mysql -ss -e "select id from information_schema.processlist where user='admin' and db='ytt' and time>10 and regexp_like(info,'^select','i')"`
if [ $QUERY_ID ];then
echo "kill query $QUERY_ID"
mysql -e "kill query $QUERY_ID"
fi

完后把腳本放到 crontab 或者 MySQL 自己的 event 里來定時(shí)執(zhí)行即可。單獨(dú)執(zhí)行腳本效果如下:

root@ytt-normal:/home/ytt/script# ./kill_query 
kill query 50

除了自己編寫腳本,還有一個(gè)工具可以實(shí)現(xiàn)類似的效果,它包含在我們熟知的 Percona-toolkit 工具箱里,叫 pt-kill 。

pt-kill 工具可以根據(jù)各種觸發(fā)條件來執(zhí)行指定動(dòng)作:比如 cancel 掉指定 SQL 語句、kill 掉指定 session 等。所以完全可以使用 pt-kill 工具來實(shí)現(xiàn) select 語句超時(shí)被自動(dòng) cancel 掉。如下所示:pt-kill 工具會(huì)在后臺(tái)一直運(yùn)行,監(jiān)聽 MySQL 進(jìn)程,一旦觸發(fā)條件被激活,即可執(zhí)行相應(yīng)動(dòng)作。

root@ytt-normal:/home/ytt/script# pt-kill --match-db=ytt --match-user=admin --match-host=%  \--match-info='^select' --victims=all --busy-time='10s' --print --kill-query

# 2022-08-15T17:29:03 KILL QUERY 50 (Query 11 sec) select sleep(2000) from t1 limit 1

有一點(diǎn)需要注意:select 語句超時(shí)自動(dòng) cancel 掉這樣的功能不適宜用在生產(chǎn)環(huán)境!因?yàn)槟銦o法預(yù)知其執(zhí)行結(jié)果的時(shí)效性、上下文是否相關(guān)等特點(diǎn)。?

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2011-04-11 17:28:50

oracle存儲(chǔ)select語句

2012-12-26 09:55:09

Windows 8

2009-07-23 14:10:38

Hibernate J

2022-09-07 09:00:00

計(jì)算數(shù)據(jù)庫

2016-09-07 20:28:17

MySQL存儲(chǔ)數(shù)據(jù)庫

2010-05-31 16:57:09

2010-05-27 17:45:13

MySQL存儲(chǔ)過程

2022-10-14 08:18:07

Guavaweb應(yīng)用

2010-05-27 17:56:39

MySQL存儲(chǔ)過程

2017-08-30 17:21:05

LinuxShell超時(shí)現(xiàn)象

2010-11-26 16:18:13

MySQL變量定義

2010-04-15 16:54:31

Oracle存儲(chǔ)過程

2010-11-12 09:18:13

SQL Server存

2024-04-22 08:17:23

MySQL誤刪數(shù)據(jù)

2022-07-05 11:48:47

MySQL死鎖表鎖

2011-08-15 15:56:31

SQL Server

2010-04-16 09:03:28

Oracle 存儲(chǔ)過程

2017-12-04 08:37:31

存儲(chǔ)空間NAS

2010-10-09 16:41:54

MYSQL存儲(chǔ)過程

2010-05-05 14:55:15

Oracle存儲(chǔ)過程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)