partition function 和 partition scheme 修改示例

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.

时间: 2024-10-18 18:09:23

partition function 和 partition scheme 修改示例的相关文章

SQL Server ->> 生成时间类型的Partition Function和Partition Scheme代码

有时工作中要建个分区函数,可是像日期这种分区函数要是搞个几百个的值那不是要搞死我.于是写了点代码自动生成一个从1990年开始的按月的分区函数和对应的分区主题 USE [TestDB] GO DECLARE @STR NVARCHAR(MAX) SET @STR = 'CREATE PARTITION FUNCTION [PF_RangeByMonth_FromYear1990](DATETIME) AS RANGE LEFT FOR VALUES (' DECLARE @STR2 NVARCHA

查看分区表使用的partition scheme 和 partition function

分区表和partition scheme之间的关系是由sys.indexes 确定的,当index_id=0,表示分区表是个heap,表没有创建聚集索引,当index_id=1,表示分区表是个BTree,表存在聚集索引. sys.indexes Contains a row per index or heap of a tabular object, such as a table, view, or table-valued function. sys.indexes 有一个非常关键的字段 d

SQL Server PARTITION FUNCTION(分区)

分区并不影响Linq,sql查询 在MSSQL中,选中目标表,右键-存储-创建分区 根据提示完成分区,存储成sql 这里展示如何根据Id的数据范围分区 在执行前,可能需要设置日志文件大小为"无限制",否则执行过程中可能出现事务日志已满,原因为"LOG_BACKUP"的报错 --其实就是写个生成字符串列表(数据范围)的函数 DECLARE @fenqu nvarchar(max) = N'CREATE PARTITION FUNCTION [fenqu](bigint

Partition算法及Partition算法用于快速排序

JavaScript简单方便,所以用JavaScript实现,可以在Chrome控制台下观察运行结果.主要实现Partition算法,比如输入为   var array = [4, 2, 1, 3, 6, 8, 9, 7, 5];   partition(array, 0, 8); 那么按照array[0]即4进行划分,结果为 [3, 2, 1, 4, 6, 8, 9, 7, 5] ?1. [代码][JavaScript]代码 // 先来看Partition算法,Partition算法是快速排序

C++11中function和bind的用法示例

环境Visual Studio 2012,具体代码如下 #include <iostream> #include <functional> #include <string> void PrintNumber(int num) { std::cout << num << std::endl; } struct Printer { void Print(std::string print_str) { std::cout << prin

mysql partition(mysql range partition,对历史数据建分区)

官方文档:http://www.w3resource.com/mysql/mysql-partition.php 对于分区的类型,以及新建表时就写好分区的话,按照例子做就好了. 这边文章主要是为了处理历史数据,也就是表里已经有海量的数据,比如两年的,一天的数据就是G+的,像这样的话,我们必须及早处理这些没有提前分区的表. 按照range 分区,主要是按照月和天来分区. e.g.1: (1) CREATE TABLE sale_mast (bill_no INT NOT NULL, bill_da

entity framework model.tt 模板修改示例

//添加引用 public string UsingDirectives(bool inHeader, bool includeCollections = true) { return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion()) ? string.Format( CultureInfo.InvariantCulture, "{0}using System;{1}{3}" + "{2}"

Partition Table

what is Partition Table? Looking to optimize the performance of your SQL Server database? If your database contains very large tables, you may benefit from partitioning those tables onto separate filegroups. Allows you to spread data onto different p

sqlserver 编辑、修改字段说明(备注) sp_addextendedproperty

语法   sp_addextendedproperty [ @name = ] { 'property_name' } [ , [ @value = ] { 'value' } [ , [ @level0type = ] { 'level0_object_type' } , [ @level0name = ] { 'level0_object_name' } [ , [ @level1type = ] { 'level1_object_type' } , [ @level1name = ] {