SQL query processing steps_performance optimization

Write the conclusion first:
1, On and Where of Inner Join (Written according to the convenience of reading, the query analyzer will automatically optimize)
2, Write Inner Join first and then Left Join
3, first connect the table with a small amount of data and then connect the table with a large amount of data
4, If there are Left Join and Group By at the same time, first Group By to the temporary table, and then use the temporary table Left Join other data
4. When optimizing, if there are Left Join and Inner Join at the same time, first Inner Join to the temporary table and then use the temporary table Left Join other data
5, do not sort unless necessary


SQL query processing steps [reproduced]

The most obvious feature that sets SQL apart from other programming languages ​​is the order in which code is processed. In most programming languages, code is processed in encoding order, but in SQL languages, the first clause to be processed is the FROM clause, and although the SELECT statement appears first, it is almost always processed last.
Each step produces a virtual table that is used as input for the next step. These virtual tables are not available to callers (client applications or external queries). Only the table generated in the last step is returned to the caller. If a clause is not specified in the query, the corresponding step will be skipped. The following is a brief description of the various logical steps that apply to SQL Server 2000 and SQL Server 2005.
Introduction to Logical Query Processing Phases
FROM: Perform a Cartesian product (cross join) on the first two tables in the FROM clause to generate a virtual table VT1
ON: Apply ON filter to VT1. Only those rows that make <join_condition> true are inserted into VT2.
OUTER(JOIN): If OUTER JOIN is specified (as opposed to CROSS JOIN or (INNER JOIN), reserved table: left outer join marks the left table as a reserved table, right outer join marks the right table as a reserved table, completely The outer join marks both tables as reserved tables) No matching rows are found in VT2 as outer rows, resulting in VT3. If the FROM clause contains more than two tables, the result table generated by the previous join and the next Repeat steps 1 to 3 for a table until all tables are processed.
WHERE: Apply the WHERE filter to VT3. Only rows that make <where_condition> true are inserted into VT4.
GROUP BY: Groups the rows in VT4 by the column list in the GROUP BY clause, producing VT5.
CUBE|ROLLUP: Insert Suppergroups into VT5 to generate VT6.
HAVING: Apply the HAVING filter to VT6. Only groups with <having_condition> true will be inserted into VT7.
SELECT: Process the SELECT list, producing VT8.
DISTINCT: Remove duplicate lines from VT8, yielding VT9.
ORDER BY: Sorts the rows in VT9 by the column list in the ORDER BY clause, generating a cursor (VC10).
TOP: Select the specified number or proportion of rows from the beginning of VC10, generate table VT11, and return to the caller.
Note: Step 10, sort the rows returned in the previous step by the column list in the ORDER BY clause, and return to the cursor VC10. This step is the first and only step that can use the column aliases in the SELECT list. This step differs from the other steps in that it does not return a valid table, but a cursor. SQL is based on set theory. A collection doesn't pre-order its rows, it's just a logical collection of members, the order of the members doesn't matter. A query that sorts a table can return an object containing rows organized in a specific physical order. ANSI refers to such objects as cursors. Understanding this step is the foundation of a proper understanding of SQL.
Because this step does not return a table (it returns a cursor), queries that use the ORDER BY clause cannot be used as table expressions. Table expressions include: views, inline table-valued functions, subqueries, derived tables, and pooled expressions. Its results must be returned to client applications expecting physical records.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326378726&siteId=291194637