创建分区表2:对已经存在的表进行分区
2015-12-10 15:59
369 查看
普通表转化成分区表的过程是不可逆的,普通表能够转化成分区表,而分区表不能转化成普通表。
下面是Create Partition function 和 Create Partition Scheme的脚本
一,如果表有聚集索引,并且聚集索引列就是分区列,那么重建聚集索引使表分区。聚集索引的创建有两种方式,一是使用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 创建的,
分区只有一个
重建表的聚集索引,并使用partition scheme
重建聚集索引之后,表的分区有三个
2,如果表的聚集索引是使用Primary key clustered 来创建,并且primary key 就是分区列,使用 alter table drop constraint 命令使表分区
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 创建示例表
查看表的分区,只有一个
2.2 删除 PK clustered 约束,并将聚集索引的叶子节点的数据移动到 new location(partition scheme)。
删除 PK clustered 约束之后,表没有PK,没有index,是个分区的heap。
查看分区
二,如果已经存在的表是heap,使堆表分区非常简单,只需要建立一个分区的clustered index。
1,示例数据表
查看表分区
2,创建聚集并在on子句中应用 partition scheme。
查看分区
三,使用SSMS UI 创建Partition Table
如果不想影响表的结构,可以将聚集索引删除,表仍然是分区表。
1,SSMS提供了一个非常便利的UI来对普通表进行分区,单击 Storage->Create partition... 菜单
2,选择partition column
partition column只有一个,勾选 Storage-align all non-unique indexes and unique indexes with indexed partitioning column。
3,Create partition function
4,create partition scheme
5,为partition scheme指定 boundary value 和 FileGroup
6,查看创建的脚本
四,普通表转化成分区表的过程是不可逆的,普通表能够转化成分区表,而分区表不能转化成普通表。
通过创建Clustered index 来使已经存在的普通表转化成partition table,如果不想影响表的结构,可以将Clustered index删除,表仍然是分区表。这个转化的过程是不可逆的,一个Partition Table 是不能转化成普通表的,即使通过合并分区,使Partiton Table 只存在一个Partition,这个表的仍然是Partition Table,这个Table的Data Space 是Partition Scheme,而不会转化成File Group,只不过这个Partition Table 和普通表一样,都只有一个Partition。
一个DB的Data Space 可以从 sys.data_spaces 中查看,共有两种类型,分别是FG 和 PS。FG是File Group,意思是Table是从File Group中分配存储空间,一个Table 只能存在于一个FileGroup中。PS 是Partition Scheme,意思是存储Table数据的File Group是根据Partition column值的范围来分配的,SQL Server可以从不同的File Group分配存储空间给Table,虽然一个Table只能指定一个Partition Scheme,但是其数据却可以分布在不同的File Group中。每一个Partition column 值得范围都叫一个Partition,Partition 可以存在于同一个FileGroup中,也可以存在于不同的FileGroup中。
Script,查看Table的Data Space。
在分区之前,查看Data_space是Name是 Primary File Group
在分区之后,查看Table的 Data Space 是ps_int Partition Scheme
目前无法将Table的Data Space 转化成FG
下面是Create Partition function 和 Create Partition Scheme的脚本
-- 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')
三,使用SSMS UI 创建Partition Table
如果不想影响表的结构,可以将聚集索引删除,表仍然是分区表。
1,SSMS提供了一个非常便利的UI来对普通表进行分区,单击 Storage->Create partition... 菜单
2,选择partition column
partition column只有一个,勾选 Storage-align all non-unique indexes and unique indexes with indexed partitioning column。
3,Create partition function
4,create partition scheme
5,为partition scheme指定 boundary value 和 FileGroup
6,查看创建的脚本
BEGIN TRANSACTION CREATE PARTITION FUNCTION [pf_int](int) AS RANGE LEFT FOR VALUES (N'1', N'100') CREATE PARTITION SCHEME [ps_int] AS PARTITION [pf_int] TO ([PRIMARY], [PRIMARY], [PRIMARY]) CREATE CLUSTERED INDEX [ClusteredIndex_on_ps_int_635896021514996028] ON [dbo].[dt_test_partition] ( [CreatedDateKey] )WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [ps_int]([CreatedDateKey]) DROP INDEX [ClusteredIndex_on_ps_int_635896021514996028] ON [dbo].[dt_test_partition] COMMIT TRANSACTION
四,普通表转化成分区表的过程是不可逆的,普通表能够转化成分区表,而分区表不能转化成普通表。
通过创建Clustered index 来使已经存在的普通表转化成partition table,如果不想影响表的结构,可以将Clustered index删除,表仍然是分区表。这个转化的过程是不可逆的,一个Partition Table 是不能转化成普通表的,即使通过合并分区,使Partiton Table 只存在一个Partition,这个表的仍然是Partition Table,这个Table的Data Space 是Partition Scheme,而不会转化成File Group,只不过这个Partition Table 和普通表一样,都只有一个Partition。
一个DB的Data Space 可以从 sys.data_spaces 中查看,共有两种类型,分别是FG 和 PS。FG是File Group,意思是Table是从File Group中分配存储空间,一个Table 只能存在于一个FileGroup中。PS 是Partition Scheme,意思是存储Table数据的File Group是根据Partition column值的范围来分配的,SQL Server可以从不同的File Group分配存储空间给Table,虽然一个Table只能指定一个Partition Scheme,但是其数据却可以分布在不同的File Group中。每一个Partition column 值得范围都叫一个Partition,Partition 可以存在于同一个FileGroup中,也可以存在于不同的FileGroup中。
Script,查看Table的Data Space。
select o.name as TableName,o.type_desc, i.name as IndexName, i.index_id,i.type_desc,i.data_space_id, ds.name as DataSpaceName,ds.type_desc from sys.indexes i inner join sys.objects o on o.object_id=i.object_id inner join sys.data_spaces ds on i.data_space_id=ds.data_space_id where i.object_id=object_id(N'[dbo].[dt_test_partition]') and i.index_id=0
在分区之前,查看Data_space是Name是 Primary File Group
在分区之后,查看Table的 Data Space 是ps_int Partition Scheme
目前无法将Table的Data Space 转化成FG
相关文章推荐
- seajs中spm压缩工具使用
- 解决“The type initializer for'Oracle.DataAccess.Client.OracleConnection' threw an exception ”异常
- 详解CSS设置默认字体样式
- solr java api
- Sublime相关配置
- Ubuntu 14.04 端口映射
- 如何查看linux 日志 磁盘使用情况?
- python使用tab补齐
- iOS开发之opaque、alpha
- NSPredicate(谓词) 模糊、精确、查询
- ansible命令应用示例
- 搭建nginx+tomcat+Java的负载均衡环境
- OCP-1Z0-051-2015-56题
- Android 反射实战 - 更换APP皮肤<3>
- 重载(overloading)和重写(overriding)
- Android研发规范
- Ubuntu apt-get 更改默认下载位置
- Unity3D 游戏开发架构篇 ——性格一流的设计和持久性
- 【Java并发编程二】同步容器和并发容器
- 从窗体泄漏谈android:configChanges属性