≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > MySQL

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

人气:922 时间:2020-07-09

这篇文章主要为大家详细介绍了SQL查询超时的设置方法(关于timeout的处理),具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!

为了优化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先进。

本文来自:http://www.q1010.com/177/9979-0.html

注:关于SQL查询超时的设置方法(关于timeout的处理)的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:MYSQL

您可能感兴趣的文章

  • MySQL 超大数据/表管理技巧
  • MySQL解决远程不能访问的二种方法
  • 服务器不支持 MySQL 数据库的解决方法
  • MySQL select in 按id排序实现方法
  • DBA应该知道的一些关于SQL Server跟踪标记的使用
  • jdbc调用MySQL存储过程实现代码
  • MySQL自增列插入0值的解决方案
  • MySQL 替换某字段内部分内容的UPDATE语句
  • MySQL的安全问题从安装开始说起
  • 如何用cmd连接MySQL数据库
上一篇:使用MySQL Slow Log来解决MySQL CPU占用高的问题
下一篇:如何用cmd连接MySQL数据库
热门文章
  • mysql 修改character_set_server为utf-8的简单示例
  • 解决MySQL丢失文件localhost.localdomain.pid、mysql.sock的示例
  • MySQL 数据类型binary和varbinary的简单示例
  • MySQL:reading initial communication packet问题解决方法
  • MySql 表类型MYISAM、InnoDB区别
  • bash: mysql: command not found 的解决方法
  • MYSQL默认用户名ROOT修改方法
  • MySQL 常用命令菜鸟教程
  • MySQL 使用命令行新建用户并授予权限
  • MySql 数据库物理文件存放位置查看示例
  • 最新文章
    • MySQL查看死锁与解除死锁的简单示例
    • MySQL 慢查询的功能实例
    • MySQL查看死锁与去除死锁的简单示例
    • MySQL找出未提交事务的SQL的简单示例
    • MySQL锁阻塞的的简单示例
    • MySQL中的binary类型使用操作的示例
    • SQL优化教程之in与range查询的简单示例
    • MySQL 的 21 个规范、优化最佳实践!
    • MySQL 字符类型大小写敏感的简单示例
    • 解决mybatis-plus分页传入参数后sql where条件没有limit分页信息的问题

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。