zoukankan
html css js c++ java
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
查看全文
相关阅读:
5.装饰器生成器迭代器
python-进程&线程
模块(二)
MYSQL数据库操作(一)
python-网络编程-socket编程
python-异常处理
python-面向对象进阶
python-模块
python_面向对象编程
生成器 迭代器,装饰器 ,软件开发规范
原文地址:https://www.cnblogs.com/yukaizhao/p/sql_partition_test.html
最新文章
I/O流
(1)Map集合 (2)异常机制 (3)File类 (4)I/O流
(1)List集合 (2)Queue集合 (3)Set集合
(1)StringBuilder类和StringBuffer类 (2)日期相关的类 (3)集合框架 (4)List集合
(1)String类 (2)StringBuilder类和StringBuffer类 (3)日期相关的类
优化MariaDB导入大量数据时间
MariaDB安装与基本使用方法
SQLite下载与基本操作
单例模式
多线程面试题
热门文章
IDEA 常用快捷键
1.4linux文件和目录常用命令
1.3Linux 终端命令格式
1.2Linux 主要目录速查表
1.1Ubuntu安装
linux主要目录速查表
9.random_os_sys_shutil_shelve_xml_hashlib
8.模块定义导入优化time datetime内置模块
7.json&pickle及软件目录结构规范
6.内置函数
Copyright © 2011-2022 走看看