A: Scenario
- peak business period, production of MySQL too much pressure , can not respond to normal , the need in the short term, temporarily lifting some performance .
- In business time high, Mysql service pressure, resulting in damage to the business, the user's development chief says, no matter what program you use, let it start and repeat business.
- Today we'll talk about these temporary programs , talk and focus on the risk they may be present. (Lossy scheme, non-destructive program certainly will not this time to execute)
II: short connection performance problems caused by
- Why short connections can cause performance problems of the database?
- normal short connection mode is connected to the database after the execution of SQL statements can rarely disconnected , next time when needed and then reconnect .
- In the "execution of a SQL How? " I referred, MySQL to establish a connection (requires TCP handshake, logon rights / data read and write permissions judgment) ( expensive ).
- in the database pressure is relatively small, these additional costs are not obvious.
- However, short connection model there is a risk that once the database processing more slowly, the number of connections will be skyrocketing . (In the present case under pressure)
- Mysql the max_connections parameters , used to control a maximum number of connections simultaneously present example MySQL .
- this is exceeded, the system will refuse the connection request next, and error prompt "Too many connections".
- for the connection request is rejected, the perspective is from the service database is unavailable.
- Therefore, when a machine load is relatively high, the processing time becomes long existing request, each time the connection is maintained longer.
- At this time, then have something new connection, it may exceed the limit of max_connections.
- Why can not simply solve the problem of excessive short connection by way of increase max_connections?
- because the design max_connections this parameter was trying to protect MySQL.
- If we change it too much, so that more connections can come in, then the system is likely to further increase the load , a large amount of resources spent on the authorization verification logic.
- The result may be counterproductive, the thread has been unable to get connected CPU resources to execute operations SQL requests.
- The first method: first dispose of those threads occupied connection but does not work.
- Select which links disconnected?
- Show processlist to link sleep in.
- You can give priority to disconnect external affairs idle for too long connection (transaction table information_schema.INNODB_TRX);
- If this is not enough, then consider idle for too long within a transaction is disconnected.
- how to disconnect the short connection?
- disconnect the connector from the server using the kill connection + id command.
- when a client is in sleep state, after which the connection is active off the server, the client will not immediately know.
- until the next time the client initiates a request, you will receive this error "ERROR 2013 (HY000): Lost connection to MySQL server during query".
- potential problem?
- Disconnect from the database end of the initiative may be lossy , especially after some end applications receive this error, do not reconnect, but this can not directly use the handle to retry the query.
- This will lead from the application side looks, "MySQL has not been restored."
- The second method: reducing the consumption of the connection process. (Cancel permissions and authentication)
- Some business code will apply to a large number of database connections in a short time to do backup, if the database is now identified as being connected to behavior and hanging, then one possible approach is to skip the permissions database validation stage.
- Skip rights verification methods are: to restart the database, and use -skip-grant-tables parameter to start. Thus, the entire MySQL will skip all the permissions validation phase, including the connection process and procedure statements, including execution.
- However, this method is particularly in line with our title speaks of "harm than good" high risk, in particular, we do not recommend the use of the program . Especially if you are outside the network can access the library, they can not even do that.
Three: the slow query performance problems caused by
- the index is not designed well
- a general scenario is through to solve urgent to create the index .
- MySQL 5.6 or later, create indexes to support Online DDL up.
- for the kind of hit the peak of this database has been linked to the case statement, the most efficient approach is to directly execute alter table statements.
- Compare desirable to be able to perform in the standby database.
- Suppose you service is a main one, the main library A, prepared by the library B, the flow of this program is substantially this:
- executed on the standby database B set sql_log_bin = off, i.e. not written binlog, and then execute the statement alter table index plus;
- Preparation of execution of the main switch;
- this time the primary database be B, is prepared by the library A. Execution set sql_log_bin = off on A, and then execute the statement alter table index plus.
- SQL statement is not written.
- slow queries repair.
- MySQL wrong index.
- At this time, the emergency plan is to give this statement with force index.
Four: the rest of the method
- whitelist mechanism
- business accounts separate.
Five: Summary
- In the actual development, we have to try to avoid some of the inefficient methods, such as avoiding the use of a large number of short connections.
- At the same time, if you do business development, then, you know, connection exception disconnect is often the case, your code must be correctly and retry mechanism of reconnection.
- DBA can override though temporarily deal with the problem by the statement , but that in itself is high a risk of the operation , good opportunity SQL (explain) can reduce the need for this type of operation.