问一个执行计划的问题

zlcqupt 2009-09-03 03:31:17
《Microsoft.Press.Inside.Microsoft.SQL.Server.2005.T-SQL.Querying.Apr.2006》里面关于执行计划,有一段代码


USE Northwind;

SELECT C.CustomerID, COUNT(O.OrderID) AS NumOrders
FROM dbo.Customers AS C
LEFT OUTER JOIN dbo.Orders AS O
ON C.CustomerID = O.CustomerID
WHERE C.City = 'London'
GROUP BY C.CustomerID
HAVING COUNT(O.OrderID) > 5
ORDER BY NumOrders;

它说的执行顺序是:
where C.City = 'London'先找到Customers里一行,再根据这一行里的CustomerID找到Orders里的所有的行,再流聚合,计算标量得到COUNT(O.OrderID),经过筛选器到达排序。
然后再找到Customers里where C.City = 'London'的下一行,重复上面的顺序。

请问,当Customers里满足where C.City = 'London'的记录,它们的CustomerID可能相同,也可能不相同,如果有相同的,执行顺序还会是上面的顺序吗?
...全文
78 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlcqupt 2009-09-03
  • 打赏
  • 举报
回复
可能纠结这些顺序用处不大。。。

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>
(8) SELECT (9) DISTINCT (11) <TOP_specification> <select_list>
这里,可以根据where涉及了一个table还是两个table, 4的顺序稍微调整一下?
不懂
zlcqupt 2009-09-03
  • 打赏
  • 举报
回复
The engine starts execution by performing the Index Seek at the top of Figure 2-2 on the Customers tableand it will select the first row with the customer residing in London. You can see the seek predicate Prefix: [Northwind].[dbo].[Customers].City = N'London' in a small pop-up window if you hover the cursor over the Index Seek on the Customer table icon, as shown in Figure 2-3. The selected row is passed to the Nested Loops operator on the arrow 1, and as soon as it reaches the Nested Loops the so-called inner side of the Nested Loops operator is activated. In our case, in Figure 2-2 the inner side of the Nested Loops operator consists of the Compute Scalar, Stream Aggregate, and Index Seek operators connected to the Nested Loops by arrows 4, 3, and 2, respectively.

这是《Microsoft.Press.Inside.Microsoft.SQL.Server.2005.T-SQL.Querying.Apr.2006》里的《Flow of Data During Query Processing》一章里的。
如果各位老大在自己的机器里看执行计划的话,Figure 2-2指整个执行计划图,Figure 2-3指嵌套循环里的索引查找,查找键是.city=N'London'....
zlcqupt 2009-09-03
  • 打赏
  • 举报
回复
我觉得(可能不对)
当Customers里满足where C.City = 'London'的记录,它们的CustomerID可能相同,也可能不相同,如果有相同的,

在循环嵌套与筛选器之前,应该有一个流聚合和计算标量,而不应该只有一个计算标量
===================

不知道应该怎么理解。
lihan6415151528 2009-09-03
  • 打赏
  • 举报
回复

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>
(8) SELECT (9) DISTINCT (11) <TOP_specification> <select_list>

黄_瓜 2009-09-03
  • 打赏
  • 举报
回复
--查询的逻辑执行过程,来自技术内幕 
(8) SELECT (9) DISTINCT (11) <TOP_specification> <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>
--小F-- 2009-09-03
  • 打赏
  • 举报
回复
SQL Select语句完整的执行顺序:

1、from子句组装来自不同数据源的数据;
2、where子句基于指定的条件对记录行进行筛选;
3、group by子句将数据划分为多个分组;
4、使用聚集函数进行计算;
5、使用having子句筛选分组;
6、计算所有的表达式;
7、使用order by对结果集进行排序。


另外:一个查询语句各个部分的执行顺序:
--8) SELECT (9) DISTINCT (11) <TOP_specification> <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>
zlcqupt 2009-09-03
  • 打赏
  • 举报
回复
SQL77:
你能详细得说一说吗?等他们拿来?
SQL77 2009-09-03
  • 打赏
  • 举报
回复
权威执行顺序!!等他们拿来

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧