当前位置:网站首页>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
边栏推荐
- [cloud native | kubernetes] kubernetes configuration
- 学习Mysql基础第一天
- Continuous management design
- C # Advanced Programming - Feature Section
- [Collection des questions d'examen les plus complètes de Tencent Ali] (quatre côtés: 3 tours de technologie + 1 tour de RH)
- MySQL does not recommend setting the column default value to null. Why on earth is this
- Ticdc synchronization task
- Postgraduate entrance examination English
- Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge
- Is it safe to open an account online in Hangzhou?
猜你喜欢
![[weak transient signal detection] matlab simulation of SVM detection method for weak transient signal under chaotic background](/img/11/d6cd333a2fa56af2dc61b7597f1ada.png)
[weak transient signal detection] matlab simulation of SVM detection method for weak transient signal under chaotic background

不间断管理设计

基于SSM实现水果商城批发平台

Table access among Oracle database users

Ticdc introduction

Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge

Tidb dashboard modify port

牙周炎问题调研(持续进行中)

号称下一代监控系统 来看看它有多牛逼

2022-06-12:在N*N的正方形棋盘中,有N*N个棋子,那么每个格子正好可以拥有一个棋子。 但是现在有些棋子聚集到一个格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二维数组代表,一
随机推荐
It's called the next generation monitoring system. Let's see how awesome it is
Department store center supply chain management system
NFV基本概述
Recently, the popular social e-commerce marketing model, blind box e-commerce, how beautiful every second is accurately drained
Host computer development (Architecture Design of firmware download software)
SDN basic overview
Micro isolation (MSG)
What does my financial product mean in clearing?
Byte (nine)
Through the function seaborn cubehelix_ Palette build order palette
Vsys of Isis (virtual system)
Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge
Upper computer development (software test of firmware download software)
DM Experiment 6: filter replication
【RS-422与RS-485】RS-422与RS-485串行接口标准
An example of CSRF attack defense in web application scenarios
Application of DS18B20 temperature sensor based on FPGA
不间断管理设计
How to seize the bonus of social e-commerce through brand play to achieve growth and profit?
同花顺可以开股票账户吗?安全吗?