建立分区表的步骤:
1 创建数据库(必须在多个文件组中)
2 创建分区函数
3 创建分区Scheme
4 创建分区表
USE
master;
GO
IF OBJECT_ID (N ' PartitionDataBase ' ) IS NOT NULL
DROP DATABASE PartitionDataBase;
GO
/* CREATE DATABASE */
CREATE DATABASE PartitionDataBase
ON PRIMARY
(
NAME = N ' File_A_H ' ,FileName = ' D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PartitionDataBase_AH.mdf '
),
FILEGROUP FileGroup_I_N
(
NAME = N ' File_I_N ' ,FileName = ' D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PartitionDataBase_IN.mdf '
),
FILEGROUP FileGorup_M_Z
(
NAME = N ' File_M_Z ' ,FileName = ' D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PartitionDataBase_MZ.mdf '
)
GO
USE PartitionDataBase;
GO
/* 创建分区函数 */
CREATE PARTITION FUNCTION StaffNameRangePFN( varchar ( 100 ))
AS
RANGE LEFT FOR VALUES ( ' H ' , ' M ' )
GO
/* 创建分区Scheme */
CREATE PARTITION SCHEME StaffNamePScheme
AS
PARTITION StaffNameRangePFN
TO ( [ PRIMARY ] , FileGroup_I_N, FileGorup_M_Z)
GO
IF OBJECT_ID (N ' Staff ' ) IS NOT NULL
DROP TABLE dbo.Staff;
/* 创建分区表 */
CREATE TABLE [ dbo ] . [ Staff ]
(
[ StaffName ] [ varchar ] ( 100 ) NOT NULL
)
ON StaffNamePScheme ( [ StaffName ] )
GO
CREATE CLUSTERED INDEX IX_StaffName ON [ Staff ] ( [ StaffName ] )
GO
GO
IF OBJECT_ID (N ' PartitionDataBase ' ) IS NOT NULL
DROP DATABASE PartitionDataBase;
GO
/* CREATE DATABASE */
CREATE DATABASE PartitionDataBase
ON PRIMARY
(
NAME = N ' File_A_H ' ,FileName = ' D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PartitionDataBase_AH.mdf '
),
FILEGROUP FileGroup_I_N
(
NAME = N ' File_I_N ' ,FileName = ' D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PartitionDataBase_IN.mdf '
),
FILEGROUP FileGorup_M_Z
(
NAME = N ' File_M_Z ' ,FileName = ' D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PartitionDataBase_MZ.mdf '
)
GO
USE PartitionDataBase;
GO
/* 创建分区函数 */
CREATE PARTITION FUNCTION StaffNameRangePFN( varchar ( 100 ))
AS
RANGE LEFT FOR VALUES ( ' H ' , ' M ' )
GO
/* 创建分区Scheme */
CREATE PARTITION SCHEME StaffNamePScheme
AS
PARTITION StaffNameRangePFN
TO ( [ PRIMARY ] , FileGroup_I_N, FileGorup_M_Z)
GO
IF OBJECT_ID (N ' Staff ' ) IS NOT NULL
DROP TABLE dbo.Staff;
/* 创建分区表 */
CREATE TABLE [ dbo ] . [ Staff ]
(
[ StaffName ] [ varchar ] ( 100 ) NOT NULL
)
ON StaffNamePScheme ( [ StaffName ] )
GO
CREATE CLUSTERED INDEX IX_StaffName ON [ Staff ] ( [ StaffName ] )
GO
下面的代码将Staff表按StaffName分布到三个分区中。
插入一些测试数据
INSERT
INTO
[
dbo
]
.
[
Staff
]
SELECT FirstName FROM AdventureWorks.Person.Contact
SELECT FirstName FROM AdventureWorks.Person.Contact
运行下面的代码,看看数据的分布情况
SELECT
$partition.StaffNameRangePFN(StaffName) AS [ Partition Number ] ,
MIN (StaffName) AS [ Min StaffName ] ,
MAX (StaffName) AS [ Max StaffName ] ,
COUNT (StaffName) AS [ Rows In Partition ]
FROM dbo.staff AS o
GROUP BY $partition.StaffNameRangePFN(StaffName)
ORDER BY [ Partition Number ]
$partition.StaffNameRangePFN(StaffName) AS [ Partition Number ] ,
MIN (StaffName) AS [ Min StaffName ] ,
MAX (StaffName) AS [ Max StaffName ] ,
COUNT (StaffName) AS [ Rows In Partition ]
FROM dbo.staff AS o
GROUP BY $partition.StaffNameRangePFN(StaffName)
ORDER BY [ Partition Number ]
看起来还不错。三个分区都差不多的数据
我们来打破这个平衡
INSERT
INTO
[
dbo
]
.
[
Staff
]
SELECT AddressLine1 FROM AdventureWorks.Person.Address
SELECT AddressLine1 FROM AdventureWorks.Person.Address
修改分区表的步骤:
1 添加一个文件组到数据库
2 修改分区Scheme
3 修改分区函数
Use
master
GO
ALTER DATABASE PartitionDataBase ADD FILEGROUP FileGroup_0_9
GO
ALTER DATABASE PartitionDataBase
ADD FILE
(
NAME = N ' File_0_9 ' ,FileName = ' D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PartitionDataBase_09.mdf '
)
TO FILEGROUP FileGroup_0_9
GO
Use PartitionDataBase
GO
ALTER PARTITION SCHEME StaffNamePScheme
NEXT USED FileGroup_0_9;
GO
ALTER PARTITION FUNCTION StaffNameRangePFN()
SPLIT RANGE ( ' A ' );
GO
GO
ALTER DATABASE PartitionDataBase ADD FILEGROUP FileGroup_0_9
GO
ALTER DATABASE PartitionDataBase
ADD FILE
(
NAME = N ' File_0_9 ' ,FileName = ' D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\PartitionDataBase_09.mdf '
)
TO FILEGROUP FileGroup_0_9
GO
Use PartitionDataBase
GO
ALTER PARTITION SCHEME StaffNamePScheme
NEXT USED FileGroup_0_9;
GO
ALTER PARTITION FUNCTION StaffNameRangePFN()
SPLIT RANGE ( ' A ' );
GO
请注意修改和创建之间的一些差别
再用上面的代码看看数据分布的情况。