1

在 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
4

3 回答 3

1

If your parition function is defined like

CREATE PARTITION FUNCTION TransactionRangePF1(DATETIME)
AS RANGE RIGHT FOR VALUES ('2007-01-01', '2008-01-01', '2009-01-01')

, then this clause:

$PARTITION.TransactionRangePF1(TransactionDate)

is equivalent to:

CASE
  WHEN TransactionDate < '2007-01-01' THEN 1
  WHEN TransactionDate < '2008-01-01' THEN 2
  WHEN TransactionDate < '2009-01-01' THEN 3
  ELSE 4
END

If all your dates fall before '2007-01-01', then the first WHEN clause will always fire and it will always return 1.

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.

于 2009-03-02T15:21:45.077 回答
0

它返回分区表中每个非空分区中的记录数Production.TransactionHistory,所以是的,您的推理是正确的。

于 2009-03-01T14:01:16.777 回答
0

您是否尝试过为语句生成执行计划?这可能会让您对它在封面下的实际操作有所了解。

如果您想要一些解释,请按“Control-L”生成执行计划并将其发布在此处。

于 2009-03-02T15:06:53.943 回答