可伸缩性性是数据库管理系统的一个很重要的方面,在SQL Server 2005中可伸缩性方面提供了表分区功能。
其实对于有关系弄数据库产品来说,对表、数据库和服务器进行数据分区的从而提供大数据量的支持并不是什么新鲜事,但 SQL Server 2005 提供了一个新的体系结构功能,用于对数据库中的文件组进行表分区。水平分区可根据分区架构,将一个表划分为几个较小的分组。表分区功能是针对超大型数据库(从数百吉字节到数千吉字节或更大)而设计的。超大型数据库 (VLDB) 查询性能通过分区得到了改善。通过对广大分区列值进行分区,可以对数据的子集进行管理,并将其快速、高效地重新分配给其他表。
设想一个大致的电子交易网站,有一个表存储了此网站的历史交易数据,这此数据量可能有上亿条,在以前的SQL Server版本中存储在一个表中不管对于查询性能还是维护都是件麻烦事,下面我们来看一下在SQL Server2005怎么提高性能和可管理性:
-- 创建要使用的测试数据库,Demo
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
USE [master]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'DEMO')
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DROP DATABASE [DEMO]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE DATABASE [DEMO]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--由于表分区使用使用新的体系结构,使用文件组来进行表分区,所以我们创建将要用到的6个文件组,来存储6个时间段的交易数据[<2000],[ 2001], [2002], [2003], [2004], [>2005]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILEGROUP YEARFG1;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILEGROUP YEARFG2;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILEGROUP YEARFG3;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILEGROUP YEARFG4;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILEGROUP YEARFG5;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILEGROUP YEARFG6;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-- 下面为这些文件组添加文件来进行物理的数据存储
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF1', FILENAME = 'C:\ADVWORKSF1.NDF') TO FILEGROUP YEARFG1;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF2', FILENAME = 'C:\ADVWORKSF2.NDF') TO FILEGROUP YEARFG2;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF3', FILENAME = 'C:\ADVWORKSF3.NDF') TO FILEGROUP YEARFG3;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF4', FILENAME = 'C:\ADVWORKSF4.NDF') TO FILEGROUP YEARFG4;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF5', FILENAME = 'C:\ADVWORKSF5.NDF') TO FILEGROUP YEARFG5;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER DATABASE Demo ADD FILE (NAME = 'YEARF6', FILENAME = 'C:\ADVWORKSF6.NDF') TO FILEGROUP YEARFG6;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-- HERE WE ASSOCIATE THE PARTITION FUNCTION TO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-- THE CREATED FILEGROUP VIA A PARTITIONING SCHEME
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
USE DEMO;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-- 创建分区函数
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE PARTITION FUNCTION YEARPF(datetime)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AS
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
RANGE LEFT FOR VALUES ('01/01/2000'
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
,'01/01/2001'
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
,'01/01/2002'
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
,'01/01/2003'
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
,'01/01/2004')
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-- 创建分区架构
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE PARTITION SCHEME YEARPS
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AS PARTITION YEARPF TO (YEARFG1, YEARFG2,YEARFG3,YEARFG4,YEARFG5,YEARFG6)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-- 创建使用此Schema的表
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE TABLE PARTITIONEDORDERS
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ID INT NOT NULL IDENTITY(1,1),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DUEDATE DATETIME NOT NULL,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
) ON YEARPS(DUEDATE)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--为此表填充数据
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @DT datetime
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT @DT = '1999-01-01'
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--start looping, stop at ending date
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
WHILE (@DT <= '2005-12-21')
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
INSERT INTO PARTITIONEDORDERS VALUES(@DT)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @DT=dateadd(yy,1,@DT)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-- 现在我们可以看一下我们刚才插入的行都分布在哪个Partition
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT *, $PARTITION.YEARPF(DUEDATE) FROM PARTITIONEDORDERS
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--我们可以看一下我们现在PARTITIONEDORDERS表的数据存储在哪此partition中,以及在这些分区中数据量的分布
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT * FROM SYS.PARTITIONS WHERE OBJECT_ID = OBJECT_ID('PARTITIONEDORDERS')
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--现在我们设想一下,如果我们随着时间的流逝,现在已经到了2005年,按照我们先前的设定,我们想再想入一个分区,这时是不是重新创建表分区架构然后重新把数据导放到新的分区架构呢,答案是完全不用。下面我们就看如果新加一个分区。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--更改分区架构定义语言,让下一个分区使用和现在已经存在的分区YEARFG6分区中,这样此分区就存储了两段partition的数据。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER PARTITION SCHEME YEARPS
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
NEXT USED YEARFG6;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--更改分区函数
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ALTER PARTITION FUNCTION YEARPF()
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPLIT RANGE ('01/01/2005')
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--现在我们可以看一下我们刚才插入的行都分布在哪个Partition?
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT *, $PARTITION.YEARPF(DUEDATE) FROM PARTITIONEDORDERS
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
--我们可以看一下我们现在PARTITIONEDORDERS表的数据存储在哪此partition中,以及在这些分区中数据量的分布
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT * FROM SYS.PARTITIONS WHERE OBJECT_ID = OBJECT_ID('PARTITIONEDORDERS')
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)