Sql Server 分區

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'D:\sqldata\test_1.mdf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ),
 FILEGROUP [test_A] 
( NAME = N'Test_A', FILENAME = N'D:\sqldata\test_A.ndf' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ),
FILEGROUP [test_B] 
( NAME = N'Test_B', FILENAME = N'D:\sqldata\test_B.ndf' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'Test_log', FILENAME = N'D:\sqldata\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 *,分區序號 = $partition.test_partition(class) from student
GO

參考:http://www.cnblogs.com/yukaizhao/archive/2008/05/07/sql_partition_test.htmlhtml

相關文章
相關標籤/搜索