[转载]Sql Server 分区演练

http://www.cnblogs.com/yukaizhao/archive/2008/05/07/sql_partition_test.html

代码加注释,希望对初学者有用。

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

转载于:https://www.cnblogs.com/fx2008/archive/2011/12/08/2280226.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值