SQL server 2005的表分区
下面来说下,在sql server 2005的表分区里,如何对已经存在的有数据的表进行分区,其实道理和之前在说到一样,只不过交换下顺序而已,下面依然用例子说明:
依然在c盘的data2目录下建立4个文件夹,用来做4个文件组,然后建立
use master
if exists (select name from sys.databases where name = n'data partition db3')
drop database [data partition db3]
go
create database [data partition db3]
on primary
(name='data partition db primary fg3',
filename=
'c:\data2\primary\data partition db primary fg3.mdf',
size=5,
maxsize=500,
filegrowth=1 ),
filegroup [data partition db3 fg1]
(name = 'data partition db3 fg1',
filename =
'c:\data2\fg1\data partition db3 fg1.ndf',
size = 5mb,
maxsize=500,
filegrowth=1 ),
filegroup [data partition db3 fg2]
(name = 'data partition db3 fg2',
filename =
'c:\data2\fg2\data partition db3 fg2.ndf',
size = 5mb,
maxsize=500,
filegrowth=1 ),
filegroup [data partition db3 fg3]
(name = 'data partition db3 fg3',
filename =
'c:\data2\fg3\data partition db3 fg3.ndf',
size = 5mb,
maxsize=500,
filegrowth=1 ),
filegroup [data partition db3 fg4]
(name = 'data partition db3 fg4',
filename =
'c:\data2\fg4\data partition db3 fg4.ndf',
size = 5mb,
maxsize=500,
filegrowth=1 )
然后建立一个数据表:
use [data partition db3]
go
create table mytable
(id int not null,
date datetime,
cost money ) on [primary]
并建立一个索引
use [data partition db3]
go
create unique clustered index mytable_ixc
on mytable(id) on [primary]
接下来往表里增加数据
use [data partition db3]
go
declare @count int
set @count =-25
while @count <=100
begin
insert into mytable select @count,getdate(),100.00
set @count=@count+1
end
set @count =101
while @count <=200
begin
insert into mytable select @count,getdate(),200.00
set @count=@count+1
end
set @count =201
while @count <=300
begin
insert into mytable select @count,getdate(),300.00
set @count=@count+1
end
set @count =301
while @count <=400
begin
insert into mytable select @count,getdate(),400.00
set @count=@count+1
end
set @count =401
while @count <=800
begin
insert into mytable select @count,getdate(),500.00
set @count=@count+1
end
此时查询一下,可以看到数据都在一个表里select * from sys.partitions where object_name(object_id)='mytable'
我们再建立表分区函数use [data partition db3]
go
create partition function [data partition range](int)
as range left for values (100,200,300)
这里表明分区的原则是四个分区,从负数到100,101-200,201-300,大于300
当然,如果用right for values的话,就是从负数到99,100到199,200-299,和大于300
最后,把表分区函数应用到文件组里
use [data partition db3]
go
create partition scheme [data partition scheme]
as partition [data partition range]
to ([data partition db3 fg1], [data partition db3 fg2], [data partition db3 fg3],[data partition db3 fg4]);
把原来建立好的表,移动到这个表分区里
drop index mytable_ixc on mytable with (move to [data partition scheme] (id) )
最后看一看select * from sys.partitions where object_name(object_id)='mytable'
可以看到,原来的表的数据被正确分拆到四个文件组里去了,实现了表分区
推荐阅读
-
在SQL Server 2005中创建CLR存储过程的详细介绍
-
SQL Server 2005 创建简单的存储过程--总结分析
-
深入sql server 2005 万能分页存储过程的详解
-
基于Python的SQL Server数据库实现对象同步轻量级
-
深入SQL Server 跨数据库查询的详解
-
使用SQL Server 获取插入记录后的ID(自动编号)
-
SQL SERVER函数之深入表值函数的处理分析
-
SQL Server利用sp_spaceused如何查看表记录存在不准确的情况
-
深入SQL SERVER合并相关操作Union,Except,Intersect的详解
-
Oracle 实现类似SQL Server中自增字段的一个办法