Sql Server 支持对一个已经存在的表进行分区。
-- create parition function CREATE PARTITION FUNCTION pf_int_Left (int) AS RANGE LEFT FOR VALUES (10,20); --determine partition number select $Partition.pf_int_left(21) CREATE PARTITION SCHEME PS_int_Left AS PARTITION pf_int_Left TO ([primary], [primary], [primary]);
一,如果表有聚集索引,并且聚集索引列就是分区列,那么重建聚集索引使表分区。聚集索引的创建有两种方式,一是使用primary key clustered 约束创建,一是使用 create clustered index 创建。
1,如果聚集索引是使用 create clustered index 创建的,并且聚集索引列就是分区列,那么删除所有的 nonclustered index,重建clustered index使表分区。
If the table is clustered on the partition column, you can drop all the indexes except the clustered index, and rebuild the clustered index on the partition scheme, as in the following example.
如果对以下表,聚集索引是使用 create clustered index 创建的,
create table dbo.dt_partition ( ID int, Code int ) create clustered index cix_dt_partition_ID on dbo.dt_partition(ID)
分区只有一个
select * from sys.partitions p where p.object_id=object_id(N‘dbo.dt_partition‘,N‘U‘)
重建表的聚集索引,并使用partition scheme
create clustered index cix_dt_partition_ID on dbo.dt_partition(ID) with(drop_existing=on) on PS_int_Left(ID)
重建聚集索引之后,表的分区有三个
select * from sys.partitions p where p.object_id=object_id(N‘dbo.dt_partition‘,N‘U‘)
2,如果表的聚集索引是使用Primary key clustered 来创建,并且primary key 就是分区列,使用 alter table drop constraint 命令使表分区
ALTER TABLE schema_name . table_name DROP [ CONSTRAINT ] { constraint_name [ WITH ( <drop_clustered_constraint_option> [ ,...n ] )] } [ ,...n ]
WITH <drop_clustered_constraint_option>
Specifies that one or more drop clustered constraint options are set.
MAXDOP = max_degree_of_parallelism <as applies to drop_clustered_constraint_option>
Overrides the max degree of parallelism configuration option only for the duration of the operation.Use the MAXDOP option to limit the number of processors used in parallel plan execution. The maximum is 64 processors.
ONLINE = { ON | OFF } <as applies to drop_clustered_constraint_option>
Specifies whether underlying tables and associated indexes are available for queries and data modification during the index operation. The default is OFF. REBUILD can be performed as an ONLINE operation.
MOVE TO { partition_scheme_name(column_name [ 1, ... n] ) | filegroup | [default] } <as applies to drop_clustered_constraint_option>
Specifies a location to move the data rows currently in the leaf level of the clustered index. The table is moved to the new location. This option applies only to constraints that create a clustered index.
In this context, default is not a keyword. It is an identifier for the default filegroup and must be delimited, as in MOVE TO [default].
关键option是move to,将聚集索引的叶子节点移动到new location,如果new location 是partition scheme界定的分区,那么可以在删除 pk clustered的同时,将表数据分区,这种操作类似使用 create table on partition scheme创建分区表。
2.1 创建示例表
create table dbo.dt_partition_pk ( ID int not null constraint pk__dt_partition_ID primary key clustered , Code int )
查看表的分区,只有一个
select * from sys.partitions p where p.object_id=object_id(N‘dbo.dt_partition_pk‘,N‘U‘)
2.2 删除 PK clustered 约束,并将聚集索引的叶子节点的数据移动到 new location(partition scheme)。
alter table dbo.dt_partition_pk drop constraint pk__dt_partition_ID with( move to PS_int_Left(ID))
删除 PK clustered 约束之后,表没有PK,没有index,是个分区的heap。
查看分区
select * from sys.partitions p where p.object_id=object_id(N‘dbo.dt_partition_pk‘,N‘U‘)
二,如果已经存在的表是heap,使堆表分区非常简单,只需要建立一个分区的clustered index。
1,示例数据表
create table dbo.dt_partition_heap ( ID int not null, Code int )
查看表分区
select * from sys.partitions p where p.object_id=object_id(N‘dbo.dt_partition_heap‘,N‘U‘)
2,创建聚集并在on子句中应用 partition scheme。
create clustered index cix_partition_heap_ID on dbo.dt_partition_heap(ID) on PS_int_Left(ID)
查看分区
select * from sys.partitions p where p.object_id=object_id(N‘dbo.dt_partition_heap‘,N‘U‘)