mssqlserver 分区表

USE [master]
GO
if exists (select * from sys.databases where name = 'Test_1')
drop database Test_1
GO
--创建新库,要演练分区所以我们会多创建两个文件组Test_A,Test_B,以便在后面的分区方案中使用。
CREATE DATABASE [Test_1] ON PRIMARY 
( NAME = N'test_1', FILENAME = N'e:\sqltest\fenqv\test_1.mdf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ),
FILEGROUP [test_A] 
( NAME = N'Test_A', FILENAME = N'g:\sqltest\fenqv\test_A.ndf' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ),
FILEGROUP [test_B] 
( NAME = N'Test_B', FILENAME = N'g:\sqltest\fenqv\test_B.ndf' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON 
( NAME = N'Test_log', FILENAME = N'e:\sqltest\fenqv\Test_log.ldf' , SIZE = 7616KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
COLLATE Chinese_PRC_CI_AS
GO
USE [Test_1]
GO

--若分区函数存在则先drop掉
IF EXISTS (SELECT * FROM sys.partition_functions WHERE name = N'test_partition')
DROP PARTITION FUNCTION [test_partition]
GO
/**//*创建分区函数给后面的分区方案使用,分区函数很简单就是指定一个范围确定在某个值为什么的时候放在那个分区上*/
--新建一个简单的分区函数,该函数以1000为界分两个区
create partition function test_partition(int)
AS
RANGE LEFT FOR VALUES (1000) 
go
/**//*看分区方案是否存在,若存在先drop掉*/
IF EXISTS (SELECT * FROM sys.partition_schemes WHERE name = N'test_scheme')
DROP PARTITION SCHEME test_scheme
GO
--创建分区方案,分区方案需要指定一个分区函数,并指定在分区函数中分的区需要放在哪一个文件组上
create partition scheme test_scheme 
AS 
PARTITION [test_partition] TO (test_A,test_B)
GO
--创建分区表
if object_id('student','U') is not null
drop table student;
go
create table student
(
id int identity(1,1) not null,
name varchar(10) not null,
class int not null,
grade int
) on test_scheme(class) --在此处指定该表要使用的分区方案,并将指定分区依据列
go
--随便插入几条数据
insert into student values ('AQU',10,100); -- 这条数据在A分区上
insert into student values ('AQU_边界',1000,89); -- 这边数据也在A分区上是个边界,因为我们上面在函数中指定的是RANGE LEFT,所以1000在A分区上
insert into student values ('BQU',1001,90); -- 这一条肯定是在B分区上了。

go
--最后看看结果。$partition.分区函数(分区列)可以返回某一行所在的分区序号
select top 10000 *,分区序号 = $partition.test_partition(class) from student
GO

-------------------------------
--------循环插入10W条数据
-------------------------------
declare @i int
set @i=0
while @i<100000
begin
insert into student values ('AQU',@i,100+@i)
set @i = @i+1
end

 

posted on 2012-07-02 14:05  。!  阅读(234)  评论(0编辑  收藏  举报