Sql Server 分区演练
USE [master]
GO
if exists (select * from sys.databases where name = 'Test_1')
drop database Test_1
GO
--創(chuàng)建新庫(kù),要演練分區(qū)所以我們會(huì)多創(chuàng)建兩個(gè)文件組Test_A,Test_B,以便在后面的分區(qū)方案中使用。
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
--若分區(qū)函數(shù)存在則先drop掉
IF? EXISTS (SELECT * FROM sys.partition_functions WHERE name = N'test_partition')
DROP PARTITION FUNCTION [test_partition]
GO
/**//*創(chuàng)建分區(qū)函數(shù)給后面的分區(qū)方案使用,分區(qū)函數(shù)很簡(jiǎn)單就是指定一個(gè)范圍確定在某個(gè)值為什么的時(shí)候放在那個(gè)分區(qū)上*/
--新建一個(gè)簡(jiǎn)單的分區(qū)函數(shù),該函數(shù)以1000為界分兩個(gè)區(qū)
create partition function test_partition(int)
AS
RANGE LEFT FOR VALUES (1000)
go
/**//*看分區(qū)方案是否存在,若存在先drop掉*/
IF? EXISTS (SELECT * FROM sys.partition_schemes WHERE name = N'test_scheme')
DROP PARTITION SCHEME test_scheme
GO
--創(chuàng)建分區(qū)方案,分區(qū)方案需要指定一個(gè)分區(qū)函數(shù),并指定在分區(qū)函數(shù)中分的區(qū)需要放在哪一個(gè)文件組上
create partition scheme test_scheme
AS
PARTITION [test_partition] TO (test_A,test_B)
GO
--創(chuàng)建分區(qū)表
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) --在此處指定該表要使用的分區(qū)方案,并將指定分區(qū)依據(jù)列
go
--隨便插入幾條數(shù)據(jù)
insert into student values ('AQU',10,100); -- 這條數(shù)據(jù)在A分區(qū)上
insert into student values ('AQU_邊界',1000,89); -- 這邊數(shù)據(jù)也在A分區(qū)上是個(gè)邊界,因?yàn)槲覀兩厦嬖诤瘮?shù)中指定的是RANGE LEFT,所以1000在A分區(qū)上
insert into student values ('BQU',1001,90); -- 這一條肯定是在B分區(qū)上了。
go
--最后看看結(jié)果。$partition.分區(qū)函數(shù)(分區(qū)列)可以返回某一行所在的分區(qū)序號(hào)
select *,分區(qū)序號(hào) = $partition.test_partition(class) from student
GO
轉(zhuǎn)自:http://www.cnblogs.com/yukaizhao/archive/2008/05/07/sql_partition_test.html
?
?
?
declare?? @a?? int??
? set?? @a=0??
? while?? @a<=100000?
? begin??
?????????????? insert into student values ('AQU',@a,100);
??????????????? set?? @a=@a+1??
? end
總結(jié)
以上是生活随笔為你收集整理的Sql Server 分区演练的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Visual Studio 2008 快
- 下一篇: 理解AppDomain