--查询一张表的分区
select $partition.Part_func_UserLog(createdDate) as Patition from dbo.UserLog GROUP BY $partition.Part_func_UserLog(createdDate)
--创建分区的时间段
CREATE PARTITION FUNCTION Part_func_UserLog(datetime) AS RANGE LEFT FOR VALUES ( ‘20120709 23:59:59.997‘, ‘20120710 23:59:59.997‘, ‘20120711 23:59:59.997‘, ‘20120712 23:59:59.997‘, ‘20120713 23:59:59.997‘, ‘20120714 23:59:59.997‘, ‘20120715 23:59:59.997‘, ‘20120716 23:59:59.997‘, ‘20120717 23:59:59.997‘, ‘20120718 23:59:59.997‘ );
--创建分区分组
ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks1]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks2]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks3]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks4]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks5]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks6]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks7]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks8]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks9]; GO ALTER DATABASE GoodBooks ADD FILEGROUP [FGGoodBooks10];
--创建分区文件
ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks1_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks1_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks1]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks2_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks2_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks2]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks3_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks3_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks3]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks4_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks4_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks4]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks5_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks5_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks5]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks6_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks6_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks6]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks7_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks7_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks7]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks8_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks8_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks8]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks9_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks9_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks9]; ALTER DATABASE GoodBooks ADD FILE (NAME=FGGoodBooks10_data,FILENAME=‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FGGoodBooks10_data.ndf‘,SIZE=3MB) TO FILEGROUP [FGGoodBooks10];
--绑定分组
CREATE PARTITION SCHEME Part_func_UserLog_Scheme AS PARTITION Part_func_UserLog TO ([FGGoodBooks1],[FGGoodBooks2],[FGGoodBooks3],[FGGoodBooks4],[FGGoodBooks5],[FGGoodBooks6],[FGGoodBooks7],[FGGoodBooks8],[FGGoodBooks9],[FGGoodBooks10],[Primary]); GO
--给表添加主键
ALTER TABLE dbo.UserLog ADD CONSTRAINT PK_UserLog PRIMARY KEY NONCLUSTERED (UserLogID) WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO CREATE CLUSTERED INDEX IX_UserLog_partitioncol ON dbo.UserLog (UserLogID) WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON Part_func_UserLog_Scheme(CreatedDate) GO
--删除表的主键、聚集索引
alter table UserLog drop CONSTRAINT PK_UserLog
--查询指定分区的数据
select * from dbo.UserLog WHERE $PARTITION.Part_func_UserLog(CreatedDate)=3
--自动添加分区
declare @databaseName varchar(50), @fileGroupName varchar(50), @ndfName varchar(50), @newNameStr varchar(50), @fullPath varchar(500), @newDay varchar(50), @oldDay datetime, @partFunName varchar(50), @schemeName varchar(50) set @databaseName=‘GoodBooks‘ set @newDay=CONVERT(varchar(100), getdate(), 23)--23:按天 114:按时间 set @oldDay=cast(CONVERT(varchar(10),dateadd(day,-5,getdate()), 120 ) as datetime) set @newNameStr=Replace(Replace(@newDay,‘:‘,‘_‘),‘-‘,‘_‘) set @fileGroupName=N‘G‘[email protected] set @ndfName=N‘F‘[email protected]+‘‘ set @fullPath=N‘E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\‘[email protected]+‘.ndf‘ set @partFunName=N‘Part_func_UserLog‘ set @schemeName=N‘Part_func_UserLog_Scheme‘ --创建文件组 if exists(select * from sys.filegroups where [email protected]) begin print ‘文件组存在,不需添加‘ end else begin exec(‘ALTER DATABASE ‘[email protected]+‘ ADD FILEGROUP [‘[email protected]+‘]‘) print ‘新增文件组‘ if exists(select * from sys.partition_schemes where name [email protected]) begin exec(‘alter partition scheme ‘[email protected]+‘ next used [‘[email protected]+‘]‘) print ‘修改分区方案‘ end if exists(select * from sys.partition_range_values where function_id=(select function_id from sys.partition_functions where name [email protected]) and [email protected]) begin exec(‘alter partition function ‘[email protected]+‘() split range(‘‘‘[email protected]+‘‘‘)‘) print ‘修改分区函数‘ end end --创建NDF文件 if exists(select * from sys.database_files where [state]=0 and ([email protected] or [email protected])) begin print ‘ndf文件存在,不需添加‘ end else begin exec(‘ALTER DATABASE ‘[email protected]+‘ ADD FILE (NAME =‘[email protected]+‘,FILENAME = ‘‘‘[email protected]+‘‘‘)TO FILEGROUP [‘[email protected]+‘]‘) PRINT ‘ALTER DATABASE ‘[email protected]+‘ ADD FILE (NAME =‘[email protected]+‘,FILENAME = ‘‘‘[email protected]+‘‘‘)TO FILEGROUP [‘[email protected]+‘]‘ print ‘新创建ndf文件‘ end /*--------------------以上创建数据库的文件组和物理文件------------------------*/ --分区函数 if exists(select * from sys.partition_functions where name [email protected]) begin print ‘此处修改需要在修改分区函数之前执行‘ end else begin exec(‘CREATE PARTITION FUNCTION ‘[email protected]+‘(DateTime)AS RANGE RIGHT FOR VALUES (‘‘‘[email protected]+‘‘‘)‘) print ‘新创建分区函数‘ end --分区方案 if exists(select * from sys.partition_schemes where name [email protected]) begin print ‘此处修改需要在修改分区方案之前执行‘ end else begin exec(‘CREATE PARTITION SCHEME ‘[email protected]+‘ AS PARTITION ‘[email protected]+‘ TO (‘‘PRIMARY‘‘,‘‘‘[email protected]+‘‘‘)‘) print ‘新创建分区方案‘ end print ‘---------------以下是变量定义值显示---------------------‘ print ‘当前数据库:‘[email protected] print ‘当前日期:‘[email protected]+‘(用作随机生成的各种名称和分区界限)‘ print ‘合法命名方式:‘[email protected] print ‘文件组名称:‘[email protected] print ‘ndf物理文件名称:‘[email protected] print ‘物理文件完整路径:‘[email protected] print ‘分区函数:‘[email protected] print ‘分区方案:‘[email protected] /* --查看创建的分区函数 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
时间: 2024-11-25 15:46:55