SQL Server 2005中的分区函数
在 MSDN 中有关分区函数的信息,$PARTITION(Transact-SQL)。
我对下面的示例的底层功能感到困惑。 我的理解是,这个SQL语句将迭代表Production.TransactionHistory中的所有行,并且因为对于映射到同一分区的所有行,$PARTITION.TransactionRangePF1(TransactionDate)将返回相同的值,即所有行的分区号这样的行。 因此,例如,分区 1 中的所有行将导致返回结果中的一行,因为它们都具有相同的 $PARTITION.TransactionRangePF1(TransactionDate) 值。 我的理解正确吗?
USE AdventureWorks ;
GO
SELECT $PARTITION.TransactionRangePF1(TransactionDate) AS Partition,
COUNT(*) AS [COUNT] FROM Production.TransactionHistory
GROUP BY $PARTITION.TransactionRangePF1(TransactionDate)
ORDER BY Partition ;
GO
In MSDN about partition function from here, $PARTITION(Transact-SQL).
I am confused about what the below sample is doing underlying. My understanding is, this SQL statement will iterate all rows in table Production.TransactionHistory, and since for all the rows which will mapping to the same partition, $PARTITION.TransactionRangePF1(TransactionDate) will return the same value, i.e. the partition number for all such rows. So, for example, all rows in partition 1 will result in one row in returning result since they all of the same value of $PARTITION.TransactionRangePF1(TransactionDate). My understanding correct?
USE AdventureWorks ;
GO
SELECT $PARTITION.TransactionRangePF1(TransactionDate) AS Partition,
COUNT(*) AS [COUNT] FROM Production.TransactionHistory
GROUP BY $PARTITION.TransactionRangePF1(TransactionDate)
ORDER BY Partition ;
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的分区函数定义如下
,则此子句:
相当于:
如果所有日期都在
'2007-01-01'
之前,则第一个WHEN
子句将总是触发并且总是返回1
。您发布的查询将为每个分区最多返回
1
行,因为它将将该分区中的所有行(如果有)分组为一组。如果任何分区都没有行,则结果集中不会返回该分区的任何行。
If your parition function is defined like
, then this clause:
is equivalent to:
If all your dates fall before
'2007-01-01'
, then the firstWHEN
clause will always fire and it will always return1
.The query you posted will return at most
1
row for each partition, as it will group all the rows from the partition (if any) into one group.If there are no rows for any partition, no rows for it will be returned in the resultset.
它返回分区表
Production.TransactionHistory
中每个非空分区中的记录数,所以是的,您的推理是正确的。It returns the number of records in each of the non-empty partitions in the partitioned table
Production.TransactionHistory
, so yes your reasoning is correct.您是否尝试过为该语句生成执行计划? 这可能会让您深入了解它在幕后实际所做的事情。
按“Control-L”生成执行计划,如果您需要一些解释,请将其发布到此处。
Have you tried generating an execution plan for the statement? That might give you some insight into what it's actually doing underneath the cover.
Press "Control-L" to generate an execution plan and post it here if you'd like some interpretation.