SQLServer 2005 海量数据解决方案 分区表

该文转载自http://blog.csdn.net/gudesheng/article/details/1963951.
Csdn Blog在2007年,由于访问量和数据量的大幅度增长,使得我们原有的在.text 0.96版本上修改的代码基本不堪重负。

在数据库方面主要表现为,单单文章表,2007年1年的数据已经达到了30G的量(最后的解决方案是对把文章表分为两个表,分别存放文章相关信息和专门存放文章内容的表,然后对两个表作分区)。熟悉.Text得朋友可能知道,有一个专门记录流量的表,页面内的每一次点击,直接记录到数据库里面,反映到页面上就是单个用户的博客流量,分别是EntryID,BlogID,DateTime,表名称叫做blog_viewcount,这个表我们现在已经到了4亿的数据,之前仅仅是优化索引和索引试图来在真实的环境中查看到底sql2005单表能负载多大的量。
最后得出结论,在sql2005中,单表的量最多不能超过200w数据。

而文章表和blog_viewcount这个表,我们现在采用2005得新特性,分区表来实现。事实上,反映还不错。以下写出创建分区表的步鄹(我们用blog_viewcount来做实例):

先根据表数据的量大小,来拆分每一个分区,保证分区之间在200w以下的数据
CREATEPARTITIONFUNCTIONOneYearDateRangePFN(datetime)
ASRANGELEFTFORVALUES
(
'2007013123:59:59.997',
'2007022823:59:59.997',
'2007033123:59:59.997',
'2007043023:59:59.997',
'2007053123:59:59.997',
'2007063023:59:59.997',
'2007073123:59:59.997',
'2007083123:59:59.997',
'2007093023:59:59.997',
'2007103123:59:59.997',
'2007113023:59:59.997',
'2007123123:59:59.997'
)

然后我们需要添加相应的文件组
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200701 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200702 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200703 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200704 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200705 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200706 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200707 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200708 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200709 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200710 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200711 ]
ALTER DATABASE newcsdnblog ADD FILEGROUP [ ViewCount200712 ]


第三步,我们需要在服务器上建立出单独的文件(涉及到的朋友可能知道,我们如果把文件分布在不同的磁盘上,可以最大限度的提高磁头的读写能力;另:这里微软建议数据库服务器最好作Raid0 + Raid1)
ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200701 ' ,FILENAME = N ' f:SmallTableViewCount200701.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200701 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200702 ' ,FILENAME = N ' f:SmallTableViewCount200702.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200702 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200703 ' ,FILENAME = N ' f:SmallTableViewCount200703.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200703 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200704 ' ,FILENAME = N ' f:SmallTableViewCount200704.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200704 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200705 ' ,FILENAME = N ' f:SmallTableViewCount200705.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200705 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200706 ' ,FILENAME = N ' f:SmallTableViewCount200706.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200706 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200707 ' ,FILENAME = N ' f:SmallTableViewCount200707.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200707 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200708 ' ,FILENAME = N ' f:SmallTableViewCount200708.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200708 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200709 ' ,FILENAME = N ' f:SmallTableViewCount200709.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200709 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200710 ' ,FILENAME = N ' f:SmallTableViewCount200710.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200710 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200711 ' ,FILENAME = N ' f:SmallTableViewCount200711.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200711 ]

ALTER DATABASE newcsdnblog
ADD FILE
(NAME
= N ' ViewCount200712 ' ,FILENAME = N ' f:SmallTableViewCount200712.ndf ' ,SIZE = 5MB,MAXSIZE = 100MB,FILEGROWTH = 5MB)
TO FILEGROUP [ ViewCount200712 ]

第四步,创建关联
CREATE PARTITIONSCHEMEOneYearDateRangePScheme
AS
PARTITIONOneYearDateRangePFN
ALL TO ( [ PRIMARY ] )

第五步,创建表和索引(我这里只列出如何创建表)
CREATE TABLE [ dbo ] . [ Blog_ViewCount2 ]
(
[ EntryID ] [ int ] NOT NULL ,
[ BlogID ] [ int ] NOT NULL ,
[ ViewDate ] [ datetime ] NOT NULL
)
ON OneYearDateRangePScheme(ViewDate)
GO

如果大家希望查询此表中的相关数据,可以使用如下语句
select $partition.OneYearDateRangePFN(ViewDate) from [ Blog_ViewCount2 ] a order by a.ViewDate asc

关于增量问题,我会在下一篇文章中,和大家分享。

你可能感兴趣的:(sqlserver)