SQL server 2005的表分區

 
下面來說下,在SQL SERVER 2005的表分區裏,如何對已經存在的有數據的表進行分區,其實道理和之前在http://www.cnblogs.com/jackyrong/archive/2006/11/13/559354.html說到一樣,只不過交換下順序而已,下面依然用例子說明:
  
依然在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)
這裏表明分區的原則是四個分區,從負數到100101-200201-300,大於300
當然,如果用right for values的話,就是從負數到99100199200-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'
 
可以看到,原來的表的數據被正確分拆到四個文件組裏去了,實現了表分區
 


參考示例 : http://www.5h6.com/article/27994.html
 
 
 
發佈了8 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章