• Partition2:对现有表分区


    在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]);
    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_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

  • 相关阅读:
    vue 重定向
    vue 通过插槽分发内容
    vue 表单输入绑定 checkbox
    jq enter键发送
    vue footer点击变色
    vue computed和methods 计算属性和侦听器
    实时监听input输入情况
    关于Input输入框蓝色外框的操作
    鼠标悬浮指针变手
    鼠标悬浮样式
  • 原文地址:https://www.cnblogs.com/ljhdo/p/5036346.html
Copyright © 2020-2023  润新知