One of mysql statement-level query optimization

mysql statement-level query optimization one
mysql statement-level query optimization two


The highest state of optimizing SQL is to return to the requirements. Only when the requirements are correctly understood can efficient SQL be written. Therefore, rewriting SQL is often an important means of optimizing SQL.
Although understanding the execution plan is an extremely important part of SQL optimization, if you cannot understand the requirements, write complex and tedious SQL statements with twists and turns, and optimize by analyzing the execution plan, you will often fall into the Helpless.

1. Join query type

1.1, SQL writing order

SELECT <select_list>
FROM
	<left_table> <join_type> JOIN <right_table> ON <join_condition>
WHERE <where_condition>
GROUP BY <group_by_list>
HAVING <having_condition>
ORDER BY <order_by_condition>
LIMIT <limit_number>

1.2, SQL execution order

FROM <left_table>
ON <join_condition>
<join_type> JOIN <right_table> 
WHERE <where_condition>
GROUP BY <group_by_list>
HAVING <having_condition>
SELECT
DISTINCT <select_list>
ORDER BY <order_by_condition>
LIMIT <limit_number>

1.3. Summary of SQL execution results

2 Diagram of JOIN query result set

2.1 LEFT JOIN ON

SELECT <select_list> 
FROM tableA A
LEFT JOIN tableB B
ON A.key = B.key;

2.2 RIGHT JOIN ON

SELECT <select_list> 
FROM tableA A
RIGHT JOIN tableB B
ON A.key = B.key;

2.3 INNER JOIN ON

join 默认为 inner join

SELECT <select_list> 
FROM tableA A
INNER JOIN tableB B
ON A.key = B.key;

2.4 LEFT JOIN ON WHERE

SELECT <select_list> 
FROM tableA A
LEFT JOIN tableB B
ON A.key = B.key
WHERE B.key IS NULL;

2.5 RIGHT JOIN ON WHERE

SELECT <select_list> 
FROM tableA A
RIGHT JOIN tableB B
ON A.key = B.key
WHERE A.key IS NULL;

2.6 FULL OUTER JOIN ON

SELECT <select_list> 
FROM tableA A
FULL OUTER JOIN tableB B
ON A.key = B.key

2.7 FULL OUTER JOIN ON WHERE

SELECT <select_list> 
FROM tableA A
FULL OUTER JOIN tableB B
ON A.key = B.key
WHERE A.key IS NULL OR 
	  B.key IS NULL

2.3. Create table SQL

2.4, 7 join instances

3. Index Introduction

4. Performance analysis

5. Index optimization

1. Performance decline SQL slow, long execution time, long waiting time

Guess you like

Origin blog.csdn.net/Michael_lcf/article/details/105003536#comments_25439025