optimize mysql memory

When Mysql occupies too much CPU, what should be optimized?
If the CPU usage is too high, the following considerations can be made:
1) Generally speaking, to exclude high concurrency factors, it is still necessary to find out which SQLs are being executed that cause your CPU to be too high, show processlist statements , and find the SQL statements with the heaviest load , optimize the SQL, such as properly establishing an index for a field;
2) Open the slow query log, and use those SQLs that execute too long and take up too many resources for explain analysis, resulting in high CPU, most of which are sorted by GroupBy and OrderBy caused by the problem, and then slowly optimized and improved. For example, optimize the insert statement, optimize the group by statement, optimize the order by statement, optimize the join statement, etc.;
3) Consider timing optimization of files and indexes ;
4) Regularly analyze tables and use optimize table;
5) Optimize database objects;
6) Consider whether It is a lock problem ;
7) Adjust some MySQL Server parameters, such as key_buffer_size, table_cache, innodb_buffer_pool_size, innodb_log_file_size , etc.;
8) If the amount of data is too large, you can consider using MySQL cluster or build a high-availability environment.
9) The database CPU may be high due to memory latch (leakage).
10) In the case of multi-user high concurrency, any system will not be able to hold it. Therefore, it is necessary to use the cache .Either memcached or redis cache can be used;
11) Check whether the size of tmp_table_size is too small, if allowed, increase it appropriately ;
12) If the configuration of max_heap_table_size is too small, increase it a little ;
13) Set the timeout time for sleep connection of mysql sql statement Question ( wait_timeout )
14) Use show processlist to check the number of mysql connections to see if it exceeds the number of connections set by mysql (http://www.cnblogs.com/kevingrace/p/6226324.html)

Let’s share an example of what I have encountered Case:
The website is accessed during peak hours, and the click page is slightly stuck. Log in to the server and find that the machine load is a bit high, and mysql takes up a lot of CPU resources, as follows:

 

The MySQL load remains high. If the slow query log function is enabled, the best way is to optimize the slow execution of SQL statements in the slow query log. If the SQL statement uses a large number of group by and other statements, the union query will definitely be Increase the occupancy rate of mysql. Therefore, it is necessary to optimize the sql statement .

In addition to optimizing the sql statement , you can also do some configuration optimization. Run show proceslist in mysql; the following echo results appear:
1. The query has a large number of Copying to tmp table on disk status.
Obviously, because the temporary table is too large, mysql writes the temporary table to the hard disk , which affects the overall performance.

The default value of tmp_table_size in Mysql is only 16MB, which is obviously not enough in the current situation.
mysql> show variables like "%tmp%";
+-------------------+----------+
| Variable_name | Value |
+- ------------------+----------+
| max_tmp_tables | 32 |
| slave_load_tmpdir | /tmp |
| tmp_table_size | 16777216 |
| tmpdir | / tmp |
+-------------------+----------+
4 rows in set (0.00 sec)

Solution: adjust the size of the temporary table
1) Enter the mysql terminal command to modify, add global, the next time you enter mysql, it will take effect
mysql> set global tmp_table_size=33554432 ;
Query OK, 0 rows affected (0.00 sec)

Log in to mysql again
mysql> show variables like "%tmp%";
+-------------------+----------+
| Variable_name | Value |
+-------------------+----------+
| max_tmp_tables | 32 |
| slave_load_tmpdir | /tmp |
| tmp_table_size | 33554432 |
| tmpdir | /tmp |
+-------------------+----------+
4 rows in set (0.01 sec)

2) my .cnf configuration file modification
[root@www ~]# vim my.cnf
.....
tmp_table_size = 32M

restart mysql
[root@www ~]# /etc/init.d/mysqld restart

2 .show processlist; The output of the command shows which threads are running and can help identify problematic queries. For example, the following results:
Id User Host db Command Time State Info
207 root 192.168.1.25:51718 mytest Sleep 5 NULL
Let’s briefly talk about the meaning and purpose of each column.

The first column, id , is an identifier, useful when you want to kill a statement.

The user column displays the single former user. If it is not root, this command only displays the SQL statements within your authority.

The host column shows which port on which ip the statement was sent from. Oh, it can be used to track down the user who made the problem statement.

The db column shows which database the process is currently connected to.

The command column displays the commands executed by the current connection, generally sleep, query, and connect. The time column, the duration of this state, in seconds.

The state column shows the state of the sql statement using the current connection. It is a very important column. There will be descriptions of all states in the future. Please note that state is only a certain state in the execution of the statement, a sql statement, which has been queried as an example. It can only be completed after copying to tmp table, Sorting result, Sending data and other states.

The info column shows this sql statement. Because the length is limited, the long sql statement is not displayed completely, but it is an important basis for judging the problem statement.
Common problems:
Generally, there are too many sleep connections, which seriously consumes mysql server resources (mainly cpu, memory), and may cause mysql to crash.

Solution:
In the mysql configuration my.cnf file, there is a wait_timeout parameter setting. You can set the sleep connection timeout in seconds. If a connection times out, it will be naturally terminated by mysql.
Too large wait_timeout has drawbacks, which is reflected in the fact that a large number of SLEEP processes in MySQL cannot be released in time, which drags down system performance, but you cannot set this finger too small, otherwise you may encounter problems such as "MySQL has gone away" .
Generally speaking, setting wait_timeout to 10 hours is a good choice, but there may be problems in some cases. For example, if there is a CRON script where the interval between two SQL queries is greater than 10 seconds, then this setting There is a problem (of course, this is not an unsolvable problem, you can mysql_ping from time to time in the program, so that the server knows that you are still alive and recalculates the wait_timeout time):

The default "wait_timeout" of the MySQL server is 28800 seconds or 8 hours , meaning that if a connection is idle for more than 8 hours, MySQL will automatically disconnect the connection.
However, the connection pool thinks that the connection is still valid (because the validity of the connection is not verified), when the application applies to use the connection, it will cause the following error:
The last packet successfully received from the server was 596,688 milliseconds ago.
mysql> show variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------- -----+-------+
| wait_timeout | 28800 |
+---------------+-------+
1 row in set (0.00 sec)

28800seconds, which is 8 hours.
If the database connection (java.sql.Connection) has been in a waiting state during the wait_timeout seconds, mysql closes the connection. At this point, your Java application's connection pool still legally holds a reference to that connection. When using this connection for database operations, the above error is encountered.
You can increase the default value of the mysql global variable wait_timeout.
Check the mysql manual and find that the maximum value of wait_timeout is 24 days/365 days (windows/linux).

For example, change it to 30 days
mysql> set global wait_timeout=124800;
Query OK, 0 rows affected (0.00 sec)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073929&siteId=291194637