检查 Partiton Scheme是否mark Next Used FileGroup,需要使用一个system view:sys.destination_data_spaces,如果存在Next Used FileGroup,那么视图返回的Partition的个数会比Partition Function划分的分区数量+1.
Contains a row for each data space destination of a partition scheme.
Column name |
Data type |
Description |
---|---|---|
partition_scheme_id |
int |
ID of the partition-scheme that is partitioning to the data space. |
destination_id |
int |
ID (1-based ordinal) of the destination-mapping, unique within the partition scheme. |
data_space_id |
int |
ID of the data space to which data for this scheme‘s destination is being mapped. |
表示一个Partition Scheme中的每个Partition和FileGroup之间的关系。
destination_id 是Partition Number
data_space_id 是FileGroupID
1,创建分区函数
-- create parition function CREATE PARTITION FUNCTION pf_int_Left (int) AS RANGE LEFT FOR VALUES (10,20);
2,创建分区scheme
--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。
--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.
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‘
参考文档:
http://sqlfascination.com/2009/09/30/how-to-remember-the-next-used-filegroup-in-a-partition-scheme/