我在SQL Server中创建了一个分区表,如下所示:
--Add File Groups
Alter Database Test Add Filegroup [Part_20181201]
Go
Alter Database Test Add Filegroup [Part_20181202]
Go
--create files
Alter Database [Test] Add FILE ( NAME = N'Part_20181201', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Part_20181201.ndf' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_20181201]
Alter Database [Test] Add FILE ( NAME = N'Part_20181202', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Part_20181202.ndf' , SIZE = 5120KB , FILEGROWTH = 1024KB ) TO Filegroup [Part_20181202]
--create partition function
CREATE PARTITION FUNCTION DailyFunction (datetime2) AS RANGE RIGHT FOR VALUES ('20181201','20181202');
--create schema
Create Partition Scheme Schemedaily as Partition DailyFunction
To (Part_Before20181201, [Part_20181201], [Part_20181202])
--Create TABLE
Create TABLE [TempTable] (
id int identity(0, 1) not null,
date datetime2 not null,
text char(8000)
)
On Schemedaily (date) ;
如果要截断某些分区:
TRUNCATE TABLE [dbo].[TempTable]
WITH (PARTITIONS (1 TO 2));
现在,可以通过分区名称截断表分区吗?
我的意思是这样的:
TRUNCATE TABLE [dbo].[TempTable] WITH (PARTITIONS (Part_20181201));
答案 0 :(得分:1)
根据Microsoft定义的有分区,否,您不能
可以通过以下方式指定:
•提供分区号,例如:
WITH (PARTITIONS (2))
•提供几个用逗号分隔的单独分区的分区号,例如:
WITH (PARTITIONS (1, 5))
•提供范围和单个分区,例如:
WITH (PARTITIONS (2, 4, 6 TO 8))
•
<range>
可以指定为以单词TO分隔的分区号,例如:WITH (PARTITIONS (6 TO 8))
要截断分区表,必须将表和索引对齐(在同一分区函数上分区)。
答案 1 :(得分:1)
首先,这些不是分区名称,而是文件组名称。 documentation对此非常清楚:
WHERE (the NOT IN)
可以通过以下方式指定:•提供分区号,例如:
<partition_number_expression>
•提供几个用逗号分隔的单独分区的分区号,例如:
WITH (PARTITIONS (2))
•提供范围和单个分区,例如:
WITH (PARTITIONS (1, 5))
•
WITH (PARTITIONS (2, 4, 6 TO 8))
可以指定为以单词TO分隔的分区号,例如:<range>
您可以使用以下查询在文件组中找到分区号:
WITH (PARTITIONS (6 TO 8))
然后构造一个动态sql来截断表。
这是一个查询,用于检查数据库中每个对象和索引所位于的每个分区号是哪个文件组:
SELECT p.partition_number
FROM sys.partitions p
INNER JOIN sys.indexes i ON i.object_id = p.object_id and i.index_id = p.index_id
INNER JOIN sys.data_spaces ds ON ds.data_space_id = i.data_space_id
INNER JOIN sys.filegroups AS fg ON fg.data_space_id = i.data_space_id
WHERE p.object_id = object_id('dbo.TempTable')
and gf.name = 'Part_20181201'
and p.index_id = 1 /* clustered index or 0 for heaps */
答案 2 :(得分:0)
如果你有表分区的Partition功能,你必须使用分区功能来截断表中的特定分区
TRUNCATE TABLE [dbo].[TableName] WITH (PARTITIONS($PARTITION.DailyFunction('Part_201812017')));