下面是具体的SQL语句:
1、最基本,最重要的一步就是创建分区函数.创建分区函数首先要确定分区键--既按照哪字段来进行分区.在这个例子里,我用提交时间来作为分区键,由于数据量的问题,最终决定每个月的数据放一个单独的分区.
CREATE PARTITION FUNCTION IODPFN(VARCHAR(20))
AS RANGE LEFT FOR VALUES (
'2007/12/01 00:00:00',
'2008/01/01 00:00:00',
'2008/02/01 00:00:00',
'2008/03/01 00:00:00',
'2008/04/01 00:00:00',
'2008/05/01 00:00:00'
);
2、上一步是完成一个概念上的分区,接下来要完成一个物理的构建,使得属于不同分区的数据存储到不同的物理文件上去.
a.创建文件组
ALTER DATABASE IOD ADD FILEGROUP [iod200712];
ALTER DATABASE IOD ADD FILEGROUP [iod200801];
ALTER DATABASE IOD ADD FILEGROUP [iod200802];
ALTER DATABASE IOD ADD FILEGROUP [iod200803];
ALTER DATABASE IOD ADD FILEGROUP [iod200804];
ALTER DATABASE IOD ADD FILEGROUP [iod200805];
b.创建物理文件,在这里,为了方便起见,我把每个物理文件放到了一个单独的文件组里面.
ALTER DATABASE IOD ADD FILE
(NAME = 'iod200712',FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\iod200712.ndf',SIZE = 1GB,MAXSIZE = UNLIMITED,FILEGROWTH = 100MB) TO FILEGROUP [iod200712];
ALTER DATABASE IOD ADD FILE
(NAME = 'iod200801',FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\iod200801.mdf',SIZE = 1GB,MAXSIZE = UNLIMITED,FILEGROWTH = 100MB) TO FILEGROUP [iod200801];
ALTER DATABASE IOD ADD FILE
(NAME = 'iod200802',FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\iod200802.ndf',SIZE = 1GB,MAXSIZE = UNLIMITED,FILEGROWTH = 100MB) TO FILEGROUP [iod200802];
ALTER DATABASE IOD ADD FILE
(NAME = 'iod200803',FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\iod200803.mdf',SIZE = 1GB,MAXSIZE = UNLIMITED,FILEGROWTH = 100MB) TO FILEGROUP [iod200803];
ALTER DATABASE IOD ADD FILE
(NAME = 'iod200804',FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\iod200804.mdf',SIZE = 1GB,MAXSIZE = UNLIMITED,FILEGROWTH = 100MB) TO FILEGROUP [iod200804];
ALTER DATABASE IOD ADD FILE
(NAME = 'iod200805',FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\iod200805.mdf',SIZE = 1GB,MAXSIZE = UNLIMITED,FILEGROWTH = 100MB) TO FILEGROUP [iod200805];
3、创建完分区函数,接下来就要建立分区架构,用来将概念上的分区和文件组(物理文件)关联起来.
CREATE PARTITION SCHEME IODPScheme
AS PARTITION IODPFN
TO ([iod200712],[iod200801],[iod200802],[iod200803],[iod200804],[iod200805],[PRIMARY]);
4、最后是建表
drop table iod_data;
create table iod_data
(
id varchar(20) primary key,
msg_type varchar(10),
send_service_type varchar(10),
receive_service_type varchar(10),
src varchar(20),
src_top varchar(10),
src_npi varchar(10),
dest varchar(20),
dest_top varchar(10),
dest_npi varchar(10),
last_time varchar(20),
remote_user_type varchar(10),
pid varchar(10),
dcs varchar(10),
msg_length varchar(10),
msg_status varchar(10),
send_ip varchar(15),
resive_ip varchar(15),
smid varchar(10),
sm_status varchar(10),
sm_errcode varchar(10),
fee varchar(10),
submit_time varchar(20),
business_subtype varchar(10),
msgid_timestamp varchar(10),
msgid_gateway varchar(10),
msgid_seq varchar(10),
same_msg_sum varchar(10),
same_msg_seq varchar(10),
msg_level varchar(10),
fee_user_type varchar(10),
fee_terminal_Id varchar(20),
msg_src varchar(10),
fee_type varchar(10),
fee_code varchar(10),
service_code varchar(20),
isnot_write_fee varchar(10),
confirm_type varchar(10),
pps varchar(10),
local_gateway_code varchar(10),
process_time varchar(20),
src_gateway_code varchar(10),
front_gateway_code varchar(10),
content varchar(255)
)
on IODPScheme(submit_time);
其它,可以建索引以提高查询速度,但插入数据的速度会变慢
CREATE INDEX iod_data_ix1 ON iod_data(src,dest);