在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之间的关系:测试
从表的存储结构来分析这三列的意义:ui
二,测试用例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