Execution order parsing of T-SQL query statements _mssql

Source: Internet
Author: User
Tags microsoft sql server

(5) SELECT (5-2) DISTINCT (5-3) Top (<top_specification>) (5-1) <select_list>

(1) from (1-j) <left_table> <join_type> join <right_table> on <on_predicate>

(2) WHERE <where_predicate>

(3) GROUP by <group_by_specification>

(4) Having

(6) Order by <order_by_list>;

Each step generates a virtual table that will act as the next input. These virtual tables are not available to callers, and only the virtual tables that were generated in the last step are returned to the caller. If you do not specify a clause in the query, the corresponding step is skipped. The next step is to briefly describe these different logical steps.

(1) the From from phase identifies the source table of the query, handles the table operators, and each table operator applies a series of child stages. For example, the stage involved in a JOIN operation is (1-J1) Cartesian product, (1-J2) on filter, and (1-J3) adding an outer row. The From phase generates virtual table VT1.

(1-j1) Cartesian product This phase performs a Cartesian product (cross join) of the two tables involved in table operators, generating virtual table vt1-j1.

(1-j2) onfilter This phase filters the rows in Vt1-j1 according to the predicates that appear in the ON clause. Only rows that have the predicate value true can be inserted into the vt1-j2.

(1-J3) Add an outer row if a outer join (relative to a cross join or a INNER join) is specified, no matching row is found in the reserved table (preserved table), as the outer row is added to the vt1-j2. Generate VT1-J3.

(2)where this phase filters the rows in VT1 according to the predicate (<where_predicate>) that appears in the WHERE clause. Only rows that have the predicate evaluate to true are inserted into the VT2.

(3) Group by grouping rows in VT2 to generate VT3 according to the list of column names specified in the GROUP BY clause.

(4) A group in VT3 is screened according to the predicate (

(5)Select processes the elements in the SELECT clause to generate VT5.

(5-1) evaluates an expression to evaluate an expression in the select list to generate a vt5-1.

(5-2)DISTINCT Delete duplicate rows in vt5-1 to generate vt5-2.

(5-3) Top selects the previously specified number or percentage of rows from the Vt5-2, based on the logical sort defined by the Order BYI clause, and generates the table vt5-3.

(6) Order by ordering the rows in vt5-3 based on the list of column names specified in the orders by clause, generating the cursor VC6.

The above is a complete T-SQL statement of the implementation process, I hope to give you help.

This is part of the Microsoft SQL Server 2008 Technology Insider T-SQL query.

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.