SQL server计划任务分区并按照年月作为文件目录实现分类管理

    近期需要对数据量较大的表进行数据分区,但是若按照150W条数据进行分区,需要三天创建一个ndf文件,一年120个文件存放在一个文件夹中不方便进行文件管理。经过一下午的查阅资料,将以下两篇文章所提到的内容进行综合利用,实现按照年份和月份进行分类存放的SQL定时数据分区。

使用过程中碰到以下问题:

1.dir 命令当路径中存在空格‘ ’时会报错,若路径存在空格时用双引号""将路径包含起来即可   

2.改作业失败.无法确定所有者是否有服务器访问权限.               将所有者改为sa即可

本文主要参考:

http://www.cnblogs.com/linjt0416/articles/5666235.html           SQL计划实现自动数据分区

http://www.cnblogs.com/linjt0416/articles/5667111.html           SQL查找路径是否存在,不存在则创建

 

 

 

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

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

varchar(250), @newDay varchar(50), @oldDay datetime, @partFunName varchar(50), @schemeName varchar(50)
set @tableName='DC_WATER_GZ'
set @newDay=CONVERT(varchar(100), GETDATE(), 23)--23:按天 114:按时间
set @oldDay=cast(CONVERT(varchar(10),dateadd(day,-7,getdate()), 120 ) as datetime)
set @newNameStr=Replace(Replace(@newDay,':','_'),'-','_')
set @fileGroupName=N'G'+@newNameStr+'_His'
set @ndfName=N'F'+@newNameStr+'_His'
set @fullPath=N'D:\MSSQL\DATA\'+@ndfName+'.ndf'

--此处该为自己的数据文件路径,lui注释2015-5-4(右击服务器-属性-数据库设置可看到)
set @partFunName=N'pf_TimeHis'
set @schemeName=N'ps_TimeHis'
--创建文件组
if exists(select * from sys.filegroups where name=@fileGroupName)
begin
print '文件组存在,不需添加'
end
else
begin
exec('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')
print '新增文件组'
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_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) )
begin
exec('alter partition function '+@partFunName+'() split range('''+@newDay+''')')
print '修改分区函数'
end
/*--------------------以上创建数据库的文件组和物理文件------------------------*/
--分区函数
if exists(select * from sys.partition_functions where name =@partFunName)
begin
print '此处修改需要在修改分区函数之前执行'
end
else
begin
exec('CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR 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 where function_id=65536
--查询分区方案
select * from sys.partition_schemes
--查询表数据在哪个分区中存储,where条件查询第一个分区中存在的数据
select *,$partition.pf_SaveTime(分区字段) as Patition from 表名 where $partition.pf_SaveTime(分区字段)=1
*/

GO

 

  

记下方便日后直接查看。

在实际生产环境中运行发生无法正常创建分区的错误,发现可能是语句的执行顺序导致,对SQL进行修改,观察后续执行情况

posted @ 2016-07-13 17:04  狂暴小蛋蛋  阅读(531)  评论(0)    收藏  举报