在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

Partition2:对现有表分区的更多相关文章

  1. SQL Server表分区-水平分区

    SQL Server表分区,sql server水平分区 转自:http://www.cnblogs.com/knowledgesea/p/3696912.html  根据时间的,直接上T-SQL代码 ...

  2. 8、SQL Server 表分区

    什么是表分区?表分区其实就是将一个大表分成若干个小表.表分区可以从物理上将一个大表分成几个小表,但是逻辑上还是一个表.所以当执行插入.更新等操作的时候,不需要我们去判断应该插入或更新到哪个表中.只需要 ...

  3. sqlserver2012 表分区

    无论是新建数据库,还是现有的问题,都可以执行表分区的操作. 1.在数据库中点鼠标右键点击属性,在选择页,选中文件栏,在数据库文件列表中,可以看到现有的数据库文件逻辑名称.文件类型.初始大小.保存位置等 ...

  4. MySQL表分区技术

    MySQL表分区技术 MySQL有4种分区类型: 1.RANGE 分区 - 连续区间的分区 - 基于属于一个给定连续区间的列值,把多行分配给分区: 2.LIST 分区 - 离散区间的分区 - 类似于按 ...

  5. 如何在大型的并且有表分区的数据库中进行DBCC CHECKDB操作

    如何在大型的并且有表分区的数据库中进行DBCC CHECKDB操作 其实这个问题已经在<SQLSERVER企业级平台管理实践>里徐老师已经讲过了,不过我想用自己的语言再讲详细一些 笔记链接 ...

  6. Oracle-13:Oracle中的表分区

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客记录了表分区 表分区的含义: 典型的拿空间换时间的案例! 表分区对一张表进行分区,分区之后表中的数据存 ...

  7. mysql 表分区技术

    表分区,是指根据一定规则,将数据库中的一张表分解成多个更小的,容易管理的部分.从逻辑上看,只有一张表,但是底层却是由多个物理分区组成. 表分区有什么好处: a.分区表的数据可以分布在不同的物理设备上, ...

  8. mysql表分区实战

    一,什么是数据库分区以mysql为例,mysql数据库中的数据是以文件的形势存在磁盘上的,默认放在/mysql/data下面 (可以通过my.cnf中的datadir来查看),一张表主要对应着三个文件 ...

  9. oracle表分区创建

    一.什么是分区表表分区有以下优点: 1.数据查询:数据被存储到多个文件上,减少了I/O负载,查询速度提高. 2.数据修剪:保存历史数据非常的理想. 3.备份:将大表的数据分成多个文件,方便备份和恢复. ...

随机推荐

  1. CSS未知宽高元素水平垂直居中

    方法一 :table.cell-table 思路:显示设置父元素为:table,子元素为:cell-table,这样就可以使用vertical-align: center,实现水平居中优点:父元素(p ...

  2. 安装SQL sever2008时显示重新启动计算机规则失败,应该怎么解决?

    1.删除注册表:在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager中找到 PendingFileRenameOpe ...

  3. 推荐一个国外C开发的PHP框架--Phalcon,性能相当好

    本人亲自配置测试后.性能相当不错.不过有一点.使用极不符合国人习惯,甚至和大多数主流PHP框架如Zend Framework,Yii,Ci,Thinkphp都不一样. Phalcon 是一个开源的,全 ...

  4. Windows 2012桌面显示“我的电脑”

    Windows 2012桌面显示“我的电脑”  rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0 

  5. 关于mybatis反向生成为什么有时候实体类会变成两个

    一般来说,将TEXT字段,从一张操作频繁的表中拆分出去,成为一个Key-Value结构的独立表是 好处颇多的. 其有利之处主要体现在下面三个方面: PS:以下的讨论对象均基于Innodb引擎 1. 便 ...

  6. SQL 性能不佳的几个原因

    •不准确的统计数据•差劲的索引•差劲的查询设计 •差劲的执行计划,通常是由不正确的参数引起的•过度阻塞和死锁 •非基于集合的操作•不良数据库设计 •过度碎片 •不能重复使用执行计划 •查询频繁重编译 ...

  7. zepto.js不支持scrollTop的解决办法

    zepto.js不支持animate({ scrollTop: 100},1000); 可以在移动端使用原生window.scrollTop(x,y);简便

  8. H5页面meta标签小结:

    <meta name="viewport" content="width=device-width,user-scalable=no"> <m ...

  9. 动态显示checkbox选中条数

    <script> $('input[type=checkbox]').click( function () { $('span#cheak_len').empty(); var len = ...

  10. leetcode717—1-bit and 2-bit Characters

    We have two special characters. The first character can be represented by one bit 0. The second char ...