博客
关于我
Partition2:对现有表分区
阅读量:439 次
发布时间:2019-03-06

本文共 3996 字,大约阅读时间需要 13 分钟。

在SQL Server中,普通表可以转化为分区表,而分区表不能转化为普通表,普通表转化成分区表的过程是不可逆的,将普通表转化为分区表的方法是:

在分区架构(Partition Scheme)上创建聚集索引,就是说,将聚集索引分区。

数据库中已有分区函数(partition function) 和分区架构(Partition scheme):

-- create parition functionCREATE PARTITION FUNCTION pf_int_Left (int)AS RANGE LEFT FOR VALUES (10,20);--determine partition numberselect $Partition.pf_int_left(21)CREATE PARTITION SCHEME PS_int_LeftAS PARTITION pf_int_LeftTO ([primary], [primary], [primary]);
View Code

如果在普通表上存在聚集索引,并且聚集索引列是分区列,那么重建聚集索引,就能使表转化成分区表。聚集索引的创建有两种方式:使用clustered 约束(primary key 或 unique约束)创建,使用 create clustered index 创建。

一,在分区架构(Partition Scheme)上,创建聚集索引

如果聚集索引是使用 create clustered index 创建的,并且聚集索引列就是分区列,使普通表转换成分区表的方法是:删除所有的 nonclustered index,在partition scheme上重建clustered index

1,表dbo.dt_partition的聚集索引是使用 create clustered index 创建的,

create table dbo.dt_partition(ID int,Code int)create clustered index cix_dt_partition_ID on dbo.dt_partition(ID)

2,从系统表Partition中,查看该表的分区只有一个

select *from sys.partitions p where p.object_id=object_id(N'dbo.dt_partition',N'U')

3,使用partition scheme,重建表的聚集索引

create clustered index cix_dt_partition_ID on dbo.dt_partition(ID)with(drop_existing=on)on PS_int_Left(ID)

4,重建聚集索引之后,表的分区有三个

select *from sys.partitions p where p.object_id=object_id(N'dbo.dt_partition',N'U')

二,如果表的聚集索引是使用Primary key clustered 来创建,并且primary key 就是分区列

在SQL Server中,不能修改约束,将普通表转换成分区表,有两种方式来实现,第一种方式是:在删除clustered constraint 时,将数据移动到分区scheme上;第二种方式,删除clustered constraint,在分区scheme上重建clustered constraint。

1,在删除clustered 约束时,将数据移动到分区scheme上

使用 alter table drop constraint 命令,在删除聚集索引时,将数据移动到指定的Partition Scheme上,此时该表变成分区的堆表:

ALTER TABLE schema_name . table_nameDROP [ CONSTRAINT ]  constraint_name  [ WITH ( MOVE TO { partition_scheme_name(partition_column_name ) | filegroup | [default] } )]

move to 选项的作用是将Table移动到新的Location中,如果新的location 是partition scheme,那么在删除clustered 约束时,SQL Server将表数据移动到分区架构中,这种操作和使用 create table on partition scheme创建分区表的作用相同。

create table dbo.dt_partition_pk(ID int not null constraint pk__dt_partition_ID primary key clustered ,Code int not null)alter table dbo.dt_partition_pkdrop constraint pk__dt_partition_IDwith( move to PS_int_Left(ID))

2,删除clustered 约束,在partition scheme上重建clustered 约束

create table dbo.dt_partition_pk(ID int not null constraint pk__dt_partition_ID primary key clustered ,Code int not null)alter table dbo.dt_partition_pkdrop constraint pk__dt_partition_IDalter table  dbo.dt_partition_pkadd constraint pk__dt_partition_ID primary key clustered(ID)on PS_int_Left(ID);

三,将堆表转换成分区表

使堆表转换成分区,只需要在堆表上创建一个分区的clustered index

create table dbo.dt_partition_heap(ID int not null,Code int not null)create clustered index cix_partition_heap_IDon dbo.dt_partition_heap(ID)on PS_int_Left(ID)

四,普通表转化成分区表的过程是不可逆的,普通表能够转化成分区表,而分区表不能转化成普通表。

普通表存储的Location是FileGroup,分区表存储的Location是Partition Scheme,在SQL Server中,存储表数据的Location叫做Data Space。通过在Partition Scheme上创建Clustered Index ,能够将已经存在的普通表转化成partition table,但是,将Clustered index删除,表仍然是分区表转化过程(将普通表转换成分区表)是不可逆的,一个Partition Table 是不能转化成普通表的,即使通过合并分区,使Partiton Table 只存在一个Partition,这个表的仍然是Partition Table,这个Table的Data Space 是Partition Scheme,而不会转化成File Group。

从 sys.data_spaces 中查看Data Space ,共有两种类型,分别是FG 和 PS。FG是File Group,意味着数据表的数据存储在File Group分配的存储空间,一个Table 只能存在于一个FileGroup中。PS 是Partition Scheme,意味着将数据分布式存储在不同的File Groups中,存储数据的File Group是根据Partition column值的范围来分配的。对于分区表,SQL Server从指定的File Group分配存储空间,虽然一个Table只能指定一个Partition Scheme,但是其数据却分布在多个File Groups中,这些File Groups由Partition Scheme指定,可以相同,也可以不同。

查看Table的Data Space,通过索引的data_space_id 字段来查看各个索引(聚集索引是表本身)数据的存储空间:

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_descfrom sys.indexes iinner join sys.objects o on o.object_id=i.object_idinner join sys.data_spaces ds on i.data_space_id=ds.data_space_idwhere 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

 

转载地址:http://vixyz.baihongyu.com/

你可能感兴趣的文章
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>