防窜货系统分区表脚本实例

分区ImportCode

/*准备工作:加入时间字段*/
alter table ImportCode add CreateTime datetime default getdate()

update  ImportCode set createtime = b.ScanDate from ImportCode a inner join ImportInfo b  on a.BatchId=b.Id



/*第一步:创建分区函数*/
Create partition function Part_func_ImportCode(datetime) as range right 
/*正式区间for values(N'01100923909760', N'01100936207030', N'6311001806524',N'92451400060101');*/
for values('20140101','20150101','20160101','20170101');
--CREATE PARTITION FUNCTION [Part_func_Bag](datetime) AS RANGE RIGHT FOR VALUES (N'2011-01-01T00:00:00.000', N'2012-01-01T00:00:00.000', N'2013-01-01T00:00:00.000', N'2014-01-01T00:00:00.000')

go

/*第二步:创建文件组和文件*/
alter database db_haidilao20141015 add filegroup [ImportCode_1]; 
alter database db_haidilao20141015 add filegroup [ImportCode_2];
alter database db_haidilao20141015 add filegroup [ImportCode_3];
alter database db_haidilao20141015 add filegroup [ImportCode_4];
alter database db_haidilao20141015 add filegroup [ImportCode_5];
go

alter database db_haidilao20141015 add file (name = ImportCode1_data,filename = 'D:MSSQLhaidilao20141015ImportCode1_data.ndf',size = 3MB) to filegroup [ImportCode_1];
alter database db_haidilao20141015 add file (name = ImportCode2_data,filename = 'D:MSSQLhaidilao20141015ImportCode2_data.ndf',size = 3MB) to filegroup [ImportCode_2];
alter database db_haidilao20141015 add file (name = ImportCode3_data,filename = 'D:MSSQLhaidilao20141015ImportCode3_data.ndf',size = 3MB) to filegroup [ImportCode_3]; 
alter database db_haidilao20141015 add file (name = ImportCode4_data,filename = 'D:MSSQLhaidilao20141015ImportCode4_data.ndf',size = 3MB) to filegroup [ImportCode_4]; 
alter database db_haidilao20141015 add file (name = ImportCode5_data,filename = 'D:MSSQLhaidilao20141015ImportCode5_data.ndf',size = 3MB) to filegroup [ImportCode_5];
go

/*第三步:创建分区方案并关联到分区函数*/
Create partition scheme Part_func_ImportCode_scheme as partition Part_func_ImportCode to ([ImportCode_1],[ImportCode_2],[ImportCode_3],[ImportCode_4],[ImportCode_5]); 
--CREATE PARTITION SCHEME [Part_func_Bag_scheme] AS PARTITION [Part_func_Bag] TO ([Bag_1], [Bag_2], [Bag_3], [Bag_4], [PRIMARY])
go


/*第四步 重建索引(删除聚集索引以及需要分区字段的索引后重建该类索引,表被按分区值将分配到各文件组。数据在这一步开始转移。)*/ 
EXEC sp_helpindex N'ImportCode' --查看orders中使用的索引 
--删除主键(主键是聚集索引,分区表只能有一个聚集索引,索引要删除主键)
alter table [db_haidilao20141015].[dbo].[ImportCode] drop constraint PK_ImportCode
go

--创建聚集索引并开始迁移数据
create clustered index idx_cl_od on ImportCode(createTime) 
on Part_func_ImportCode_scheme(createTime); 
go

分区ExportCode

/*准备工作:加入时间字段*/
alter table ExportCode add CreateTime datetime default getdate()

update  ExportCode set createtime = b.ScanDate from ExportCode a inner join ExportInfo b  on a.BatchId=b.Id

alter table ExportCode add CreateTime datetime default getdate()

update  ExportCode set createtime = b.ScanDate from ExportCode a inner join ExportInfo b  on a.BatchId=b.Id


/*第一步:创建分区函数*/
Create partition function Part_func_ExportCode(datetime) as range right 
/*正式区间for values(N'01100923909760', N'01100936207030', N'6311001806524',N'92451400060101');*/
for values(N'2015-01-01T00:00:00.000',N'2016-01-01T00:00:00.000',N'2016-07-01T00:00:00.000',N'2017-01-01T00:00:00.000');
--CREATE PARTITION FUNCTION [Part_func_Bag](datetime) AS RANGE RIGHT FOR VALUES (N'2011-01-01T00:00:00.000', N'2012-01-01T00:00:00.000', N'2013-01-01T00:00:00.000', N'2014-01-01T00:00:00.000')

go

/*第二步:创建文件组和文件*/
alter database db_sishuang2test20170105 add filegroup [ExportCode_1]; 
alter database db_sishuang2test20170105 add filegroup [ExportCode_2];
alter database db_sishuang2test20170105 add filegroup [ExportCode_3];
alter database db_sishuang2test20170105 add filegroup [ExportCode_4];
alter database db_sishuang2test20170105 add filegroup [ExportCode_5];
go

alter database db_sishuang2test20170105 add file (name = ExportCode1_data,filename = 'D:MSSQLsishuang2test20170105ExportCode1_data.ndf',size = 3MB) to filegroup [ExportCode_1];
alter database db_sishuang2test20170105 add file (name = ExportCode2_data,filename = 'D:MSSQLsishuang2test20170105ExportCode2_data.ndf',size = 3MB) to filegroup [ExportCode_2];
alter database db_sishuang2test20170105 add file (name = ExportCode3_data,filename = 'D:MSSQLsishuang2test20170105ExportCode3_data.ndf',size = 3MB) to filegroup [ExportCode_3]; 
alter database db_sishuang2test20170105 add file (name = ExportCode4_data,filename = 'D:MSSQLsishuang2test20170105ExportCode4_data.ndf',size = 3MB) to filegroup [ExportCode_4]; 
alter database db_sishuang2test20170105 add file (name = ExportCode5_data,filename = 'D:MSSQLsishuang2test20170105ExportCode5_data.ndf',size = 3MB) to filegroup [ExportCode_5];
go

/*第三步:创建分区方案并关联到分区函数*/
Create partition scheme Part_func_ExportCode_scheme as partition Part_func_ExportCode to ([ExportCode_1],[ExportCode_2],[ExportCode_3],[ExportCode_4],[ExportCode_5]); 
--CREATE PARTITION SCHEME [Part_func_Bag_scheme] AS PARTITION [Part_func_Bag] TO ([Bag_1], [Bag_2], [Bag_3], [Bag_4], [PRIMARY])
go


/*第四步 重建索引(删除聚集索引以及需要分区字段的索引后重建该类索引,表被按分区值将分配到各文件组。数据在这一步开始转移。)*/ 
EXEC sp_helpindex N'ExportCode' --查看orders中使用的索引 
--删除主键(主键是聚集索引,分区表只能有一个聚集索引,索引要删除主键)
alter table [db_sishuang2test20170105].[dbo].[ExportCode] drop constraint PK_ExportCode
go

--创建聚集索引并开始迁移数据
create clustered index idx_cl_od on ExportCode(createTime) 
on Part_func_ExportCode_scheme(createTime); 
go

分区CodeProduct

/*准备工作:加入时间字段*/

alter table CodeProduct add CreateTime datetime default getdate()

update  CodeProduct set createtime = b.createtime from CodeProduct a inner join ExportCode b  on a.FlowCode=b.FlowCode


/*第一步:创建分区函数*/
Create partition function Part_func_CodeProduct(datetime) as range right 
/*正式区间for values(N'01100923909760', N'01100936207030', N'6311001806524',N'92451400060101');*/
for values(N'2014-01-01T00:00:00.000',N'2015-01-01T00:00:00.000',N'2016-01-01T00:00:00.000',N'2017-01-01T00:00:00.000');
--CREATE PARTITION FUNCTION [Part_func_Bag](datetime) AS RANGE RIGHT FOR VALUES (N'2011-01-01T00:00:00.000', N'2012-01-01T00:00:00.000', N'2013-01-01T00:00:00.000', N'2014-01-01T00:00:00.000')

go

/*第二步:创建文件组和文件*/
alter database db_sishuang2test20170104 add filegroup [CodeProduct_1]; 
alter database db_sishuang2test20170104 add filegroup [CodeProduct_2];
alter database db_sishuang2test20170104 add filegroup [CodeProduct_3];
alter database db_sishuang2test20170104 add filegroup [CodeProduct_4];
alter database db_sishuang2test20170104 add filegroup [CodeProduct_5];
go

alter database db_sishuang2test20170104 add file (name = CodeProduct1_data,filename = 'D:MSSQLsishuang2test20170104CodeProduct1_data.ndf',size = 3MB) to filegroup [CodeProduct_1];
alter database db_sishuang2test20170104 add file (name = CodeProduct2_data,filename = 'D:MSSQLsishuang2test20170104CodeProduct2_data.ndf',size = 3MB) to filegroup [CodeProduct_2];
alter database db_sishuang2test20170104 add file (name = CodeProduct3_data,filename = 'D:MSSQLsishuang2test20170104CodeProduct3_data.ndf',size = 3MB) to filegroup [CodeProduct_3]; 
alter database db_sishuang2test20170104 add file (name = CodeProduct4_data,filename = 'D:MSSQLsishuang2test20170104CodeProduct4_data.ndf',size = 3MB) to filegroup [CodeProduct_4]; 
alter database db_sishuang2test20170104 add file (name = CodeProduct5_data,filename = 'D:MSSQLsishuang2test20170104CodeProduct5_data.ndf',size = 3MB) to filegroup [CodeProduct_5];
go

/*第三步:创建分区方案并关联到分区函数*/
Create partition scheme Part_func_CodeProduct_scheme as partition Part_func_CodeProduct to ([CodeProduct_1],[CodeProduct_2],[CodeProduct_3],[CodeProduct_4],[CodeProduct_5]); 
--CREATE PARTITION SCHEME [Part_func_Bag_scheme] AS PARTITION [Part_func_Bag] TO ([Bag_1], [Bag_2], [Bag_3], [Bag_4], [PRIMARY])
go


/*第四步 重建索引(删除聚集索引以及需要分区字段的索引后重建该类索引,表被按分区值将分配到各文件组。数据在这一步开始转移。)*/ 
EXEC sp_helpindex N'CodeProduct' --查看orders中使用的索引 
--删除主键(主键是聚集索引,分区表只能有一个聚集索引,索引要删除主键)
alter table [db_sishuang2test20170104].[dbo].[CodeProduct] drop constraint PK_CodeProduct
go

--创建聚集索引并开始迁移数据
create clustered index idx_cl_od on CodeProduct(createTime) 
on Part_func_CodeProduct_scheme(createTime); 
go
原文地址:https://www.cnblogs.com/zhuawang/p/6249449.html