/*

    Author: kinwar

    Create Date: 2014-09-03

    Description: 积分贴纸数据表 普通表 转为分区表

                 目地:将数据分散存储在不同磁盘,提高并行性

   

*/

 

--数据表结构,当前为普通表

USE [Credit]

GO

 

CREATE TABLE [dbo].[Scanjob](

    [pid] [nvarchar](50) NOT NULL,

    [test] [nvarchar](10) NOT NULL,

    [yzm] [nvarchar](50) NOT NULL,

    [scandate] [varchar](20) NULL,

    [model] [nvarchar](20) NULL,

    [chbh] [nvarchar](10) NOT NULL,

    [chbhver] [tinyint] NULL,

    [sn] [int]NULL,

    [check] [nchar](1) NULL,

    [boxno] [nchar](10) NOT NULL,

    [barcodevalue] [int] NOT NULL,

    [FactoryCode] [nchar](1) NOT NULL

)

 

/* 

--创建分区表示例

CREATE TABLEPartitionTable

(

id INT,

SalesDate DATE

) ONSchemaForPartition(SalesDate)--SalesDate为指定划分区列

 

--查看创建的分区表

SELECT * FROMSYS.partitions WHERE OBJECT_ID=OBJECT_ID('PartitionTable')

 

*/

 

--增加主键和索引

USE [Credit]

GO

ALTER TABLE [dbo].[Scanjob] ADD  CONSTRAINT [PK_Scanjob] PRIMARY KEY CLUSTERED

(

    [pid] ASC,

    [chbh] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

GO

 

CREATE NONCLUSTERED INDEX [IX_Scanjob] ON [dbo].[Scanjob]

(

    [test] ASC,

    [yzm] ASC

)

INCLUDE( [scandate])  ON [PRIMARY]

GO

 

 

--增加文件组和文件,可以分别保存在不同磁盘,提高并行性

USE Credit

GO

 

ALTER DATABASE [Credit] ADD FILEGROUP [CreditFG1]

GO

ALTER DATABASE [Credit] ADD FILE ( NAME = N'CreditFG1', FILENAME = N'D:\Program Files\Microsoft SQLServer\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FG1.ndf' , SIZE = 30720KB , FILEGROWTH = 1024KB ) TO FILEGROUP [CreditFG1]

GO

ALTER DATABASE [Credit] ADD FILEGROUP [CreditFG2]

GO

ALTER DATABASE [Credit] ADD FILE ( NAME = N'CreditFG2', FILENAME = N'D:\Program Files\Microsoft SQLServer\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\FG2.ndf' , SIZE = 30720KB , FILEGROWTH = 1024KB ) TO FILEGROUP [CreditFG2]

GO

ALTER DATABASE [Credit] ADD FILEGROUP [CreditFG3]

GO

ALTER DATABASE [Credit] ADD FILE ( NAME = N'CreditFG3', FILENAME = N'F:\Database\FileGroup\1FG3.ndf' , SIZE = 30720KB , FILEGROWTH = 1024KB ) TO FILEGROUP [CreditFG3]

GO

 

--删除主键和索引

USE Credit

GO

 

ALTER TABLE dbo.scanjob DROP CONSTRAINT PK_Scanjob

GO

DROP INDEX IX_Scanjob ON dbo.scanjob

GO

 

USE [Credit]

GO

--创建分区函数

CREATE PARTITION FUNCTION fnPartition(varchar(20))   --这个列应该为Datetime

AS RANGE RIGHT              --LEFT是左边包含分割点,RIGHT是右边包含

FOR VALUES('2012-01-01 00:00:00','2013-01-01 00:00:00','2014-01-01 00:00:00')

GO

--查看分区函数是否创建成功

SELECT * FROM sys.partition_functions

 

USE [Credit]

GO

--创建分区架构

CREATE PARTITION SCHEME SchemaForPartition

AS PARTITION fnPartition

TO ([primary], CreditFG1, CreditFG2, CreditFG3)--因为有个边界值,则有个分区,需要指定个文件组,也可以使用ALL指向一个文件组

GO

 

--查看已创建的分区架构schema

SELECT * FROM sys.partition_schemes

GO

 

 

--创建主键,但不设为聚集索引

ALTER TABLE dbo.Scanjob ADD CONSTRAINT PK_ScanjobPRIMARY KEY NONCLUSTERED(

    [pid] ASC,

    [chbh] ASC

)

ON [PRIMARY]

GO

 

--创建一个新的聚集索引,在该聚集索引中使用分区方案

CREATE CLUSTERED INDEX IX_Scanjob ON dbo.Scanjob(scandate)

ON SchemaForPartition(scandate)      --SchemaForPartition为分区方案

GO

 

--统计所有分区表中的记录总数(3个分区?)

SELECT $PARTITION.fnPartition(scandate) AS 分区编号,count(PID) AS 记录数

FROM dbo.Scanjob

GROUP BY $PARTITION.fnPartition(scandate)

 

--查询号分区的记录笔数

SELECT * FROM dbo.Scanjob  WHERE $PARTITION.fnPartition(scandate)=2

 

--查询2012-01-0100:00:00 这条记录所在的分区号

SELECT $PARTITION.fnPartition('2012-01-01 00:00:00') AS PartitionNum 

 

/*

分区表建立OK

合并和新增分区

*/

 

--合并分区,提供分割点,执行语句后再次查询分区编号与记录数

ALTER PARTITION FUNCTION fnPartition()

MERGE RANGE('2014-01-01 00:00:00')

 

--拆分分区,首先加入文件组,这里省略,使用上面的CreditFG3 文件组

/*

ALTER DATABASE[Credit] ADD FILEGROUP [CreditFG3]

GO

ALTER DATABASE[Credit] ADD FILE ( NAME = N'CreditFG3', FILENAME =N'F:\Database\FileGroup\1FG3.ndf' , SIZE = 30720KB , FILEGROWTH = 1024KB ) TOFILEGROUP [CreditFG3]

GO

*/

--指定分割后多出来的分区应该存储在哪个文件组

ALTER PARTITION SCHEME SchemaForPartitionNEXT USED 'CreditFG4'

--添加分割点

ALTER PARTITION FUNCTION fnPartition()

SPLIT RANGE('2014-01-01 00:00:00')

 

--切换分区

--Scanjob_month5没有分区,将数据全部插入Scanjob的分区中,表结构要一致

ALTER TABLE dbo.Scanjob_month5 SWITCHTO dbo.Scanjob PARTITION 3

 

--将分区表的数据切换到普通表

--特别注意数据表所有字段、索引、约束都应该一致

--普通表建立聚集索引时(名称一致),必须指定在所要切换的分区,本例为ON CreditFG3

--如果主键不是聚集索引,可能需要删除掉主键,不然会出现错误(本例删除掉主键,只保留聚集索引)

ALTER TABLE dbo.Scanjob SWITCH PARTITION 3 TO dbo.Scanjob_month5

 

 

/**************************************************************/

--将分区表转为普通表

--合并所有的分区分界(只剩一个分区编号),此时其实已是普通表了,但系统标记为分区表

--删除分区索引,并在原来索引字段上重建一个索引

/*

ALTERPARTITION FUNCTION fnPartition()

MERGERANGE('2012-01-01 00:00:00')

ALTERPARTITION FUNCTION fnPartition()

MERGERANGE('2013-01-01 00:00:00')

ALTERPARTITION FUNCTION fnPartition()

MERGERANGE('2014-01-01 00:00:00')

 

DROP INDEXdbo.Scanjob.IX_Scanjob

CREATECLUSTERED INDEX PK_scandate ON dbo.Scanjob(scandate) ON [PRIMARY]

 

*/