Altering a Partition Function: SPLIT and MERGE
Partition functions are not static: They can change over time, and their changes propagate through the partition scheme to the partitioned table. You can alter a partition function to change the boundary value list one value at a time, a method that makes it possible to add and remove new partitions. (You cannot change the partition function‘s data type or range direction, though, just the boundary values.)
The Partition Scheme
You must provide a sufficient number of filegroups to handle all of the partition function‘s partitions. If you supply more filegroups than there are partitions defined in the partition function, SQL Server will mark the first filegroup listed after the existing partitions are mapped as NEXT USED, that is, the next filegroup to use if the number of partitions increases, and ignore the rest. SQL Server only keeps track of the existing mapped filegroups and the single filegroup designated as NEXT USED.
Altering a Partition Scheme
After you‘ve created a partition scheme, there‘s only one change you can make to it: You can adjust which filegroup will be considered the next used. For example, the following code makes Filegroup5 the next to be used.
ALTER PARTITION SCHEME PS2_Right NEXT USED Filegroup5;
To remove all potential filegroups from being marked as next used, just reissue the ALTER PARTITION SCHEME command with no filegroups.
ALTER PARTITION SCHEME PS2_Right NEXT USED;
Marking a filegroup as NEXT USED means that if a partition is split by adding a new boundary value using ALTER PARTITION FUNCTION, the new partition will be assigned to the filegroup marked as NEXT USED.
Using SPLIT with Partition Schemes
If you modify a partition function to add or remove a boundary value with SPLIT or MERGE, SQL Server automatically adjusts any partition scheme‘s filegroups that use that partition function.
To add a new boundary value to the partition function used a partition scheme, you must have a filegroup that has been marked as NEXT USED.
Using MERGE with Partition Schemes
If you remove a boundary value using the MERGE option, a partition is removed from the partition function, and therefore one less filegroup is required. The filegroup that is removed is the filegroup where the removed boundary point resided.
The filegroup that was removed from the partition scheme will be marked as the NEXT USED if no other filegroup was already marked as such.
一,示例数据
1,创建partition function
-- create parition function CREATE PARTITION FUNCTION pf_int_Left (int) AS RANGE LEFT FOR VALUES (10,20);
2,创建partition scheme
--create partition scheme CREATE PARTITION SCHEME PS_int_Left AS PARTITION pf_int_Left TO ([primary], [primary], [primary]);
3,创建parition table
create table dbo.dt_partition ( ID int, Code int ) on PS_int_Left (ID)
二,partition function 和 partition scheme 修改
1,split 分区
在split分区之前,必须 alterpartition scheme ,mark 一个 next used filegroup。
--alter partition scheme ALTER PARTITION SCHEME PS_int_Left NEXT USED [primary];
修改partition function,增加一个boundary value,split分区
--split range and add new one boudary value ALTER PARTITION FUNCTION pf_int_Left () split range (30);
查看分区的数量
select * from sys.partitions p where p.object_id=object_id(N‘dbo.dt_partition‘,N‘U‘)
2,Merge 分区
Merge分区,不需要修改partition scheme,只需要修改partition function,将一个已经存在的boundary value消除,相邻的两个分区合并。
在merge 分区之后,多余的一个FileGroup会被mark为 next used filegroup或被remove。
修改partition function,使用一个已经存在的boundary value进行partition merge。
ALTER PARTITION FUNCTION pf_int_Left () Merge range (20);
查看表的分区
select * from sys.partitions p where p.object_id=object_id(N‘dbo.dt_partition‘,N‘U‘)
3,如何查看Next Used FileGroup?
mark一下,需要继续学习,,,,
三,Alter Partition Syntax
1,使用 alter partition scheme 增加或删除 Next Used FileGroup
Adds a filegroup to a partition scheme or alters the designation of the NEXT USED filegroup for the partition scheme.
ALTER PARTITION SCHEME partition_scheme_name NEXT USED [ filegroup_name ] [ ; ]
filegroup_name
Specifies the filegroup to be marked by the partition scheme as NEXT USED. This means the filegroup will accept a new partition that is created by using an ALTER PARTITION FUNCTION statement.
In a partition scheme, only one filegroup can be designated NEXT USED.
A filegroup that is not empty can be specified. If filegroup_name is specified and there currently is no filegroup marked NEXT USED, filegroup_name is marked NEXT USED. If filegroup_name is specified, and a filegroup with the NEXT USED property already exists, the NEXT USED property transfers from the existing filegroup to filegroup_name.
If filegroup_name is not specified and a filegroup with the NEXT USED property already exists, that filegroup loses its NEXT USED state so that there are no NEXT USED filegroups in partition_scheme_name.
If filegroup_name is not specified, and there are no filegroups marked NEXT USED, ALTER PARTITION SCHEME returns a warning.
2,使用ALTER PARTITION FUNCTION 分拆和合并分区
Alters a partition function by splitting or merging its boundary values. By executing ALTER PARTITION FUNCTION, one partition of any table or index that uses the partition function can be split into two partitions, or two partitions can be merged into one less partition.
More than one table or index can use the same partition function. ALTER PARTITION FUNCTION affects all of them in a single transaction.
ALTER PARTITION FUNCTION partition_function_name() { SPLIT RANGE ( boundary_value ) | MERGE RANGE ( boundary_value ) } [ ; ]
SPLIT RANGE ( boundary_value )
Adds one partition to the partition function. boundary_value determines the range of the new partition, and must differ from the existing boundary ranges of the partition function. Based on boundary_value, the Database Engine splits one of the existing ranges into two. Of these two, the one where the new boundary_value resides is considered the new partition.
A filegroup must exist online and be marked by the partition scheme that uses the partition function as NEXT USED to hold the new partition. Filegroups are allocated to partitions in a CREATE PARTITION SCHEME statement. If a CREATE PARTITION SCHEME statement allocates more filegroups than necessary (fewer partitions are created in the CREATE PARTITION FUNCTION statement than filegroups to hold them), then there are unassigned filegroups, and one of them is marked NEXT USED by the partition scheme. This filegroup will hold the new partition. If there are no filegroups marked NEXT USED by the partition scheme, you must use ALTER PARTITION SCHEME to either add a filegroup, or designate an existing one, to hold the new partition. A filegroup that already holds partitions can be designated to hold additional partitions. Because a partition function can participate in more than one partition scheme, all the partition schemes that use the partition function to which you are adding partitions must have a NEXT USED filegroup. Otherwise, ALTER PARTITION FUNCTION fails with an error that displays the partition scheme or schemes that lack a NEXT USED filegroup.
If you create all the partitions in the same filegroup, that filegroup is initially assigned to be the NEXT USED filegroup automatically. However, after a split operation is performed, there is no longer a designated NEXT USED filegroup. You must explicitly assign the filegroup to be the NEXT USED filegroup by using ALTER PARITION SCHEME or a subsequent split operation will fail.
MERGE [ RANGE ( boundary_value) ]
Drops a partition and merges any values that exist in the partition into one of the remaining partitions. RANGE (boundary_value) must be an existing boundary value, into which the values from the dropped partition are merged. The filegroup that originally held boundary_value is removed from the partition scheme unless it is used by a remaining partition, or is marked with the NEXT USED property. The merged partition resides in the filegroup that originally did not hold boundary_value. boundary_value is a constant expression that can reference variables (including user-defined type variables) or functions (including user-defined functions). It cannot reference a Transact-SQL expression. boundary_value must either match or be implicitly convertible to the data type of its corresponding partitioning column, and cannot be truncated during implicit conversion in a way that the size and scale of the value does not match that of its corresponding input_parameter_type.
Best Practices
Always keep empty partitions at both ends of the partition range to guarantee that the partition split (before loading new data) and partition merge (after unloading old data) do not incur any data movement. Avoid splitting or merging populated partitions. This can be extremely inefficient, as this may cause as much as four times more log generation, and may also cause severe locking.
Limitations and Restrictions
ALTER PARTITION FUNCTION repartitions any tables and indexes that use the function in a single atomic operation. However, this operation occurs offline, and depending on the extent of repartitioning, may be resource-intensive.
ALTER PARTITION FUNCTION can only be used for splitting one partition into two, or merging two partitions into one. To change the way a table is otherwise partitioned (for example, from 10 partitions to 5 partitions), you can exercise any of the following options. Depending on the configuration of your system, these options can vary in resource consumption:
- Create a new partitioned table with the desired partition function, and then insert the data from the old table into the new table by using an INSERT INTO...SELECT FROM statement.
- Create a partitioned clustered index on a heap. Note:Dropping a partitioned clustered index results in a partitioned heap.
- Drop and rebuild an existing partitioned index by using the Transact-SQL CREATE INDEX statement with the DROP EXISTING = ON clause.
- Perform a sequence of ALTER PARTITION FUNCTION statements.
All filegroups that are affected by ALTER PARITITION FUNCTION must be online.
ALTER PARTITION FUNCTION fails when there is a disabled clustered index on any tables that use the partition function.
SQL Server does not provide replication support for modifying a partition function. Changes to a partition function in the publication database must be manually applied in the subscription database.