Partition2:对现有表分区

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:

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

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

数据库中已有分区函数(partition function) 和分区架构(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]);
复制代码

如果在普通表上存在聚集索引,并且聚集索引列是分区列,那么重建聚集索引,就能使表转化成分区表。聚集索引的创建有两种方式:使用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_name
DROP [ 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_pk
drop constraint pk__dt_partition_ID
with( 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_pk
drop constraint pk__dt_partition_ID

alter table  dbo.dt_partition_pk
add 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_ID
on 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_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

 

作者悦光阴
本文版权归作者和博客园所有,欢迎转载,但未经作者同意,必须保留此段声明,且在文章页面醒目位置显示原文连接,否则保留追究法律责任的权利。

分类: Partition






本文转自悦光阴博客园博客,原文链接:http://www.cnblogs.com/ljhdo/p/5036346.html,如需转载请自行联系原作者
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
3月前
|
人工智能 自然语言处理 监控
分区Partition介绍及应用
本文详细介绍分区Partition的概念及使用场景,具体如何使用。
|
SQL HIVE
hive插入分区报错SemanticException Partition spec contains non-partition columns
hive插入分区报错SemanticException Partition spec contains non-partition columns
|
算法
每个 Partition
每个 Partition
74 0
|
关系型数据库 数据库 PostgreSQL
PostgreSQL分区表(Table Partitioning)应用
一、简介   在数据库日渐庞大的今天,为了方便对数据库数据的管理,比如按时间,按地区去统计一些数据时,基数过于庞大,多有不便。
1691 0
|
SQL 算法 关系型数据库
PostgreSQL 普通表在线转换为分区表 - online exchange to partition table
PostgreSQL 普通表在线转换为分区表 - online exchange to partition table
2583 0
|
大数据 数据库管理 数据库
Greenplum介绍 - Partitioned Table
GP支持分区表,主要用来存放大表,如fact table 目的: 1. 把大数据切片,便于查询 2. 便于数据库维护 分区创建时,每个分区会自带一个Check约束,来限定数据的范围。
1371 0
|
关系型数据库 MySQL