Analysis of the performance and efficiency of MYSQl left JOIN query

Source: Internet
Author: User
Tags joins mysql tutorial
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 there is also a user_id=4, Action=swim record in the user_action, but not in the results,
Id=3 in the user table, Name=daodao users do not have a corresponding record in user_action, but they appear in the result set
Because now is the left join, all work is left.
Results 1,2,3,4 are both in the left table and on the right table record, 5 is only on the left table, not on the right table record


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

Extended:
We can use the right table without on match to show the law of NULL, to find all the records in the left table, not the right table, note that the column to be judged must be declared not NULL.
Such as:
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. A column value of NULL should be null and cannot be used with =null
2. Here the a.user_id column must be declared not NULL
Result
ID | name | Action
————————–
3 | Daodao | Null

——————————————————————————–

Tips Tutorial:
1. On a.c1 = B.C1 equivalent to using (C1)
2. INNER JOIN and, (comma) are semantically equivalent
3. When MySQL retrieves information from a table, you can indicate which index it chooses.
This feature is useful if EXPLAIN shows that MySQL uses the wrong index in the list of possible indexes.
By specifying use index (key_list), you can tell MySQL to find a record row in a table using the most appropriate index in a possible index.
An optional two-choice syntax IGNORE index (key_list) can be used to tell MySQL not to use a specific index.
4. Some examples:
MySQL Tutorial > 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. mysql how to optimize left join and right join
In MySQL, A left JOIN B join_condition is executed as follows:

· Set Table B based on all tables that are dependent on tables A and a.

· Set Table A for all tables (except B) that are used in the left join condition.

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

· All standard joins can be optimized, except for tables that are read from all tables on which it relies. If there is a circular dependency, MySQL prompts for an error.

· Perform all standard where optimizations.

· If a row in a matches a WHERE clause, but no row in B matches the on condition, then another B row is generated, where all the columns are set to NULL.

· If you use a LEFT join to find a row that does not exist in some tables, and the following test: the where part of the col_name is NULL, where the col_name is a column declared not NULL, MySQL finds a match Stop after one line of the join condition (for a specific keyword combination) to search for additional rows.

The execution of the right join is similar to a left join, but the role of the table is reversed.

Joins the optimizer in the order in which the tables should be joined. Left JOIN and Straight_join forced table read Order can help the Join optimizer work faster because fewer table exchanges are checked. Note that this means that if you perform the following type of query, MySQL performs a full scan of B because the 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 fix is in the reverse order of a, 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 for the resulting null row, the Where condition is always false, and the left join becomes a normal join.

For example, in the following query, if T2.COLUMN1 is the Null,where clause, it will be 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 can be faster because MySQL can use table t2 before table T1 If you can make the query better. To enforce the use of table order, use Straight_join.
Related Article

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.