在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