[英]How to get COUNT(*) from one partition of a table in SQL Server 2012?
My table have 7 million records and I do split table in 14 part according to ID, each partition include 5 million record and size of partition is 40G. 我的表有700万条记录,我根据ID分成14个部分,每个分区包含500万条记录,分区大小为40G。 I want to run a query to get count in one partition but it scan all partitions and time of Query become very large.
我想运行一个查询来获取一个分区的计数,但它扫描所有分区,查询的时间变得非常大。
SELECT COUNT(*)
FROM Item
WHERE IsComplated = 0
AND ID Between 1 AND 5000000
How can I run my query on one partition only without scan other partition? 如何在不扫描其他分区的情况下在一个分区上运行查询?
Refer http://msdn.microsoft.com/en-us/library/ms188071.aspx 请参阅http://msdn.microsoft.com/en-us/library/ms188071.aspx
B. Getting the number of rows in each nonempty partition of a partitioned table or index The following example returns the number of rows in each partition of table TransactionHistory that contains data. B.获取分区表或索引的每个非空分区中的行数以下示例返回包含数据的表TransactionHistory的每个分区中的行数。 The TransactionHistory table uses partition function TransactionRangePF1 and is partitioned on the TransactionDate column.
TransactionHistory表使用分区函数TransactionRangePF1,并在TransactionDate列上进行分区。 To execute this example, you must first run the PartitionAW.sql script against the AdventureWorks2012 sample database.
要执行此示例,必须首先针对AdventureWorks2012示例数据库运行PartitionAW.sql脚本。 For more information, see PartitioningScript.
有关更多信息,请参阅PartitioningScript。
USE AdventureWorks2012;
GO
SELECT $PARTITION.TransactionRangePF1(TransactionDate) AS Partition,
COUNT(*) AS [COUNT] FROM Production.TransactionHistory
GROUP BY $PARTITION.TransactionRangePF1(TransactionDate)
ORDER BY Partition ;
GO
C. Returning all rows from one partition of a partitioned table or index The following example returns all rows that are in partition 5 of the table TransactionHistory. C.从分区表或索引的一个分区返回所有行以下示例返回表TransactionHistory的分区5中的所有行。 Note Note To execute this example, you must first run the PartitionAW.sql script against the AdventureWorks2012 sample database.
注意注意要执行此示例,必须首先针对AdventureWorks2012示例数据库运行PartitionAW.sql脚本。 For more information, see PartitioningScript.
有关更多信息,请参阅PartitioningScript。
SELECT * FROM Production.TransactionHistory
WHERE $PARTITION.TransactionRangePF1(TransactionDate) = 5 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.