欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL查询超时的设置方法(关于timeout的处理)

程序员文章站 2024-02-22 11:26:22
为了优化oceanbase的query timeout设置方式,特调研mysql关于timeout的处理,记录如下。 复制代码 代码如下: mysql> show v...
为了优化oceanbase的query timeout设置方式,特调研mysql关于timeout的处理,记录如下。
复制代码 代码如下:

mysql> show variables like '%time%';
+----------------------------+-------------------+
| variable_name | value |
+----------------------------+-------------------+
| connect_timeout | 10 |
| datetime_format | %y-%m-%d %h:%i:%s |
| delayed_insert_timeout | 300 |
| flush_time | 1800 |
| innodb_lock_wait_timeout | 50 |
| innodb_old_blocks_time | 0 |
| innodb_rollback_on_timeout | off |
| interactive_timeout | 28800 |
| lc_time_names | en_us |
| lock_wait_timeout | 31536000 |
| long_query_time | 10.000000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| slow_launch_time | 2 |
| system_time_zone | |
| time_format | %h:%i:%s |
| time_zone | system |
| timed_mutexes | off |
| timestamp | 1366027807 |
| wait_timeout | 28800 |
+----------------------------+-------------------+
21 rows in set, 1 warning (0.00 sec)

重点解释其中几个参数:
connect_timeout:
the number of seconds that the mysqld server waits for a connect packet before respondingwith bad handshake. the default value is 10 seconds as of mysql 5.1.23 and 5 seconds before that. increasing the connect_timeout value might help if clients frequently encounter errors of the form lost connection to mysql server at ‘xxx', system error: errno.
解释:在获取链接时,等待握手的超时时间,只在登录时有效,登录成功这个参数就不管事了。主要是为了防止网络不佳时应用重连导致连接数涨太快,一般默认即可。
interactive_timeout:
the number of seconds the server waits for activity on an interactive connection before closing it. an interactive client is defined as a client that uses the client_interactive option to mysql_real_connect(). see alsowait_timeout.
解释:一个持续sleep状态的线程多久被关闭。线程每次被使用都会被唤醒为acrivity状态,执行完query后成为interactive状态,重新开始计时。wait_timeout不同在于只作用于tcp/ip和socket链接的线程,意义是一样的。
mysql可以配置连接的超时时间,这个时间如果做得太长,甚至到了10min,那么很可能发生这种情况,3000个链接都被占满而且sleep在哪,新链接进不来,导致无法正常服务。因此这个配置尽量配置一个符合逻辑的值,60s或者120s等等。
说人话:
命令行下面敲一个命令后,直至下一个命令到来之前的时间间隔为interactive_time,如果这个时间间隔超过了interactive_timeout,则连接会被自动断开,下一个命令失败。不过一般的mysql客户端都有自动重连机制,下一个命令会在重连后执行。
复制代码 代码如下:

mysql> set interactive_timeout = 1;
query ok, 0 rows affected (0.00 sec)
mysql> show session variables like '%timeout%';
+----------------------------+----------+
| variable_name | value |
+----------------------------+----------+
| connect_timeout | 10 |
| interactive_timeout | 1 |
| wait_timeout | 28800 |
+----------------------------+----------+
10 rows in set (0.00 sec)

复制代码 代码如下:

mysql> set wait_timeout = 1;
query ok, 0 rows affected (0.00 sec)
【去泡杯茶,等会儿】
mysql> show session variables like '%timeout%';
error 2006 (hy000): mysql server has gone away
no connection. trying to reconnect...
connection id: 7
current database: *** none ***
+----------------------------+----------+
| variable_name | value |
+----------------------------+----------+
| connect_timeout | 10 |
| interactive_timeout | 28800 |
| wait_timeout | 28800 |
+----------------------------+----------+
10 rows in set (0.01 sec)

wait_timeout:
the number of seconds the server waits for activity on a noninteractive connection (连接上没有活动命令,可能是客户端喝咖啡去了。)before closing it. before mysql 5.1.41, this timeout applies only to tcp/ip connections, not to connections made through unix socket files, named pipes, or shared memory.
on thread startup, the session wait_timeout value is initialized from the global wait_timeout value or from the global interactive_timeout value, depending on the type of client
这里顺带解释一下什么是non-interactive connection
> non-interactive commands
just do a quick look up on a table without logging into the client, running the query then logging back out again.
you can instead just type one line using the ' -e ' flag.
复制代码 代码如下:

c:\mysql\bin\mysql -u admin -p mydatabase -e 'select * from employee'

net_read_timeout / net_write_timeout
the number of seconds to wait for more data from a connection before aborting the read. before mysql 5.1.41, this timeout applies only to tcp/ip connections, not to connections made through unix socket files, named pipes, or shared memory. when the server is reading from the client, net_read_timeout is the timeout value controlling when to abort. when the server is writing to the client, net_write_timeout is the timeout value controlling when to abort. see also slave_net_timeout.
on linux, the no_alarm build flag affects timeout behavior as indicated in the description of the net_retry_count system variable.
解释:这个参数只对tcp/ip链接有效,分别是数据库等待接收客户端发送网络包和发送网络包给客户端的超时时间,这是在activity状态下的线程才有效的参数
jdbc setquerytimeout函数:
为了避免查询出现死循环,或时间过长等现象,而导致线程阻塞,在获得statement的实例后,stmt.setquerytimeout(10); 避免因为查询导致程序出现线程阻塞。
但昨天发现程序出现了,“ora-01013: 用户请求取消当前的操作”的异常。手工执行出错sql语句发现,这个语句耗时20多秒。因为setquerytimeout(10),所以还没有执行完查询语句就抛出异常了。使用setquerytimeout(10)时一定要把时间设置的长一些,如60秒以上。只要不导致线程长期阻塞,就可以。太短了容易抛出,“ora-01013: 用户请求取消当前的操作”的异常
jdbc实现setquerytimeout的原理:
复制代码 代码如下:

class ifxcancelqueryimpl extends timertask
implements ifmxcancelquery
{
ifxstatement stmt;
timer t = null;
public void startcancel(ifxstatement paramifxstatement, int paramint)
throws exception
{
this.stmt = paramifxstatement;
this.t = new timer(true);
this.t.schedule(this, paramint * 1000);
}
public void run()
{
try
{
this.stmt.cancel();
this.t.cancel();
}
catch (sqlexception localsqlexception)
{
this.t.cancel();
throw new error(localsqlexception.geterrorcode() + ":" + localsqlexception.getmessage());
}
}
}

可见,query timeout是通过客户端解决方案来做的,服务器端无需知晓。通过一个timer线程来监控执行时间,如果执行时间超时,则会schedule run()函数。
reference:
http://wangwei.cao.blog.163.com/blog/static/10236252620111119115540534/
http://sls8204.blog.163.com/blog/static/62979632200741683453114/
oceanbase可以通过server端支持query timeout,可以设置query timeout并且不中断当前session。这一点比mysql先进。