zoukankan
html css js c++ java
Sql Server 分区演练 【转】
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 est_1.mdf
'
, SIZE
=
10240KB , MAXSIZE
=
UNLIMITED, FILEGROWTH
=
1024KB ),
FILEGROUP
[
test_A
]
( NAME
=
N
'
Test_A
'
, FILENAME
=
N
'
D:sqldata est_A.ndf
'
, SIZE
=
1024KB , MAXSIZE
=
UNLIMITED, FILEGROWTH
=
1024KB ),
FILEGROUP
[
test_B
]
( NAME
=
N
'
Test_B
'
, FILENAME
=
N
'
D:sqldata est_B.ndf
'
, SIZE
=
1024KB , MAXSIZE
=
UNLIMITED, FILEGROWTH
=
1024KB )
LOG
ON
( NAME
=
N
'
Test_log
'
, FILENAME
=
N
'
D:sqldataTest_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
查看全文
相关阅读:
[LeetCode] Repeated DNA Sequences hash map
[LeetCode] Largest Number 排序
[LeetCode] Convert Sorted Array to Binary Search Tree
[LeetCode] Populating Next Right Pointers in Each Node 深度搜索
[LeetCode] Binary Search Tree Iterator 深度搜索
[LeetCode] Best Time to Buy and Sell Stock II 贪心算法
[LeetCode] Find Peak Element 二分搜索
[LeetCode] 3Sum Closest
[LeetCode] Gas Station 贪心
[LeetCode] Number of 1 Bits 位操作
原文地址:https://www.cnblogs.com/dufu/p/4178541.html
最新文章
USB线接口线序
RT-Thread代码_线程创建
HDU5478 原根求解
bitset常用函数用法记录 (转载)
hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题
JQery w3school学习第一章 标签的隐藏和显示
HDU 5451 广义斐波那契数列
SGU 140 扩展欧几里得
POJ 1777 mason素数
HDU 4085 斯坦纳树
热门文章
Stern-Brocot树 及 法里级数分析
HDU 5439 Aggregated Counting
P3384 【模板】树链剖分
F
F
D
F
E
F
高斯消元模板
Copyright © 2011-2022 走看看