MYSQlleftjoin combined query efficiency analysis _ MySQL

Source: Internet
Author: User
MYSQlleftjoin union query efficiency analysis user table:

Id | name
---
1 | libk
2 | zyfon
3 | daodao

User_action table:

User_id | action
-----
1 | jump
1 | kick
1 | jump
2 | run
4 | swim

SQL:
Select id, name, action from user as u
Left join user_action a on u. id = a. user_id

Result:
Id | name | action
-----------
1 | libk | jump ①
1 | libk | kick ②
1 | libk | jump ③
2 | zyfon | run ④
3 | daodao | null ⑤

Analysis:
Note that user_action has a user_id = 4, action = swim record, but it does not appear in the result,
In the user table, the id = 3 and name = daodao users do not have corresponding records in user_action, but they appear in the result set.
Because it is left join, all work is subject to left.
Result 1, 2, 3, 4 are records in both the left table and the right table. 5 is a record in only the left table, not in the right table.

Conclusion:
We can imagine that left join works like this.
Read one record from the left table and select all records (n records) in the right table that match on to form n records (including duplicate rows, for example: result 1 and result 3 ),
If there is no table matching the on condition on the right side, all connected fields are null.
Then read the next one.

Extended:
If there is no on matching in the right table, we can display the null rule to find all records in the left table, not in the right table. Note that the column to be judged must be declared as not null.
For example:
SQL:
Select id, name, action from user as u
Left join user_action a on u. id = a. user_id
Where a. user_id is NULL
(Note: 1. if the column value is null, it should be "is null" instead of "= NULL ".
2. here the. user_id column must be declared as not null)
Result:
Id | name | action
---------
3 | daodao | NULL

---------------------------

Tips:
1. on a. c1 = B. c1 is equivalent to using (c1)
2. inner join and (comma) are semantically equivalent.
3. when MySQL retrieves information from a table, you can prompt which index it chooses.
This feature is useful if the EXPLAIN command shows that MySQL uses an index that may be incorrect in the index list.
By specifying the use index (key_list), you can tell MySQL to USE the most appropriate INDEX to find record rows in the table.
The optional syntax ignore index (key_list) can be used to tell MySQL not to use a specific INDEX.
4. some examples:
Mysql> SELECT * FROM table1, table2 WHERE table1.id = table2.id;
Mysql> SELECT * FROM table1 left join table2 ON table1.id = table2.id;
Mysql> SELECT * FROM table1 left join table2 USING (id );
Mysql> SELECT * FROM table1 left join table2 ON table1.id = table2.id
-> Left join table3 ON table2.id = table3.id;
Mysql> SELECT * FROM table1 use index (key1, key2)
-> WHERE key1 = 1 AND key2 = 2 AND key3 = 3;
Mysql> SELECT * FROM table1 ignore index (key3)
-> WHERE key1 = 1 AND key2 = 2 AND key3 = 3;

7.2.9. how does MySQL optimize left join and right join?
In MySQL, the execution process of a left join B join_condition is as follows:

· Set Table B based on all tables on which table A and table A depend.

· Set Table A according to all tables (except B) used in the LEFT JOIN condition.

· The left join condition is used to determine how to search rows from Table B. (In other words, do not use any conditions in the WHERE clause ).

· All standard Joins can be optimized, except for tables read from all the tables it depends on. If a circular dependency occurs, MySQL prompts an error.

· Perform all standard WHERE optimizations.

· If A has A row that matches the WHERE clause, but B does not have A row that matches the ON condition, another B row is generated. all columns are set to NULL.

· If left join is used to locate rows that do NOT exist in some tables and perform the following test: the col_name is null in the WHERE section, WHERE col_name IS a column declared as not null, mySQL finds a row that matches the left join condition and stops searching for other rows (for a specific keyword combination.

The execution of right join is similar to that of left join, but the role of the table is the opposite.

The order in which the join Optimizer calculates the table to be joined. The forced read sequence of left join and STRAIGHT_JOIN can help the JOIN Optimizer to work faster, because fewer table exchanges are checked. Note that if the following type of query is executed, MySQL performs full scan B, because LEFT JOIN forces it to read before d:

SELECT *
FROM a, B LEFT JOIN c ON (c. key = a. key) LEFT JOIN d ON (d. key = a. key)
WHERE B. key = d. key;
In this case, the reverse order of a is used for restoration, and B is listed in the FROM clause:

SELECT *
FROM B, a LEFT JOIN c ON (c. key = a. key) LEFT JOIN d ON (d. key = a. key)
WHERE B. key = d. key;
MySQL can perform the following left join optimization: if the NULL row is generated, the WHERE condition is always false, and the left join is changed to a normal JOIN.

For example, in the following query, if t2.column1 is NULL, the WHERE clause is false:

SELECT * FROM t1 left join t2 ON (column1) WHERE t2.column2 = 5;
Therefore, you can safely convert a query to a normal join:

SELECT * FROM t1, t2 WHERE t2.column2 = 5 AND t1.column1 = t2.column1;
This is faster, because MySQL can use table T2. To force table order, use STRAIGHT_JOIN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.