MySQL study notes. MySQL performance optimization

Optimization overview

The purpose of MySQL database optimization: On the one hand, it is to
find the bottleneck of the system and improve the performance of the MySQL database. On the other hand, it is reasonable structure design and parameter adjustment to improve the response speed of user operations, while saving the system as much as possible. Resources.
MySQL database optimization is multifaceted. The principle is to reduce system bottlenecks, reduce resource usage, and increase system response speed. The file system can be optimized to increase the read and write speed of the disk, and the response speed can be enhanced by optimizing the scheduling strategy of the operating system.

Usage statement: SHOW STATUS LIKE 参数;You can query the performance parameters of the MySQL database.
Commonly used parameters are: connections to connect to the MySQL server, uptime MySQL server online time, slow_queries slow query times, com_select query operations, com_insert insert operations, com_update update operations, com_delete delete The number of operations.
Example: query the number of connections and connection time

mysql> show status like 'connections';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections   | 2     |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show status like 'uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 390   |
+---------------+-------+
1 row in set (0.00 sec)

Optimize query

Query is the most frequent operation in the database. How to improve the query speed and improve the performance of mysql?

1. Analyze the query statement
Through the analysis of the query statement , you can understand the execution of the query statement, which can also find the bottleneck of the query statement execution and optimize it. mysql provides EXPLAIN and DESCRIBE statements to analyze query statements.
EXPLAIN syntax: EXPLAIN [EXTENDED] SELECT select_options
select_options is a query option, including FROM WHERE clause To
prepare data, first create a database named wb, create a table named student and insert data.

Example: analyze the following query statement

mysql> explain select * from student;
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | student | ALL  | NULL          | NULL | NULL    | NULL |   20 |       |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

select_type: indicates the type of select statement, which can have a variety of values: SIMPLE indicates a simple query, which does not include join queries and subqueries; PRIMARY indicates the main query, or the outermost query statement; UNION indicates the second of the join query Or the following query statement; DEPENDENT UNION, the second or subsequent SELECT statement in the join query, depends on the outside query; UNION RESULT, the result of the join query; SUBQUERY, the first SELECT statement in the subquery; DEPENDENT SUBQUERY , The first SELECT in the subquery depends on the outside query; DERIVED, the SELECT of the derived table (the subquery of the FROM clause).

possible_keys: Point out which index MySQL can use to find rows in this table. If the column is NULL, there is no related index. In this case, you can improve query performance by checking the WHERE clause to see if it references certain columns or columns suitable for indexing. If so, you can create suitable indexes to improve query performance.

key: indicates the index used by the query

key_len: Indicates the length in bytes of the index sub-segment selected by mysql

ref: Indicates which column or constant is used together with the index to query the record

type: indicates the connection type of the table

The DESCRIBE statement is similar to EXPLAIN

mysql> explain select * from student where sno='2018001015';
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | student | ALL  | NULL          | NULL | NULL    | NULL |   20 | Using where |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> desc select * from student where sno='2018001015';
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | student | ALL  | NULL          | NULL | NULL    | NULL |   20 | Using where |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

2. The impact of indexes on query speed
An effective way to improve performance in MySQL is to design reasonable indexes on data tables. Indexes provide an efficient way to access data and can speed up queries. Therefore, indexes have a vital impact on the speed of queries. The index can quickly locate a record in the table, thereby increasing the speed of database query and improving the performance of the database.
Example: Comparison of not using index and using index.
Do not use index

mysql> explain select * from student where sno='2018001015';
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | student | ALL  | NULL          | NULL | NULL    | NULL |   20 | Using where |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

Use index

mysql> explain select * from student where sno='2018001015';
+----+-------------+---------+------+---------------+-----------+---------+-------+------+-------------+
| id | select_type | table   | type | possible_keys | key       | key_len | ref   | rows | Extra       |
+----+-------------+---------+------+---------------+-----------+---------+-------+------+-------------+
|  1 | SIMPLE      | student | ref  | index_sno     | index_sno | 33      | const |    1 | Using where |
+----+-------------+---------+------+---------------+-----------+---------+-------+------+-------------+
1 row in set (0.00 sec)

It can be seen that rows=1 if there is an index, and 20 if there is no index. The index greatly improves the efficiency.

3. Use index query
Index can improve query speed, but it is not always effective when using indexed field query. For example, if the following query
uses the LIKE keyword query statement,
if the first character of the matching string is "%", Index will not work, that is to say, "%" will only work if it is not in the first character.

mysql> CREATE INDEX index_name ON student(sname);
Query OK, 20 rows affected (0.11 sec)
Records: 20  Duplicates: 0  Warnings: 0

mysql> explain select * from student where sname like '%l';
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | student | ALL  | NULL          | NULL | NULL    | NULL |   20 | Using where |
+----+-------------+---------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from student where sname like 'l%';
+----+-------------+---------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table   | type  | possible_keys | key        | key_len | ref  | rows | Extra       |
+----+-------------+---------+-------+---------------+------------+---------+------+------+-------------+
|  1 | SIMPLE      | student | range | index_name    | index_name | 63      | NULL |    2 | Using where |
+----+-------------+---------+-------+---------------+------------+---------+------+------+-------------+
1 row in set (0.00 sec)

Query statements using multi-column indexes
For multi-column indexes, the index will only be used when the first field is used in the query condition. An index can include 16 sub-segments.
Use OR keyword query statement
Only the OR keyword in the query statement condition, and the columns in the two conditions before and after the OR are all indexes, the index is used in the query.
Both sno and sname columns are indexes

mysql> explain select * from student where sno='2018001015' OR sname='l%';
+----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+------------------------------------------------+
| id | select_type | table   | type        | possible_keys        | key                  | key_len | ref  | rows | Extra                                          |
+----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+------------------------------------------------+
|  1 | SIMPLE      | student | index_merge | index_sno,index_name | index_sno,index_name | 33,63   | NULL |    2 | Using union(index_sno,index_name); Using where |
+----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+------------------------------------------------+
1 row in set (0.00 sec)

4. Optimize subqueries.
Use subqueries to perform nested queries of SELECT statements, that is, the result of a SELECT query can be used as the condition of another SELECT query, so subqueries can be completed by completing multiple logical steps at one time. Operation, and therefore the sub-query is more flexible, it seems that this query method is good, but because of this, its execution efficiency is not high. In mysql, you can use join queries instead of subqueries. The speed of join queries is much faster than subqueries, so try not to use subqueries if you can use join queries.

Optimize MySQL server

The optimization server is mainly optimized from two aspects, one is to optimize the hardware, and the other is to optimize the mysql service parameters.

1. Hardware optimization The
hardware is the key to determining the performance of the database, which directly determines the operating speed and efficiency of the database, so improving the hardware configuration for the performance bottleneck can effectively improve the performance of the database.
Method 1: Configure enough memory. The speed of memory is much faster than that of disk. Large memory can increase the system buffer capacity and data will stay in memory for a longer time, thereby reducing disk I/O.
Method 2: Configure a high-speed disk system to reduce waiting time for disk reading and improve response speed.
Method 3: Distribute disk I/O reasonably and distribute it on multiple devices to reduce resource competition.
Method 4: Configure multiple processors. Multiprocessors can execute multiple threads.

2. Optimize mysql parameters
By optimizing mysql parameters, resource utilization can be effectively improved.
The mysql service configuration parameters are all in the my.ini file
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/109683541