首页 > 代码库 > SQL Server 2005 分区表创建实例
SQL Server 2005 分区表创建实例
--创建一个分区函数(默认为左边界)
CREATE PARTITION FUNCTION PARTFUNC1(INT)
AS RANGE
FOR VALUES(1000,2000,3000,4000,5000);
--创建一个分区方案
CREATE PARTITION SCHEME PARTSCHEME1
AS PARTITION PARTFUNC1
ALL TO ([PRIMARY]);
--创建一个分区表
CREATE TABLE PARTTABLE1
(
col1 int,
col2 int,
col3 int
) on partscheme1(col1);
--插入测试记录
insert into parttable1 values(10,10,10)
insert into parttable1 values(999,10,10)
insert into parttable1 values(1000,10,10)
insert into parttable1 values(2000,10,10)
insert into parttable1 values(3000,10,10)
insert into parttable1 values(5000,10,10)
insert into parttable1 values(6000,10,10)
insert into parttable1 values(7000,10,10)
insert into parttable1 values(9000,10,10)
insert into parttable1 values(993,10,10)
insert into parttable1 values(6000,10,10)
insert into parttable1 values(5000,10,10)
insert into parttable1 values(7000,10,10)
insert into parttable1 values(6600,10,10)
insert into parttable1 values(8200,10,10)
insert into parttable1 values(8900,10,10)
insert into parttable1 values(17000,10,10)
insert into parttable1 values(61600,10,10)
insert into parttable1 values(81200,10,10)
insert into parttable1 values(18900,10,10)
insert into parttable1 values(10,10,10)
insert into parttable1 values(999,10,10)
insert into parttable1 values(1000,10,10)
insert into parttable1 values(2000,10,10)
insert into parttable1 values(3000,10,10)
insert into parttable1 values(5000,10,10)
insert into parttable1 values(6000,10,10)
insert into parttable1 values(7000,10,10)
insert into parttable1 values(9000,10,10)
insert into parttable1 values(1993,10,10)
insert into parttable1 values(16000,10,10)
insert into parttable1 values(15000,10,10)
insert into parttable1 values(17000,10,10)
insert into parttable1 values(16600,10,10)
insert into parttable1 values(18200,10,10)
insert into parttable1 values(15000,10,10)
insert into parttable1 values(17000,10,10)
insert into parttable1 values(16000,10,10)
insert into parttable1 values(12000,10,10)
insert into parttable1 values(11000,10,10)
--查看数据在分区的分布情况
select o.name,p.partition_number,p.rows
from sys.objects o join sys.partitions p on o.object_id=p.object_id
where o.type=‘u‘ and o.name=‘parttable1‘ and p.index_id=0
--查看更多细节
select $partition.partfunc1(col1) as partition,
col1 as [data],col2,col3
from parttable1
order by partition
--查看分区的配置情况
select f.name,f.type_desc,f.fanout,p.boundary_id,p.value
from sys.partition_functions f join sys.partition_range_values p on f.function_id=p.function_id
--合并分区,将1000<X<=2000和2000<X<=3000的合并
alter partition function partfunc1()
merge range(2000)
--拆分分区,将1000<X<=3000的分区,以2000为界进行拆分
alter partition function partfunc1()
split range(2000)
--拆分分区之前如果分区方案指定的文件组数据小于分区函数中的分区数据,则要修改分区方案
alter partition scheme partscheme1
next used [filegroup_name]
--存档分区,创建一个存档表
create table parttable2
(
col1 int,
col2 int,
col3 int
) on partscheme1(col1)
--将分区1的数据存档
alter table parttable1
switch partition 1
to parttable2 partition 1
--检查数据是否归档
select $partition.partfunc1(col1) as partition,* from parttable1
order by partition
--where $partition.partfunc1(col1)=1
select * from parttable2
--将存档的数据重新加载
alter table parttable2
switch partition 1
to parttable1 partition 1
--如果将非分区表的数据转移到分区表呢?