MySQL related query

1. Execution process of associated query

        In general, MySQL believes that any query is an association, and it is not just a query that requires two tables to match to be called an association.

Therefore, in mysql, every query, every fragment (including sub-queries, or even single table select) may be related.

Therefore, it is important to understand how mysql performs associated queries. Let ’s first look at an example of union. For union, mysql first puts a series of single query results into a temporary table.

Then re-read the temporary table data to complete the union. In the concept of mysql, each query is an association, so the temporary table of the read result is also an association.

First, the MySQL optimizer should determine who drives the table, that is, which table is used as the benchmark. When dealing with such problems, the MySQL optimizer uses a simple and crude solution:

Which table has a small result set, which table is used as the driving table, of course, the actual processing method of the MySQL optimizer will be much more complicated

 

Such as:

mysql encountered a subquery in the from clause, first execute the subquery and put the result in a temporary table, and then treat this temporary table as a normal table (as its name: derived table),

mysql also uses a similar temporary table when executing union queries. When it encounters a right outer connection, mysql rewrites it as an equivalent left outer connection. In other words, the current version of mysql will change all query types. Switch to a similar execution plan,

However, not all queries can be converted into the above form, such as: full outer connection can not be completed by nested loops and backtracking, which is why mysql does not support full outer connection.

 

Implementation plan:

Unlike many other relational databases , mysql does not generate query bytecode to execute the query, mysql generates an instruction tree for the query,

Then execute the instruction tree through the storage engine and return the result. The final execution plan contains all the information of the reconstructed query. For example , after executing explain extended on a query ,

Then execute show warnings, you can view the reconstructed query. mysql always nests loops starting from one table and backtracking to complete the association of all tables. 

Therefore, the execution plan of mysql is a depth-first tree on the left , and then trace back to the previous level of association:

 

Related query optimizer:

The most important part of the mysql optimizer is associated query optimization, which determines the order of multiple table association, usually when multiple tables are associated,

There can be many different association orders to obtain the same execution effect, and the association query optimizer chooses the least expensive association order by evaluating the cost when the order is different.

 

2. Optimization

About straight_join:

Straight_join realizes the loading order of mandatory multiple tables, from left to right, such as:

...A straight_join B on A.name = B.name

Straight_join is completely equivalent to inner join except that the join syntax is based on "which table has a small result set, which table is the driving table" to determine who loads first, and straight_join will force the table on the left to load first.

 

note:

In general, STRAIGHT_JOIN only applies to inner joins, because left join and right join already know which table is used as the drive table and which table is used as the driven table.

For example, left join uses the left table as the driving table, right join is the opposite, and STRAIGHT_JOIN is used in the inner connection, and the left table is forced to be used as the driving table.

So this feature can be used for some tuning, forcibly changing the execution plan selected by MySQL's optimizer.

But often when analyzing mysql processing performance, such as (Explain), if you find that mysql is in an unreasonable loading order, you can use this statement, but often mysql can automatically analyze and handle it.

 

 

Guess you like

Origin www.cnblogs.com/ZJOE80/p/12714868.html