How to use tools to quickly locate inefficient SQL? | 1 minute series

"Two Tools to Analyze SQL Deadlocks" Two case studies of
"The Big Hole
Caused by SQL Null Values" demonstrate the power of the MySQL performance analysis tool explain.

"The same SQL statement, why is there such a big difference in performance?
Describes the meaning of the most important type field (connection type) in the explain result in detail.

In fact, there is also an Extra field in the explain result, which is very helpful for analyzing and optimizing SQL. Today, I will have a brief chat with you in 1 minute.

data preparation:

create table user (
id int primary key,
name varchar(20),
sex varchar(5),
index(name)
)engine=innodb;
insert into user values(1, 'shenjian','no');
insert into user values(2, 'zhangsan','no');
insert into user values(3, 'lisi', 'yes');
insert into user values(4, 'lisi', 'no');

the data shows:

User table: id primary key index, name ordinary index (non-unique), sex has no index;
four rows of records: the name ordinary index has duplicate records lisi;

Purpose:

By constructing various SQL statements, explain the Extra field of explain, and heuristically locate low-performance SQL statements to be optimized.

一、【Using where】

How to use tools to quickly locate inefficient SQL?  | 1 minute series

Experimental sentence:

explain select * from user where sex='no';

Explanation of results:

Extra为Using where说明,SQL使用了where条件过滤数据。

It should be noted that:
(1) The SQL that returns all records, does not use the where condition to filter the data, the probability is not in line with expectations, for this type of SQL often needs to be optimized;
(2) The SQL that uses the where condition does not mean Need to optimize, often need to cooperate with the type (connection type) in the explain result to comprehensively judge;
voice-over: join type in the same SQL statement, why is there such a big difference in performance? There is a detailed description in the article, and this article will not be expanded.

In this example, although the Extra field description uses where condition filtering, the type attribute is ALL, which means that all data needs to be scanned and there is still room for optimization.

A common optimization method is to add an index on the where filter attribute.
Voiceover: In this example, the sex field is not highly distinguishable, and adding an index has limited performance improvement.

二、【Using index】

How to use tools to quickly locate inefficient SQL?  | 1 minute series

Experimental sentence:

explain select id,name from user where name='shenjian';

Explanation of results:

Extra is the using index description, all column data that SQL needs to return are on an index tree without accessing the actual row records.
Voiceover: The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row.

Such SQL statements tend to perform better.

The question is, what kind of column data will be included in the index tree?

三、【Using index condition】

How to use tools to quickly locate inefficient SQL?  | 1 minute series
Experimental sentence:

explain select id,name,sex from user 
where name='shenjian';

Voiceover: The difference between this SQL statement and the previous SQL statement is that the column being queried has an extra sex field.

Result description:
Extra means using index condition description, it does hit the index, but not all column data are in the index tree, and the actual row records need to be accessed.
Voice-over: Clustered index, common index underlying implementation differences, see "1 minute to understand the index differences between MyISAM and InnoDB".

The performance of this type of SQL statement is also higher, but not as good as the Using index.

The question is, how to optimize it as a Using index?

四、【Using filesort】

How to use tools to quickly locate inefficient SQL?  | 1 minute series

Experimental sentence:

explain select * from user order by sex;

Explanation of results:

Extra is the description of Using filesort, to get the required result set, all records need to be sorted by file.

This type of SQL statement has extremely poor performance and needs to be optimized.

Typically, when order by is performed on a column that is not indexed, filesort will be triggered. A common optimization solution is to add an index on the order by column to avoid all sorts of each query.

五、【Using temporary】

How to use tools to quickly locate inefficient SQL?  | 1 minute series
Experimental sentence:

explain select * from user group by name order by sex;

Result description:
Extra is a Using temporary description, and a temporary table needs to be created to temporarily store intermediate results.

Such SQL statements have low performance and often need to be optimized.

Typically, when group by and order by exist at the same time and act on different fields, a temporary table will be created to calculate the final result set.

六、【Using join buffer (Block Nested Loop)】

How to use tools to quickly locate inefficient SQL?  | 1 minute series

Experimental sentence:

explain select * from user where id in(select id from user where sex='no');

Explanation of results:

Extra means Using join buffer (Block Nested Loop), which requires nested loop calculation.
Voiceover: The type of the inner layer and the outer layer are both ALL, and the rows are both 4. It needs to perform 4*4 calculations in a loop.

The performance of such SQL statements is often low and needs to be optimized.

Typically, two related tables are joined, and the related fields are not indexed. This will happen. A common optimization solution is to add an index on the associated field to avoid each nested loop calculation.

end:

Explain is the most commonly used tool in SQL optimization. After type and Extra are fixed, explain is basically done.

  • "MySQL explain, type analysis" conducted common type analysis
    This article conducted common Extra analysis
  • "Two Tools to Analyze SQL Deadlock" and "The Big Hole Brought by SQL Null Values" are two typical case studies
  • "MyISAM and InnoDB index difference" is the analysis of the difference between InnoDB and MyISAM index
  • "Database index, what exactly do it do?" "Is the bottom layer of the index to analyze the
    above articles, I strongly recommend that you read them thoroughly.
    How to use tools to quickly locate inefficient SQL?  | 1 minute series

Architect's Road-Sharing technical ideas
related recommendations:
"Buffer pool (buffer pool), thoroughly understand this time! "
Write buffer (change buffer), thoroughly understand this time!"

Assignment:
select id, name where XXX is the Using index;
select id, name, sex where XXX is the Using index condition;
how to optimize the latter to the Using index?

I hope everyone has gained a lot, and help me see it again.

Guess you like

Origin blog.51cto.com/jyjstack/2548572