SQL Server 2008 Partition 测试

--创建FileGroup

ALTER DATABASE AdventureWorksADDFILEGROUP [2003Q3]

ALTER DATABASE AdventureWorksADDFILEGROUP [2000Q4]

ALTER DATABASE AdventureWorksADDFILEGROUP [2001Q1]

ALTER DATABASE AdventureWorksADDFILEGROUP [2001Q2]

ALTER DATABASE AdventureWorksADDFILEGROUP [2002Q1]

 

--创建数据文件

alter database  AdventureWorks

ADD FILE

       (NAME = '2003Q3',

        FILENAME=

         'D:\MSSQL\2003Q3.ndf',

        SIZE =5MB,

        MAXSIZE=500,

        FILEGROWTH=1)

TO FILEGROUP[2003Q3]

Go

 

alter database  AdventureWorks

ADD FILE

       (NAME = '2000Q4',

        FILENAME=

         'D:\MSSQL\2000Q4.ndf',

        SIZE =5MB,

        MAXSIZE=500,

        FILEGROWTH=1)

TO FILEGROUP[2000Q4]

Go

 

alter database  AdventureWorks

ADD FILE

       (NAME = '2001Q1',

        FILENAME=

         'D:\MSSQL\2001Q1.ndf',

        SIZE =5MB,

        MAXSIZE=500,

        FILEGROWTH=1)

TO FILEGROUP[2001Q1]

Go

 

alter database  AdventureWorks

ADD FILE

       (NAME = '2001Q2',

        FILENAME=

         'D:\MSSQL\2001Q2.ndf',

        SIZE =5MB,

        MAXSIZE=500,

        FILEGROWTH=1)

TO FILEGROUP[2001Q2]

Go

 

alter database  AdventureWorks

ADD FILE

       (NAME = '2002Q1',

        FILENAME=

         'D:\MSSQL\2002Q1.ndf',

        SIZE =5MB,

        MAXSIZE=500,

        FILEGROWTH=1)

TO FILEGROUP[2002Q1]

Go

 

--创建分区函数(确定分区界限)

CREATE PARTITIONFUNCTIONOrderDateRangePFN(datetime)

AS

RANGE LEFT FOR VALUES ('20000930 23:59:59.997',

            '2000123123:59:59.997',

            '2001033123:59:59.997',

            '2001063023:59:59.997')

           

---将分区界限绑定到文件组

CREATE PARTITION SCHEME OrderDatePScheme

AS

PARTITIONOrderDateRangePFN

TO ([2003Q3], [2000Q4], [2001Q1], [2001Q2], [PRIMARY])

 

--创建分区表

CREATE TABLE [dbo].[OrdersRange] 

(

   [PurchaseOrderID] [int] NOT NULL,

   [EmployeeID] [int] NULL,

   [VendorID] [int] NULL,

   [TaxAmt] [money] NULL,

   [Freight] [money] NULL,

   [SubTotal] [money] NULL,

   [Status] [tinyint] NOTNULL,

   [RevisionNumber] [tinyint] NULL ,

   [ModifiedDate] [datetime] NULL ,

   [ShipMethodID] [tinyint] NULL,

   [ShipDate] [datetime] NOT NULL,

   [OrderDate] [datetime] NOT NULL

      CONSTRAINTOrdersRangeYear

         CHECK ([OrderDate]>='20030701'

                         AND [OrderDate] <='20040630 11:59:59.997'),

   [TotalDue] [money] NULL

)

ONOrderDatePScheme(OrderDate)

GO

  

--查询当前时间应该分布到哪个分区

select $PARTITION.OrderDateRangePFN('20010530 23:59:59.997')

 

---Split分区

ALTER PARTITION SCHEME OrderDatePScheme

NEXT USED[2002Q1]

 

Alter partitionfunctionOrderDateRangePFN() splitrange('20010430 23:59:59.997')

 

--查看当前分区数量(6)

select * from sys.partitionswhereOBJECT_ID=object_id('OrdersRange')

 

---Merge 分区

Alter partitionfunctionOrderDateRangePFN()mergerange('20010430 23:59:59.997')

 

--查看当前分区数量(5)

select * from sys.partitionswhereOBJECT_ID=object_id('OrdersRange')

 

--Partition分区转出到dbo.Sales

 ALTER TABLE[OrdersRange] SWITCHPARTITION 1TO dbo.Sales

 

使用Partition的好处:

1.提高查询性能(对分区字段做筛选)/并行计划。

2.管理优化比如FileGroup 备份/单个Partition维护索引/Filegroup还原

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值