为了提高数据库的性能,我们经常用到各种数据库的索引,从SQL Server2005以后,我们也可以通过定义表的分区来将数据进行分开存储,各种数据的操作可以互不影响,达到提高数据性能的目的。
非聚集索引的存在与否并不影响数据分页的组织,因此每张表上并不像聚集索引那样只局限于拥有一个非聚集索引,SQL Server 2005 每张表能够包含249 个非聚集索引 SQL Server 2008 每张表能够包含999 个非聚集索引 ,但是实际上所用到的比这个数要少的多。
创建数据库分区文件数量,这里存储一年的数据分成十二个分区,需要现在D盘建立好Data 的文件夹 里面包含Primary 文件夹和 FG1 FG2 FG3 FG4............
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'AirAvCache')
DROP DATABASE [AirAvCache]
GO
CREATE DATABASE [AirAvCache]
ON PRIMARY
(NAME='Data Partition DB Primary FG',
FILENAME=
'D:\Data\Primary\AirAvCache Primary FG.mdf',
SIZE=5,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG1]
(NAME = 'AirAvCache FG1',
FILENAME =
'D:\Data\FG1\AirAvCache FG1.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG2]
(NAME = 'AirAvCache FG2',
FILENAME =
'D:\Data\FG2\AirAvCache FG2.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG3]
(NAME = 'AirAvCache FG3',
FILENAME =
'D:\Data\FG3\AirAvCache FG3.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG4]
(NAME = 'AirAvCache FG4',
FILENAME =
'D:\Data\FG4\AirAvCache FG4.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG5]
(NAME = 'AirAvCache FG5',
FILENAME =
'D:\Data\FG5\AirAvCache FG5.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG6]
(NAME = 'AirAvCache FG6',
FILENAME =
'D:\Data\FG6\AirAvCache FG6.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG7]
(NAME = 'AirAvCache FG7',
FILENAME =
'D:\Data\FG7\AirAvCache FG7.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG8]
(NAME = 'AirAvCache FG8',
FILENAME =
'D:\Data\FG8\AirAvCache FG8.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG9]
(NAME = 'AirAvCache FG9',
FILENAME =
'D:\Data\FG9\AirAvCache FG9.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG10]
(NAME = 'AirAvCache FG10',
FILENAME =
'D:\Data\FG10\AirAvCache FG10.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG11]
(NAME = 'AirAvCache FG11',
FILENAME =
'D:\Data\FG11\AirAvCache FG11.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [AirAvCache FG12]
(NAME = 'AirAvCache FG12',
FILENAME =
'D:\Data\FG12\AirAvCache FG12.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 )
USE AirAvCache
GO
-- 创建函数
CREATE PARTITION FUNCTION [AirAvCache Partition Range](DATETIME)
AS RANGE LEFT FOR VALUES ('2010-09-01','2010-10-01','2010-11-01','2010-12-01','2011-01-01','2011-02-01','2011-03-01','2011-04-01','2011-05-01','2011-06-01','2010-07-01');
CREATE PARTITION SCHEME [AirAvCache Partition Scheme]
AS PARTITION [AirAvCache Partition Range]
TO ([AirAvCache FG1], [AirAvCache FG2], [AirAvCache FG3],[AirAvCache FG4],[AirAvCache FG5],[AirAvCache FG6],[AirAvCache FG7],[AirAvCache FG8],
[AirAvCache FG9],[AirAvCache FG10],[AirAvCache FG11],[AirAvCache FG12]);
CREATE TABLE [dbo].[AvCache](
[CityPair] [varchar](6) NOT NULL,
[FlightNo] [varchar](10) NULL,
[FlightDate] [datetime] NOT NULL,
[CacheTime] [datetime] NOT NULL DEFAULT (getdate()),
[AVNote] [varchar](300) NULL
) ON [AirAvCache Partition Scheme] (FlightDate); --注意这里使用 [AirAvCache Partition Scheme]架构,根据FlightDate 分区
-- 查看使用情况
SELECT *, $PARTITION.[AirAvCache Partition Range](FlightDate)
FROM dbo.AVCache