MySQL performance optimization shorthand

Summary from "MySQL 5.7 from entry to proficient (video teaching version)" edited by Liu Zengjie.

Introduction to Optimization

MySQL database optimization is multi-faceted. The principle is to reduce the bottleneck of the system, reduce the occupation of resources, and increase the response speed of the system.
In MySQL, you can SHOW STATUSquery some MySQL performance parameters through the statement. For example, to query the number of connections, you can execute the following statement:
SHOW STATUS LIKE 'Connections';
To query the number of slow queries, you can execute the following statement:
SHOW STATUS LIKE 'Slow_queries';
Some commonly used performance parameters are as follows:
- Connections: the number of connections to the MySQL server;
- Uptime: the online time of the MySQL server;
- Slow_queries: The number of slow queries;
- Com_select: the number of query operations ( selectcan be changed insertto , update, delete, to query the number of inserts, updates, and deletes respectively).

MySQL database optimization can be considered from three aspects, namely, optimizing query, optimizing database structure, and optimizing MySQL database.

Optimize queries

  1. Analyze the query statement.
  2. Leverage indexes to speed up queries.
  3. Optimize subqueries.

Analyze the query statement

By analyzing the query statement, you can understand the execution of the query statement. MySQL provides EXPLAINand DESCRIBEto analyze query statements.
The example is as follows: the
EXPLAIN SELECT * FROM `user` WHERE username = 'admin';
result is as shown in the figure:

several parameters are explained:
- id: SELECT identifier. This is the query sequence number for the SELECT.
- select_type: Indicates the type of the SELECT statement.
- table: Indicates the table for the query.
- type: Indicates the connection type of the table. ALLIndicates that a full table scan was performed. This is the worst result.
- possible_keys: Indicates which index MySQL can use to find rows in this table. If NULL, there is no associated index.
- key: Indicates the index actually used by the query.
- key_len: Indicates the length in bytes of the index field selected by MySQL.
- ref: Indicates which column or constant to use with the index to query the record.
- rows: Displays the number of rows that MySQL has to check when making a query in the table.
- Extra: Indicates the details of MySQL when processing the query.

Leverage indexes to speed up queries

One of the most effective ways to improve performance in MySQL is to design reasonable indexes on data tables. Indexes provide an efficient way to query data and speed up queries. For example, in the analysis query statement, the displayed type is ALL, which means that a full table scan is performed and no index is used. usernameAdd a common index to the pair username_index, and then analyze the query statement. The result is as shown in the figure:

As you can see, the type type becomes ref, which means that all matching rows are read from the table, which is used when the index is neither UNIQUE nor PRIMARY KEY, or the left subset of the index columns is used in the query, that is, the left part of the index part of the column combination.

There are several special cases of using an index. In these cases, it is possible that the index does not work when a field with an index is used to query. The following describes these special cases.

  1. A query statement using the LIKE keyword .
    In a query using the LIKE keyword, if the first character of the matched string is "%", the index will not work. Indexing will only work if "%" is not in the first position.
  2. Query statements that use multi-column indexes .
    MySQL can create indexes for multiple fields. An index can include 16 fields. For multi-column indexes, the index will only work if the first of these fields is used in the query condition.
  3. A query statement using the OR keyword .
    The index is used in the query only when the query condition of the query statement has only the OR keyword, and the columns in the two conditions before and after the OR are both indexes. Otherwise, the query will not use the index.

Optimize subqueries

Subqueries can perform many SQL operations that logically require multiple steps to complete in one go. Although subqueries can make query statements very flexible, their execution efficiency is not high. When executing a subquery, MySQL needs to create a temporary table for the query results of the inner query statement. Then the outer query statement queries records from the temporary table. After the query is complete, these temporary tables are revoked. Therefore, the speed of subqueries will be affected to some extent.
In MySQL, you can use join (JOIN) queries instead of subqueries. Join queries do not require the creation of temporary tables and are faster than subqueries.

Optimize database structure

A reasonable database structure can not only make the database occupy less disk space, but also make the query faster. The design of the database structure needs to consider data redundancy, the speed of query and update, and whether the data type of the field is reasonable.

Decompose a table with many fields into multiple tables

For a table with many fields, if some fields are used very infrequently, these fields can be separated to form a new table. Because when a table has a large amount of data, it will be slow due to the existence of fields that are used infrequently.

Add intermediate table

For tables that need to be queried frequently, an intermediate table can be established to improve query efficiency. By establishing an intermediate table, inserting the data that needs to be queried frequently into the intermediate table, and then changing the original joint query to the query on the intermediate table, so as to improve the query efficiency.

Add redundant fields

When designing database tables, you should try to follow the stipulations of the paradigm theory, reduce redundant fields as much as possible, and make the database design look refined and elegant. However, adding redundant fields reasonably can improve query speed.

Optimize the speed of inserting records

When inserting records, the main factors that affect the insertion speed are indexes, uniqueness verification, and the number of records inserted at one time. This can be optimized individually.

Analysis table, check table and optimization table

MySQL provides statements for analyzing tables, checking tables, and optimizing tables. The analysis table is mainly to analyze the distribution of keywords; the check table is mainly to check whether there are errors in the table; the optimization table is mainly to eliminate the space waste caused by deletion or update.

  1. Analysis table: ANALYZE TABLE table name;
  2. Check table: CHECK TABLE table name;
  3. Optimize table: OPTIMIZE TABLE table name; (only VARCHAR, BLOB or TEXT type fields in the table can be optimized)

For the above three methods, the database system will automatically add a read-only lock to the table. During the analysis of the table, only the records in the table can be read, not updated and inserted. In most scenarios, you don't need to use an optimized table, even if there are a lot of updates to variable-length rows, you don't need to run it often, once a week or once a month, and only need to run on a specific table.

Optimizing MySQL Server

Optimize server hardware

The hardware performance of the server directly determines the performance of the MySQL database. The performance bottleneck of the hardware also determines the running speed and efficiency of the database. The commonly used methods for optimizing server hardware are as follows:
1. Configure larger memory.
2. Configure a high-speed disk system to reduce the waiting time for disk reading and improve the response speed.
3. Distribute disk I/O reasonably and distribute disk I/O on multiple devices to reduce resource competition and improve parallel operation capabilities.
4. Configure multi-processor, MySQL is a multi-threaded database, multi-processor can execute multiple threads at the same time.

Optimizing MySQL Parameters

By optimizing the parameters of MySQL, the resource utilization can be improved, so as to achieve the purpose of improving the performance of the MySQL server.
The configuration parameters of the MySQL service are all in the [MySQLd] group of the my.cnf or my.ini file. in:

  • key_buffer_size: Indicates the size of the index buffer. The index buffer is shared by all threads. Reasonable buffer sizes can handle indexes better.

  • table_cache: Indicates the number of open tables at the same time.

  • query_cache_size: Indicates the size of the query buffer.

  • innodb_buffer_pool_size: Represents the maximum cache for InnoDB-type tables and indexes. The larger the value, the faster the query will be.

  • max_connections: Indicates the maximum number of connections. The value is not as large as possible. Too many connections may cause the MySQL server to freeze.
    ...others are omitted.

Replenish

1. Is the more indexes the better?
A reasonable index can improve the query speed, but the more indexes the better. Before executing the insert statement, MySQL needs to create an index for the newly inserted record, so too many indexes will cause the insert operation to slow down.
2. How to use the query buffer?
The query buffer can improve the query speed, but it is only suitable for the case where there are many query statements and few update statements. For details, refer to optimizing MySQL parameters.

Guess you like

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