简体   繁体   English

如何在SQL Server 2008中完成查询执行

[英]How query execution done in SQL Server 2008

I am using SQL Server 2008 and would really like to know how the engine executes queries. 我正在使用SQL Server 2008,并且非常想知道引擎如何执行查询。

Let's suppose I run this query : 假设我运行以下查询:

select * from <table1> with(nolock)

or same with a join: 或与联接相同:

select * from <table1> a with(nolock) 
join <table2> b with(nolock) on a.<column1> = b.<column1> 
where <some condition>

How does the query work behind the scene? 查询如何在后台工作?
Is there any way to see actual steps that are performed for execution and showing results? 有什么办法可以查看执行的实际步骤并显示结果?

The database engine analyses the query and selects the best way of retrieving the requested data under the current conditions. 数据库引擎分析查询并选择在当前条件下检索请求的数据的最佳方法。

See Displaying Graphical Execution Plans (SQL Server Management Studio) . 请参见显示图形执行计划(SQL Server Management Studio)

LISTING 1-1 Logical query processing step numbers 清单1-1逻辑查询处理步骤号

(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> | (1-A) <left_table> <apply_type> APPLY <right_table_expression> AS <alias> | (1-P) <left_table> PIVOT(<pivot_specification>) AS <alias>| (1-U) <left_table> UNPIVOT(<unpivot_specification>) AS <alias> (2) WHERE <where_predicate> (3) GROUP BY <group_by_specification> (4) HAVING <having_predicate> (6) ORDER BY <order_by_list>;

Logical Query Processing Phases in Brief 逻辑查询处理阶段简介

■ (1) FROM The FROM phase identifi es the query's source tables and processes table operators. ■(1)FROM FROM阶段标识查询的源表并处理表运算符。 Each table operator applies a series of subphases. 每个表运算符都应用一系列子阶段。 For example, the phases involved in a join are (1-J1) Cartesian Product, (1-J2) ON Filter, (1-J3) Add Outer Rows. 例如,联接中涉及的阶段是(1-J1)笛卡尔积,(1-J2)ON过滤器,(1-J3)添加外部行。 The FROM phase generates virtual table VT1. FROM阶段生成虚拟表VT1。

■ (1-J1) Cartesian Product This phase performs a Cartesian product (cross join) between the two tables involved in the table operator, generating VT1-J1. ■(1-J1)笛卡尔积此阶段在表运算符涉及的两个表之间执行笛卡尔积(交叉联接),生成VT1-J1。

■ (1-J2) ON Filter This phase fi lters the rows from VT1-J1 based on the predicate that appears in the ON clause (). ■(1-J2)ON过滤器此阶段基于ON子句()中的谓词从VT1-J1中筛选行。 Only rows for which the predicate evaluates to TRUE are inserted into VT1-J2. 仅将谓词评估为TRUE的行插入VT1-J2。 (1-J3) Add Outer Rows If OUTER JOIN is specifi ed (as opposed to CROSS JOIN or INNER JOIN), rows from the preserved table or tables for which a match was not found are added to the rows from VT1-J2 as outer rows, generating VT1-J3. (1-J3)添加外部行如果指定了OUTER JOIN(与CROSS JOIN或INNER JOIN相反),则将保留表中未找到匹配项的行作为外部添加到VT1-J2中的行中行,生成VT1-J3。

■ (2) WHERE This phase fi lters the rows from VT1 based on the predicate that appears in the WHERE clause (). ■(2)WHERE该阶段根据WHERE子句()中出现的谓词从VT1中筛选行。 Only rows for which the predicate evaluates to TRUE are inserted into VT2. 仅将谓词评估为TRUE的行插入VT2。

■ (3) GROUP BY This phase arranges the rows from VT2 in groups based on the column list specifi ed in the GROUP BY clause, generating VT3. ■(3)GROUP BY此阶段基于GROUP BY子句中指定的列列表将VT2中的行按组排列,从而生成VT3。 Ultimately, there will be one result row per group. 最终,每个组将有一个结果行。

■ (4) HAVING This phase fi lters the groups from VT3 based on the predicate that appears in the HAVING clause (). ■(4)HAVING此阶段基于HAVING子句()中的谓词从VT3中筛选组。 Only groups for which the predicate evaluates to TRUE are inserted into VT4. 仅将谓词评估为TRUE的组插入VT4。

■ (5) SELECT This phase processes the elements in the SELECT clause, generating VT5. ■(5)SELECT此阶段处理SELECT子句中的元素,生成VT5。

■ (5-1) Evaluate Expressions This phase evaluates the expressions in the SELECT list, generating VT5-1. ■(5-1)评估表达式此阶段评估SELECT列表中的表达式,生成VT5-1。

■ (5-2) DISTINCT This phase removes duplicate rows from VT5-1, generating VT5-2. ■(5-2)DISTINCT此阶段从VT5-1中删除重复的行,从而生成VT5-2。

■ (5-3) TOP This phase fi lters the specifi ed top number or percentage of rows from VT5-2 based on the logical ordering defi ned by the ORDER BY clause, generating the table VT5-3. ■(5-3)TOP此阶段根据ORDER BY子句定义的逻辑顺序,从VT5-2中筛选出指定的行数或最高百分比,从而生成表VT5-3。

■ (6) ORDER BY This phase sorts the rows from VT5-3 according to the column list specifi ed in the ORDER BY clause, generating the cursor VC6. ■(6)ORDER BY此阶段根据ORDER BY子句中指定的列列表对VT5-3中的行进行排序,生成游标VC6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM