简体   繁体   English

了解 SQL Server 执行计划

[英]Understanding a SQL Server execution plan

I have the following query which I'm using to review the SQL Server execution plan.我有以下查询,用于查看 SQL Server 执行计划。

SELECT TOP 1000
    fact.division,
    case when fact.division='east' then 'XXX' else 'YYY' end div,
    count(1)
FROM
    division join fact on (division.division=fact.division) 
where
    fact.division!='east'
group by
    fact.division

And the plan is as follows:计划如下:

在此处输入图片说明

I have a few questions about the plan:我有几个关于计划的问题:

  1. Why does it do a Sort before the Aggregate?为什么它在聚合之前进行排序?
  2. What are the two Stream Aggregate operations for?两个 Stream Aggregate 操作的用途是什么? I could understand doing one after the join, but why two?我可以理解在加入后做一个,但为什么是两个?
  3. Finally, what are the two "Compute Scalar" for?最后,这两个“计算标量”是干什么用的? When I hovered over them I was expecting it to tell me something along the lines of "This is the CASE statement", but they were pretty opaque.当我将鼠标悬停在它们上方时,我以为它会告诉我一些类似于“这是CASE语句”的内容,但它们非常不透明。 How can I tell what the "Compute Scalar"s are doing?我怎么知道“计算标量”在做什么?
  1. Why does it do a Sort before the Aggregate?为什么它在聚合之前进行排序?

A stream aggregate requires the input to be sorted by the group by columns.流聚合要求输入按列group by进行排序。 That way it receives all rows for one group together and can emit the aggregated total for the group (and reset the aggregations for the next group) when it sees that it has finished processing all rows for it.这样,它会一起接收一个组的所有行,并可以在它看到它已完成处理所有行时发出该组的聚合总数(并重置下一组的聚合)。

  1. What are the two Stream Aggregate operations for?两个 Stream Aggregate 操作的用途是什么? I could understand doing one after the join, but why two?我可以理解在加入后做一个,但为什么是两个?

One optimisation that SQL Server can do is to do a partial aggregation before the join to reduce the number of rows going into the join and then calculate the final total after the join. SQL Server 可以做的一种优化是在联接之前进行部分聚合以减少进入联接的行数,然后在联接之后计算最终总数。 If fact has three rows for division = 'west' it can collapse that down to 1 row and pass the value 3 .如果factdivision = 'west'有三行,它可以将其折叠为1行并传递值3 It then just needs to do one lookup in the inner joined table rather than 3. It can then sum the join result to get the final total (ie if division has 2 matching rows. the SUM of 3 and 3 is 6 )然后,它只是需要做一个查找在所述内连接表,而不是3,可再总结连接结果得到最终的总(即,如果division具有2点匹配的行。的SUM336

In SSMS select the operator and look at the "properties" window ( F4 ) to see the "Defined Values" for both stream aggregates.在 SSMS 中,选择运算符并查看“属性”窗口 ( F4 ) 以查看两个流聚合的“定义值”。

The one to the right of the join has expression连接右边的一个有表达式

[partialagg1008] = Scalar Operator(Count(*))

And the one after the join has加入后的那个有

[globalagg1009] = Scalar Operator(SUM([partialagg1008]))
  1. Finally, what are the two "Compute Scalar" for?最后,这两个“计算标量”是干什么用的? When I hovered over them I was expecting it to tell me something along the lines of "This is the CASE statement", but they were pretty opaque.当我将鼠标悬停在它们上方时,我以为它会告诉我一些类似于“这是CASE语句”的内容,但它们非常不透明。 How can I tell what the "Compute Scalar"s are doing?我怎么知道“计算标量”在做什么?

You need to look at defined values for these too.您还需要查看这些定义的值。

One of them has expression [Expr1006] = Scalar Operator(CONVERT_IMPLICIT(int,[globalagg1009],0)) and is casting the result of the COUNT aggregation back to int .其中之一具有表达式[Expr1006] = Scalar Operator(CONVERT_IMPLICIT(int,[globalagg1009],0))并将COUNT聚合的结果转换回int Internally COUNT and COUNT_BIG use the same apparatus that returns bigint - for COUNT this needs a cast to get the final advertised datatype.内部COUNTCOUNT_BIG使用返回bigint的相同设备 - 对于COUNT这需要强制转换以获得最终广告数据类型。

The other one is calculating the result of your CASE expression and has expression [Expr1007] = Scalar Operator(CASE WHEN [avails].[dbo].[fact].[Division]=N'east' THEN 'XXX' ELSE 'YYY' END)另一个是计算你的CASE表达式的结果并且有表达式[Expr1007] = Scalar Operator(CASE WHEN [avails].[dbo].[fact].[Division]=N'east' THEN 'XXX' ELSE 'YYY' END)

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

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