当前位置:网站首页>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 control: button button style
- Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge
- Sorting of numbers and strings
- Tidb dashboard modify port
- 百货中心供应链管理系统
- Fundamentals of assembly language: register and addressing mode
- Upper computer development (software test of firmware download software)
- The innovative public platoon mode team invites users to split, beautiful every second, and links the 2+1 new business model
- [cloud native | kubernetes] kubernetes configuration
- Soup side (8)
猜你喜欢
Through the function seaborn cubehelix_ Palette build order palette
Micro isolation (MSG)
Host computer development (Architecture Design of firmware download software)
2022-06-12:在N*N的正方形棋盘中,有N*N个棋子,那么每个格子正好可以拥有一个棋子。 但是现在有些棋子聚集到一个格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二维数组代表,一
Tidb index optimization
SDN basic overview
基于SSM实现水果商城批发平台
10 Honest Facts I Want To Share With All Junior Developers
What is the new business model of Taishan crowdfunding in 2022?
Try to use renderdoc to view the shader code of UE
随机推荐
Why should two judgment expressions in if be written in two lines
百货中心供应链管理系统
在产业互联网的概念刚刚出现时,人们仅仅只是将其看成是一个获取B端流量的方法
Tidb statistics
The biggest highlight of wwdc2022: metalfx
MySQL系列之分库分表学习笔记
[Collection des questions d'examen les plus complètes de Tencent Ali] (quatre côtés: 3 tours de technologie + 1 tour de RH)
Try to use renderdoc to view the shader code of UE
DM Experiment 6: filter replication
Can flush open a stock account? Is it safe?
Monotone stack top31 of interview must brush algorithm top101
Tidb index optimization
SDN basic overview
Br backup test
Ticdc introduction
Implementation of fruit mall wholesale platform based on SSM
我的理财产品显示清算中是什么意思?
[RS-422 and RS-485] RS-422 and RS-485 serial interface standard
Ml: introduction to stability analysis of machine learning model and detailed introduction to common solutions
New Taishan crowdfunding business diversion fission growth model in 2022