[英]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:我有几个关于计划的问题:
CASE
statement", but they were pretty opaque.CASE
语句”的内容,但它们非常不透明。 How can I tell what the "Compute Scalar"s are doing? 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.这样,它会一起接收一个组的所有行,并可以在它看到它已完成处理所有行时发出该组的聚合总数(并重置下一组的聚合)。
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
.如果
fact
对division = '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点匹配的行。的SUM
的3
和3
是6
)
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]))
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.内部
COUNT
和COUNT_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.