MySQL query optimization - related inquiries

1. associated with the query execution process

MySQL policy enforcement associated with the query is simple, he would cycle out single data from a table, then use of the record to the next table for a matching row, then back to the table, until all of the data match is made. It is also known as " nested loop association ."

Look at the following SQL:

select tb1.col1, tb2,col2
  from tb1 inner join tb2 using(col3)
  where tb1.col1 in (5,6)

His execution order (pseudo code):

List outerDataList = "select * from tb1 where col1 in (5,6)"
  for(outerData in outerDataList){
    List innerDataList = "select * from tb2 where col3 = outerData.col3"
      for(innerData : innerDataList){
        output(outterData,innerData)
      }
  }

MySQL believe that all queries are associated with a query , if the query a table, the above process is also suitable, but only need to complete the basic operation of the above outer layer.

Let's look at left outter jointhe process the query, SQL as follows:

select tb1.col1, tb2,col2
from tb1 left outer join tb2 using(col3)
where tb1.col1 in (5,6)

Pseudo-code as follows:

List outerDataList = "select * from tb1 where col1 in (5,6)"
  for(outerData in outerDataList){
    List innerDataList = "select * from tb2 where col3 = outerData.col3"
      if(innerDataList != null){
        for(innerData : innerDataList){
          output(outterData,innerData)
        }
      }else{
        // inner表无对应数据,以outter数据为准
        output(outterData,null)
      }
  }

But this traversal query can not meet all of the joint inquiry, such as "full outer join" query (full outer join) can not use this method to achieve, which may be the reason MySQL does not support full external queries ~~~

2. Optimize

MySQL will generate a query command tree, such as four tables union query command tree as follows:

MySQL will first execute efficiency of SQL statements to evaluate before generating the instruction tree, he thought then select the most efficient execution order of relevance. For the following SQL:

EXPLAIN SELECT
    actor.NAME,
    film.title 
FROM
    actor actor
    INNER JOIN film_actor USING ( actor_id )
    INNER JOIN film USING ( film_id )

As can be seen from the execution plan, MySQL select the film as the first association table to get the data and then sequentially scanned film_actor, actor table fetch. MySQL option strategy is to try to make fewer query execution nested loops and backtracking operations , therefore, he will try to less amount of data that the outer query. Because the film table has only four records, actor table has six record, so I think the film selected as the first table began to query a higher efficiency.

But MySQL optimization strategies will calculate the cost of all the order of execution of more complex than that, MySQL will then select the best execution plan he thought. However, if the table joint inquiry is more, he does not necessarily exhaustive implementation of all choose the best execution strategy, so this default optimization is not necessarily always the best way. Or more than one SQL as an example, assume that the establishment of the index in the field of film film_id table, even if the field is less than the actor on film, using the actor may query the table as the first table, more efficient (the inner layer of nesting You can use the index when querying film table data). If you think there is a better implementation of the order, you can use STRAIGHT_JOINthe keyword forcibly execute the query sequence:

EXPLAIN SELECT
    actor.NAME,
    film.title 
FROM
    actor actor
    STRAIGHT_JOIN film_actor USING ( actor_id )
    STRAIGHT_JOIN film USING ( film_id )

Note: Most of the time, than human judgment to make MySQL to be accurate, most of the time, is not recommended to enforce the order.

Guess you like

Origin www.cnblogs.com/moongeek/p/11332504.html