How Mysql analyzes and optimizes queries

1. Create an index for some fields of a table through create index idx_colunmsName on tableName(columns). Note that both the primary key and the unique key will automatically create an index;

For example, create a joint index for the name and class fields of the table student: create index idx_name_class on student(name,class);

2. Analyze the efficiency of the query statement and some configuration properties through explain or describe:

For example: describe select* from student where uid=1; will output the following columns:

1. The id column, the position of the select in the entire query;

2. The select_type column, the query type, is generally simple;

3. table column, the table corresponding to this query;

4.type column, connection type, many values ​​are stored in this column, ranging from const to all;

5. The possible_keys column, important , refers to the indexes that can be used in this query statement; (note that if the two judgments in the where condition are and, then if the first

6. The key column, important , indicates which column is the actual location record in this query (the primary key is slightly special and directly displays the Primary instead of the column field name)

7. The rows column, important , is used to view the number of rows that need to be checked to execute this query , the smaller the better ;

8. key_len column, important, the length of the index used, generally the smaller the better;

The following 2 items in 9.extra mean that MYSQL cannot use indexes at all, and the efficiency will be greatly affected. This should be optimized as much as possible.

extra items illustrate
Using filesort Indicates that MySQL will use an external index to sort the results, instead of reading the relevant content from the table in index order. May be sorted in memory or on disk. A sort operation in MySQL that cannot be done with an index is called a "file sort"
Using temporary Indicates that MySQL uses temporary tables when sorting query results. Commonly used in sorting order by and grouping query group by.

Guess you like

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