Sql sever execution order

Investigation Yan Shen S QL Sever  execution sequence

Sqlsever code does not press the coding order is processed, the processed clause is a first FROM clause processed last appearing first SELECT statement, each step will generate a virtual table, the virtual table is used in the next step input.

(8)SELECT (9)DISTINCT  (11)<Top Num> <select list>

(1)FROM [left_table]

(3)<join_type> JOIN <right_table>

(2)ON <join_condition>

(4)WHERE <where_condition>

(5)GROUP BY <group_by_list>

(6)WITH <CUBE | RollUP>

(7)HAVING <having_condition>

(10)ORDER BY <order_by_list>

 

 

 

About logical query processing stage

1.FROM: on the FROM clause in the previous two tables performed Cartesian product (Cartesian product) (cross-coupling), to generate a virtual table VT1

2.ON: to ON filter VT1 application. Only those who make <join_condition> true line was only inserted VT2.

3.OUTER (JOIN): as  if specified OUTER JOIN ( relative CROSS JOIN or (INNER JOIN), reservation table (preserved table: LEFT OUTER JOIN the left table marked as reserved table, the right outer join right table marked as reserved table full outer join the two tables are marked as reserved table) match is found as an external row rows are added to VT2, generating VT3. If the FROM clause contains two or more tables, is coupled to generate a result table and the next table repeat steps 1 to 3 until all tables completely processed.

4.WHERE: for WHERE filters VT3 application. Only when the <where_condition> to row insertion was only true of VT4.

5.GROUP BY: by column list in the GROUP BY clause to group the rows VT4, generating VT5.

6.CUBE | ROLLUP: the super-group (Suppergroups) insert VT5, generate VT6.

7.HAVING: for HAVING filters VT6 application. Only when the <having_condition> is set to true will be inserted VT7.

8.SELECT: processing the SELECT list, generate VT8.

9.DISTINCT: duplicate rows from removal VT8 in generating VT9.

10.ORDER BY: the VT9 rows sorted list of columns in the ORDER BY clause, generate a cursor (VC10).

11.TOP: From the selected specified number or percentage of rows at the beginning of the VC10, to generate the table VT11, and returns to the caller.

Note: Step 10, according to a column in the ORDER BY clause to sort the list of steps rows returned, return the cursor VC10. This step is the first and only step can use steps alias SELECT column list. This step is different from the other steps, it does not return a valid table, but returns a cursor. SQL is based on set theory. It is not pre-sorted set of rows, it is only a logical set of members, members of the order does not matter. Sort the table query may return an object that contains a particular physical organization row order. This object is called the ANSI cursor. This step is to understand the correct understanding of SQL foundation.

Because this step does not return the table (but returns the cursor), use the ORDER BY clause of a query can not be used as a table expression. Table expression comprising: a view, inline table valued function, sub-queries, and the common expressions derived table. It results must be returned to the client application to give the desired physical records.

Guess you like

Origin www.cnblogs.com/zhangdashao/p/11443198.html