当前位置:网站首页>MySQL query timeout control
MySQL query timeout control
2022-06-13 07:09:00 【guangsu.】
mysql Query timeout control
mysql The communication between client and server can adopt A long connection And Short connection Two ways .
- Short connection The number of suitable clients is uncertain Small amount of data interaction . Release immediately after the interaction tcp link . Do not occupy the link
- A long connection Suitable for a fixed number of clients Scenarios with frequent data interaction save tcp Frequent creation of disconnection overhead
Generally, our servers are connected with mysql The second method is adopted . Server side And mysql The server Maintain a connection pool . hold n Multiple long connections .
If a long connection is used without any operation on the database for a long time , So in timeout After value ,mysql server Will close the connection
There is a scene dba Worry about slow queries sent by clients . Therefore, it is necessary to establish a database overload protection mechanism . That is, for the submitted query Apply to set a reasonable execution time in advance .
Control the performance overhead of slow queries , So as to eliminate the hidden dangers that affect the service quality during the peak period .
show global variables like 'wait_timeout';
wait_timeout
connect_timeout
interactive_timeout
long_query_time
max_execution_time
net_read_timeout
net_write_timeout
wait_timeout
Choose some important explanations
- wait_timeout connect_timeout
stay MYSQL In the default settings of , If a database connection exceeds 8 Hours are not used ( idle 8 Hours ), The server will disconnect this connection , Subsequent query operations on this connection will fail .
- net_read_timeout The default is 30S
- net_write_timeout The default is 60S
The number of seconds to wait for more data from a connection before aborting the read.
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 replica_net_timeout and slave_net_timeout.
These two parameters control the abnormal timeout caused by the network . such as server In from client The client reads a large amount of data , Reading and reading, I suddenly find that I can't read , No end identifier was encountered ,
In this case ,server Waiting for the net_read_timeout The following data has not been read in seconds , Then the server will terminate this read operation . Note that this operation is terminated Instead of disconnecting a long connection ;
Or when server select A large amount of data is sent to the client , Hair, hair, hair , I suddenly found that I couldn't move , The client does not receive , The data has not been sent yet ,
At this time server Waiting for the net_write_timeout Seconds later, the write operation is terminated ..
!!! But our goal is : Prevent slow query from killing the server . Instead of solving the problem of network sending and receiving .
- max_execution_time
Query statement execution timeout control
There are two ways to set the client
_, err := data.MySQLConn.Exec("SET max_execution_time=1")SELECT /*+ MAX_EXECUTION_TIME(10) */ * FROM prop where sleep(10)
To verify this result, we need to use sleep function
- When sleep returns normally (without interruption), it returns 0:
- This statement is interrupted using KILL QUERY from another session: return 1
- This statement is interrupted by timing out: return 1
- When SLEEP() is only part of a query that is interrupted, the query returns an error:
eg.1
SET max_execution_time=1
`select *,sleep(1) from prop where id=2` // Can be in select The last observation of the result sleep Return value .
Note that this usage mode does not time out err Suggestive . because sleep(1) stay 1ms The interrupt result is returned in 1. This is equivalent to using sleep(1) The return value of . Error details are masked .
eg.2
exec:SET max_execution_time=1
exec:SELECT * from prop where SLEEP(1);
And this way of use , You can return the error to the client Query execution was interrupted, maximum statement execution time exceeded
also select Statement actual sleep Time and where The number of conditionally filtered items is related . actual sql execution time = sleep(10) * len(result) + Actual query time
Reference documents
- sleep Function introduction official documentation https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
- https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html
- https://dev.mysql.com/doc/refman/8.0/en/error-lost-connection.html
- qa https://stackoverflow.com/questions/71261028/how-to-set-mysql-max-execution-time-property-use-go
边栏推荐
- RT-Thread 模拟器 simulator LVGL控件:slider 控件
- 【转】FPGA面试题
- RT thread simulator lvgl control: button button style
- Can flush open a stock account? Is it safe?
- Tidb data migration (DM) Introduction
- [Tencent Alibaba's most comprehensive collection of test questions] (four sides: three rounds of technology +1 round of HR)
- TXT_ File encryption and compression
- Normalizing y-axis in histograms in R ggplot to proportion
- 【马尔科夫链-蒙特卡罗】马尔科夫链-蒙特卡罗方法对先验分布进行抽样
- 简单了解C语言基本语
猜你喜欢

RT-Thread 模拟器 simulator LVGL控件:slider 控件

Department store center supply chain management system

【RS-422与RS-485】RS-422与RS-485串行接口标准

How to write an amazing design document?

RT-Thread 模拟器 simulator LVGL控件:button 按钮样式

FSM状态机

Br backup test

百货中心供应链管理系统

Eureka server multi node deployment

Table access among Oracle database users
随机推荐
Network planning common interview knowledge (I)
Evolution in the digital age
What is the new business model of Taishan crowdfunding in 2022?
RT thread simulator lvgl control: button button event
ISIS的vsys(虚拟系统)
An example of CSRF attack defense in web application scenarios
不间断管理设计
对绘制丘岭密度图ridge plot的详细说明、重叠核密度估计曲线overlapping densities、FacetGrid对象、函数sns.kdeplot、函数FacetGrid.map
玄武云科技通过上市聆讯:业绩波动明显,陈永辉等三人为控股股东
个人js学习笔记
杭州证券开户是安全的吗?
Continuous management design
DM Experiment 6: filter replication
10 Honest Facts I Want To Share With All Junior Developers
Why is the blind box e-commerce mode so popular?
NFV基本概述
RT thread simulator lvgl control: switch switch button control
Nfv basic overview
Upper computer development (code debugging of firmware download software)
FSM状态机