The correct posture for MySQL SQL optimization

Hello everyone, I am the teacher of the SQL optimization class of Zhishutang. The screen name: Rabbit riding a turtle

It’s been a long time since I wrote an article. Today I share an optimized SQL case.

The following SQL in slow query

Look at the execution plan as follows 

It can be seen from the execution plan that the C table is scanned in   full

Then we look at the specific SQL  

It can be seen from the specific SQL that a good filter condition in the where condition should be 

Table B, first look at whether there is an index on table B related conditions 

As shown in the figure above, we know that there is an index. In the case of the case, table B should be used as a driving table, but in reality, it is not  . Why is table C used as a driving table?

We all know that MySQL is an optimizer based on COST. If B is the driving table, the connection order should be B->A->C. Obviously, in this process, where the COST is too high, this execution plan was not chosen.

Let's focus on the connection condition of A->C 

Let's look at the index of the C table corresponding to this connection condition 

From this we can see that the connection condition corresponding to the C table is not the leading column in the index, that is, the first column

So here is the problem .

So far, let's test it first and create a single-column index separately 

Then look at the execution plan 

Well, we achieved our expected results. Look at the specific implementation effect

My new round of SQL optimization class will start after the Spring Festival 

I am the teacher of Zhishutang's SQL optimization class~ ^^

If you have any questions about SQL optimization and communication, please add and @兔子@知数堂SQL optimization

The high-performance MySQL and SQL optimization groups include Ye Jinrong and Wu Bingxi: 579036588

Welcome to join the Zhishutang family.

My WeChat public account : SQL development and optimization ( sqlturning )

Guess you like

Origin blog.csdn.net/n88Lpo/article/details/112550464