Practical MySQL optimization ideas

Recently, a lot of SQL has been optimized in the development process. I will record my optimization ideas here. I hope to help those who are destined. If there are big guys, please advise!

1. Introduction to EXPLAIN

View the execution plan:

   

Meaning of the result field :

id select_type table partitions type possible_keys key key_len ref rows filtered Extra
Select identifier Type of query Table of output result set Matching partition Table connection type Available index Actually used index Index field length Comparison of columns and indexes Number of rows scanned Percentage of rows filtered by table criteria Description and explanation of implementation

(1) id column : the larger the value, the earlier it will be executed;

(2) select_type column : There are two types of simple query and complex query, among which simple query: simple; complex query: primary: represents the outermost select in the complex query; subquery: the subquery in the general subquery is marked as subquery, that is, the query in the select list; derived: the subquery contained in the from clause; union: the second and subsequent subquery in the union are marked as union, and the first one is marked as primary If the union is in from, it is marked as derived;

(3) Table column : Indicates which table the line of explain is accessing. When there is a subquery in the from clause, the table column is the format, indicating that the current query depends on the query with id=N, so the query with id=N is executed first. When there is a union, the value of the table column of the UNION RESULT is <union 1,2>, and 1 and 2 indicate the id of the select row participating in the union;

(4) Type column : From the best to the worst, they are: system> const> eq_ref> ref> fulltext> ref_or_null> index_merge> unique_subquery> index_subquery> range> index> ALL;

(5) Key column : the index used in the actual query;

(6) Extra column : This column is a very important parameter, showing some detailed information of MySQL in the query process; common values ​​are as follows:

  1. Using filesort: It means that MySQL will use an external index to sort the data instead of reading it according to the index order in the table. Sorting operations that cannot be done using indexes in MySQL are called "file sorting;"

  2. Using temporary: Use temporary tables to save intermediate results, which are often used in GROUP BY and ORDER BY operations. Generally, it indicates that the query needs to be optimized. Even if the use of temporary tables cannot be avoided, try to avoid the use of hard disk temporary tables;

  3. Not exists: MYSQL optimizes LEFT JOIN. Once it finds a row that matches the LEFT JOIN standard, it will no longer search;

  4. Using index: It means that the query covers the index, and there is no need to read the data file, and the information can be obtained from the index tree (index file). If using where appears at the same time, it indicates that the index is used to search for index key values. If there is no using where, it indicates that the index is used to read data instead of performing a search. This is done by the MySQL service layer, but there is no need to go back to the table to query records;

  5. Using index condition: new features added in MySQL 5.6. To put it simply, MySQL originally could not perform operations such as like on the index, but it is now possible, which reduces unnecessary IO operations, but it can only be used on secondary indexes;

  6. Using where: Indicates that where filtering is used. Note: The Using where in the Extra column indicates that the MySQL server returns the storage engine to the service layer before applying WHERE condition filtering;

  7. Using join buffer: indicates that the connection cache is used;

  8. Impossible where: The value of the where clause is always false, and no rows can be selected;

  9. Select tables optimized away: In the absence of the GROUP BY clause, optimize the MIN/MAX operation based on the index, or optimize the COUNT(*) operation for the MyISAM storage engine, without having to wait until the execution stage to calculate, the query execution plan is generated The optimization is completed at the stage;

  10. Distinct: optimize distinct operation, it will stop looking for more rows after finding the first matching row; 

2. Thinking analysis

(1) View the execution plan through explain, analyze the parameters, make the query take the index, and avoid the use of temporary tables; if there is no index, increase it.

(2) Optimize the query order, sometimes the query order will also affect the query performance;

(3) Avoid using temporary tables to store intermediate results.

Three, the situation that makes the index invalid

1. The left% of like;

2、in、not in、not exist;

3. B-tree index is null will not go, is not null will go, bitmap index is null, is not null will go;

4. != <>, if it is continuous, you can use between;

5. Perform function calculation on the index column, and the type conversion will not take the index;

6. Or, you can use union all instead;

 

 Record the pitfalls encountered in a MySQL optimization process_viagra-CSDN blog

Oracle's temporary table and MySQL's temporary table_viagra-CSDN blog

 The difference between MySQL and Oracle database, high concurrency database optimization_viagra-CSDN blog

 

 

Guess you like

Origin blog.csdn.net/weixin_42228950/article/details/110290477