sql server2008怎么建分区

sql server2008怎么建分区,第1张

打开MsSQL2008,找到作业该项,如果打不开或者SQL Server代理是未启动状态,请先在windows服务中启动SQL Server代理(参考图片),

2

右击MsSQL2008对象资源管理器中的作业,选择新建作业,输入该作业你想用的名称,类别不用管,说明里面是输入一些该作业完成的功能,可不写,请务必勾选已启用复选框.

3

点击新建作业窗体左侧的步骤项,点击右侧区域下方的新建按钮,输入步骤名称,类型请选择Transact-SQL脚本(T-SQL),运行身份默认,数据库请选择要进行分区的数据库,请不要选择master默认的,命令文本框中输入如下代码:

/*--------------------创建数据库的文件组和物理文件------------------------*/

declare @tableName varchar(50), @fileGroupName varchar(50), @ndfName varchar(50), @newNameStr varchar(50), @fullPath

varchar(50), @newDay varchar(50), @oldDay datetime, @partFunName varchar(50), @schemeName varchar(50)

set @tableName='要分区的数据库名称'

set @newDay=CONVERT(varchar(100), GETDATE(), 23)--23:按天 114:按时间

set @oldDay=cast(CONVERT(varchar(10),dateadd(day,-1,getdate()), 120 ) as datetime)

set @newNameStr=Replace(Replace(@newDay,':','_'),'-','_')

set @fileGroupName=N'G'+@newNameStr

set @ndfName=N'F'+@newNameStr+''

set @fullPath=N'E:\\SQLDataBase\\UserData\\'+@ndfName+'.ndf'

set @partFunName=N'pf_Time'

set @schemeName=N'ps_Time'

--创建文件组

if exists(select * from sys.filegroups where name=@fileGroupName)

begin

print '文件组存在,不需添加'

end

else

begin

exec('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')

print '新增文件组'

if exists(select * from sys.partition_schemes where name =@schemeName)

begin

exec('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')

print '修改分区方案'

end

if exists(select * from sys.partition_range_values where function_id=(select function_id from

sys.partition_functions where name =@partFunName) and value=@oldDay)

begin

exec('alter partition function '+@partFunName+'() split range('''+@newDay+''')')

print '修改分区函数'

end

end

--创建NDF文件

if exists(select * from sys.database_files where [state]=0 and (name=@ndfName or physical_name=@fullPath))

begin

print 'ndf文件存在,不需添加'

end

else

begin

exec('ALTER DATABASE '+@tableName+'ADD FILE (NAME ='+@ndfName+',

FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']')

print '新创建ndf文件'

end

/*--------------------以上创建数据库的文件组和物理文件------------------------*/

--分区函数

if exists(select * from sys.partition_functions where name =@partFunName)

begin

print '此处修改需要在修改分区函数之前执行'

end

else

begin

exec('CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHTFOR VALUES ('''+@newDay

+''')')

print '新创建分区函数'

end

--分区方案

if exists(select * from sys.partition_schemes where name =@schemeName)

begin

print '此处修改需要在修改分区方案之前执行'

end

else

begin

exec('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO

(''PRIMARY'','''+@fileGroupName+''')')

print '新创建分区方案'

end

print '---------------以下是变量定义值显示---------------------'

print '当前数据库:'+@tableName

print '当前日期:'+@newDay+'(用作随机生成的各种名称和分区界限)'

print '合法命名方式:'+@newNameStr

print '文件组名称:'+@fileGroupName

print 'ndf物理文件名称:'+@ndfName

print '物理文件完整路径:'+@fullPath

print '分区函数:'+@partFunName

print '分区方案:'+@schemeName

/*

--查看创建的分区函数

select * from sys.partition_functions

--查看分区函数的临界值

select * from sys.partition_range_values

--查询分区方案

select * from sys.partition_schemes

--查询表数据在哪个分区中存储,where条件查询第一个分区中存在的数据

select *,$partition.pf_SaveTime(分区字段) as Patition from 表名 where $partition.pf_SaveTime(分区字段)=1

*/

GO

点击确定按钮

上述代码中的变量名称,路径等均可自行修改,上述是按天为单位,以G开头的日期作为文件组名称,以F开头的日期作为物理分区文件名即ndf文件名称

4

选择新建分区左侧的计划项,然后点击右侧区域下方的新建按钮,设定新建分区的时间间隔,图中设置的是每天创建一个新的分区,用户也可以自行修改,按月,按周,按自定义时间等

其他的条目,通知,警报,目标可自行设置,也可不设置,至此自动创建分区的计划任务已成功设置.

END

步骤二:对表应用分区方案和分区函数

右击要分区的表,选择存储菜单下的创建分区,上述步骤一中创建的分区函数是按datetime类型进行的分区,所以创建分区的时候需要选择相应类型的字段作为分区依据,用户也可以根据int型或其他类型的字段进行分区,选择下一步,使用现有分区函数下一步使用现有分区方案,下一步会自动按照分区方案执行的日期进行分区,继续点击下一步选择立即执行,完成后即可完成的整体的表分区自动执行.

需注意:刚设置完第一步的计划任务,可能不会执行第一步的分区方案的代码,也就意味着没有创建分区函数和分区方案,第二步设置的时候使用现有分区函数和使用现有分区方案也就不可用,可先把第一步的代码执行一遍即可.

/*第一步:创建分区函数*/

Create partition function Part_func_Bag(varchar(20)) as range right

/*正式区间

for values(N'01100923909760', N'01100936207030', N'6311001806524',N'92451400060101')*/for values(N'91701311710807', N'07201280707101', N'8011011089884',N'80241001430714')goselect * from Bag where BagCode in(N'91701311710807', N'07201280707101', N'8011011089884',N'80241001430714')

/*第二步:创建文件组和文件*/

alter database ZXAutoCode add filegroup [Bag_1]alter database ZXAutoCode add filegroup [Bag_2]alter database ZXAutoCode add filegroup [Bag_3]alter database ZXAutoCode add filegroup [Bag_4]goalter database ZXAutoCode add file (name = Bag1_data,filename = 'E:\MSSQL\TESTDATA\Bag1_data.ndf',size = 3MB) to filegroup [Bag_1]alter database ZXAutoCode add file (name = Bag2_data,filename = 'E:\MSSQL\TESTDATA\Bag2_data.ndf',size = 3MB) to filegroup [Bag_2]alter database ZXAutoCode add file (name = Bag3_data,filename = 'E:\MSSQL\TESTDATA\Bag3_data.ndf',size = 3MB) to filegroup [Bag_3]alter database ZXAutoCode add file (name = Bag4_data,filename = 'E:\MSSQL\TESTDATA\Bag4_data.ndf',size = 3MB) to filegroup [Bag_4]go

/*第三步:创建分区方案并关联到分区函数*/

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'Bag' --查看orders中使用的索引 drop index idx_cl_od on Baggocreate clustered index idx_cl_od on Bag(bagcode) on Part_func_Bag_scheme(bagcode)go


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6697841.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存