Partition:Partiton Scheme是否指定Next Used?

在SQL Server中,爲Partition Scheme屢次指定Next Used,不會出錯,最後一次指定的FileGroup是Partition Scheme的Next Used,建議,在執行Partition Split操做以前,都要爲Partition Scheme指定Next Used。sql

可是,SQL Server是否提供metadata,查看Partiton Scheme是否指定Next Used FileGroup?答案是系統視圖:sys.destination_data_spaces。若是存在FileGroup被指定爲Next Used ,那麼視圖返回的Partition的個數會比Partition Function劃分的分區數量多1個。app

一,分析視圖:sys.destination_data_spaces函數

該視圖返回三列,表示Partition Scheme的每一個Partition和FileGroup之間的關係:測試

  • partition_scheme_id :ID of the partition-scheme that is partitioning to the data space.                  
  • destination_id :ID (1-based ordinal) of the destination-mapping, unique within the partition scheme.     
  • data_space_id :ID of the data space to which data for this scheme's destination is being mapped.

從表的存儲結構來分析這三列的意義:ui

  • partition_scheme_id 是數據表存儲的空間,該空間不是具體的某個FileGroup。普通的表只有一個分區,只能存儲在單個FileGroup中,可是,經過Partition Scheme,將表數據分割成多個分區,每一個分區存儲到指定的FileGroup中,在物理存儲上,每一個分區都是分開(separate)存儲的。
  • destination_id:是Partition Number,每一個分區的編號
  • data_space_id:是FileGroupID,分區存儲的FileGroup。

二,測試用例this

1,建立分區函數spa

-- create parition function
CREATE PARTITION FUNCTION pf_int_Left (int)
AS 
RANGE LEFT 
FOR VALUES (10,20);

2,建立分區scheme3d

--create partition scheme
CREATE PARTITION SCHEME PS_int_Left
AS 
PARTITION pf_int_Left
TO ([primary], [primary], [primary]);

3,在split partition以前,必須使用alter partition scheme 指定一個Next Used FileGroup。若是Partiton Scheme沒有指定 next used filegroup,那麼alter partition function split range command 執行失敗,不改變partition scheme。code

--split range and add new one boudary value
ALTER PARTITION FUNCTION pf_int_Left ()
split range (30);

Msg 7710, Level 16, State 1, Line 2
Warning: The partition scheme 'PS_int_Left' does not have any next used filegroup. Partition scheme has not been changed.blog

4,若是檢查 Partiton Scheme是否指定Next Used FileGroup?

使用sys.destination_data_spaces視圖來檢查,該系統視圖返回Partition 和filegroup之間的Mapping關係。若是一個FileGoup被alter partition scheme 標記爲next used Filegroup,那麼Partition 的個數會比多Partition function劃分的分區多一個。

複製代碼
select ps.name as PartitionSchemeName,
    ps.data_space_id as PartitionSchemeID,
    pf.name as PartitionFunctionName,
    ps.function_id as PartitionFunctionID,
    pf.boundary_value_on_right,
    dds.destination_id as PartitionNumber,
    dds.data_space_id as FileGroupID
from sys.partition_schemes ps
inner join sys.destination_data_spaces dds
    on ps.data_space_id=dds.partition_scheme_id
inner join sys.partition_functions pf
    on ps.function_id=pf.function_id
where ps.name='PS_int_Left'
複製代碼

上述腳本返回3個partition,說明沒有next used filegroup。

5,使用 alter partition scheme標記 next used filegroup

--alter partition scheme to mark next used filegroup
ALTER PARTITION SCHEME PS_int_Left 
NEXT USED [db_fg1];

查看分區個數

複製代碼
select ps.name as PartitionSchemeName,
    ps.data_space_id as PartitionSchemeID,
    pf.name as PartitionFunctionName,
    ps.function_id as PartitionFunctionID,
    pf.boundary_value_on_right,
    dds.destination_id as PartitionNumber,
    dds.data_space_id as FileGroupID
from sys.partition_schemes ps
inner join sys.destination_data_spaces dds
    on ps.data_space_id=dds.partition_scheme_id
inner join sys.partition_functions pf
    on ps.function_id=pf.function_id
where ps.name='PS_int_Left'
複製代碼

能夠看到,多了一個partition,partition number=4,存放的FileGroupID=2。

6,將 FileGroup 取消標記爲 next used filegroup

--alter partition scheme to cancel next used filegroup
ALTER PARTITION SCHEME PS_int_Left 
NEXT USED;

7,Merge Range移除FileGroup

--merge range
ALTER PARTITION FUNCTION pf_int_Left ()
merge range (20);

查看Partition Function指定的Boundary Value

複製代碼
select pf.name as PartitionFunctionName,
    pf.function_id,
    pf.type,
    pf.type_desc,
    pf.boundary_value_on_right,
    pf.fanout,
    prv.boundary_id,
    prv.value
from sys.partition_functions pf
inner join sys.partition_range_values prv
    on pf.function_id=prv.function_id
where pf.name='pf_int_Left'
複製代碼

綁定到Partition Scheme的Filegroup以下

複製代碼
select ps.name as PartitionSchemeName,
    ps.data_space_id as PartitionSchemeID,
    pf.name as PartitionFunctionName,
    ps.function_id as PartitionFunctionID,
    pf.boundary_value_on_right,
    dds.destination_id as PartitionNumber,
    dds.data_space_id as FileGroupID
from sys.partition_schemes ps
inner join sys.destination_data_spaces dds
    on ps.data_space_id=dds.partition_scheme_id
inner join sys.partition_functions pf
    on ps.function_id=pf.function_id
where ps.name='PS_int_Left'
複製代碼

參考文檔:

How to Remember the Next Used Filegroup in a Partition Scheme

相關文章
相關標籤/搜索