MySQL advanced-MySQL concurrency parameter adjustment

Mysql concurrency parameter adjustment

In terms of implementation, MySQL Server is a multi-threaded structure, including background threads and customer service threads. Multithreading can effectively use server resources and improve the concurrent performance of the database. In Mysql, the main parameters that control concurrent connections and threads include max_connections, back_log, thread_cache_size, table_open_cahce.

1 max_connections

Use max_connections to control the maximum number of connections allowed to the MySQL database. The default value is 151 . If the state variable connection_errors_max_connections is not zero and keeps increasing, it means that there are continuous connection requests failing because the number of database connections has reached the maximum allowable value. This is the value of max_connections can be considered.
Note: When the number of simultaneous requests exceeds 151, there is no available connection to process client requests. These subsequent connections will be in a waiting state. Wait for the MySQL connection to be released. If there is no idle connection, the request will time out

The maximum number of connections that Mysql can support depends on many factors, including the quality of the thread library of a given operating system platform, the size of the memory, the load of each connection, the processing speed of the CPU, and the expected response time. Under the Linux platform, it is not difficult for a server with good performance to support 500-1000 connections. It needs to be evaluated and set according to the server performance .

2 back_log

The back_log parameter controls the size of the backlog request stack set when MySQL listens on the TCP port. If the number of MySql connections reaches max_connections, new requests will be stored in the stack to wait for a connection to release resources. The number of the stack is back_log. If the number of waiting connections exceeds back_log, connection resources will not be granted. An error will be reported . The default value before version 5.6.6 is 50, and the default value for later versions is 50 + (max_connections / 5), but the maximum is not more than 900.
Note: When the concurrent number of requests sent from the client at the same time is greater than 151, the subsequent requests are in a waiting state. Then the number of waiting connections can reach back_log. These new requests will be stored in the stack. To wait for a connection to be released. The number of this stack is set by back_log.

If you need the database to process a large number of connection requests in a short time, you can consider increasing the value of back_log appropriately .

3 table_open_cache

This parameter is used to control the number of table caches that can be opened by all SQL statement execution threads. When executing SQL statements, each SQL execution thread must open at least one table cache. The value of this parameter should be set according to the maximum number of connections max_connections and the maximum number of tables involved in the execution of the associated query for each connection :

​ max_connections x N;
Note: This is not for a certain session, this is for all client execution threads. The number of table caches is the number of operating tables in each SQL statement. For example, a SQL statement generally operates at least one table. If you operate a table, you have one table cache, and if you operate multiple tables, you have multiple table caches.

mysql> show variables like 'table_open_cache%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| table_open_cache           | 431   |
| table_open_cache_instances | 16    |
+----------------------------+-------+
2 rows in set (0.06 sec)

4 thread_cache_size

In order to speed up the connection to the database, MySQL will cache a certain number of customer service threads for reuse. The parameter thread_cache_size can control the number of MySQL cache customer service threads.
Explanation: This is equivalent to opening a thread pool on the MySQL server, and the client has a request, we take out a thread in the thread pool to perform task processing.

mysql> show variables like 'thread_cache_size%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| thread_cache_size | 8     |
+-------------------+-------+
1 row in set (0.00 sec)

A total of 8 thread information is cached.

5 innodb_lock_wait_timeout

This parameter is used to set the time for the InnoDB transaction to wait for row locks. The default value is 50ms, which can be dynamically set as needed. For business systems that require fast feedback, the waiting time of row locks can be adjusted to avoid prolonged suspension of transactions; for batch processing programs running in the background, the waiting time of row locks can be increased to avoid A large rollback operation occurred.

Note: If in a business system that responds quickly, if the row lock is not obtained, just report an error directly, and there is no need to let the transaction respond for a long time.
As you can see, the default timeout is 50ms

mysql> show variables like 'innodb_lock_wait_timeout%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 50    |
+--------------------------+-------+
1 row in set (0.01 sec)

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/113247036