[Mysql] SQL execution sequence and optimization ideas

[Mysql] SQL execution sequence and optimization ideas

1. SQL basic statement execution sequence:

In MySQL, the execution order of SELECT, ORDER BY and WHERE clauses is as follows:
a. FROM clause: Specify the table to be queried.
b. WHERE clause: In the table specified by the FROM clause, use the WHERE clause to filter out rows that meet the conditions.
c. GROUP BY clause: Group the results according to the columns specified in the GROUP BY clause.
d. HAVING clause: After grouping in the GROUP BY clause, use the HAVING clause to filter out the groups that meet the conditions.
e. SELECT clause: Select the columns to be returned.
f. ORDER BY clause: Sort the results.
g. LIMIT or OFFSET clause: Limit the number of rows or offsets returned (optional).

Notice:

   这是MySQL一般情况下的执行顺序,但具体的执行顺序可能会受到MySQL查询优化器的影响。MySQL优化器会
根据统计信息、索引和查询规模等因素,选择更合适的执行计划来提高查询性能。
   如果想要了解具体查询的执行计划,可以使用EXPLAIN关键字来分析查询语句的执行计划。通过查看执行计划,
可以了解MySQL优化器选择的具体执行顺序和使用的索引等信息,以便进行性能优化。

2. SQL optimization ideas:

(2.1) Use appropriate indexes : Indexes can speed up queries. Make sure to create indexes on columns that are frequently used in query conditions and join operations. But be aware that indexes also increase the cost of write operations, so the cost of index creation and maintenance needs to be weighed.

(2.2) Write efficient query statements : avoid using overly complex query statements and try to simplify the query logic. Avoid using non-SARG (Search Argument) functions in the WHERE clause. For example, using functions to operate columns in the WHERE clause will result in the inability to use the index.

(2.3) Optimize table structure : Reasonably design the structure of the table to avoid redundant and repeated data. Optimize data models through normalization and denormalization to fit actual query needs. Avoid using large VARCHAR fields to save storage space.

(2.4) Limit the amount of data returned : select only the required columns to avoid unnecessary data transmission and processing. You can use the LIMIT or TOP keyword to limit the number of rows returned.

(2.5) Batch operation data : Try to use batch operations instead of processing data row by row. For example, use the INSERT INTO SELECT statement to insert large amounts of data instead of using a loop to insert row by row.

(2.6) Avoid using wildcard characters : % and _: Using wildcard characters, such as %, in the WHERE clause will cause a full table scan and affect performance. If possible, try to avoid using wildcards or constraints that use wildcards.

(2.7) Optimize subqueries and joint queries : avoid using too many subqueries and joint queries. Temporary tables or table variables can be used to optimize complex queries and reduce query nesting levels.

(2.8) Regularly count and update database statistics : Database statistics are important for query optimization. Ensure that statistics are collected regularly and updated after data changes so that the query optimizer can make correct execution plans.

(2.9) Reasonable use of connection operations : Connection operations (JOIN) can easily obtain relevant data, but too many connection operations will lead to a decrease in query performance. Make sure to join only necessary tables and use the appropriate join type (such as INNER JOIN, LEFT JOIN).

(2.10) Analyze and monitor query performance : Use database performance analysis tools, such as Explain Plan or Query Plan Analyzer, to analyze the query execution plan and performance bottlenecks. Make necessary optimization adjustments based on the analysis results.

Remark:

Guess you like

Origin blog.csdn.net/yqyn6/article/details/130973965