|
本文演示了 SQL Server 2005 分區(qū)表分區(qū)切換的三種形式:
1. 切換分區(qū)表的一個分區(qū)到普通數(shù)據(jù)表中:Partition to Table;
2. 切換普通表數(shù)據(jù)到分區(qū)表的一個分區(qū)中:Table to Partition;
3. 切換分區(qū)表的分區(qū)到另一分區(qū)表:Partition to Partition。
并指出了在分區(qū)表分區(qū)切換過程中的注意事項。
-- 創(chuàng)建分區(qū)函數(shù)create partition function PF_Orders_OrderDateRange(datetime)asrange right for values ('1997-01-01','1998-01-01','1999-01-01')go-- 創(chuàng)建分區(qū)方案create partition scheme PS_OrdersASPartition PF_Orders_OrderDateRangeto ([primary], [primary], [primary], [primary])go-- 創(chuàng)建分區(qū)表create table dbo.Orders( OrderID int not null ,CustomerID varchar(10) not null ,EmployeeID int not null ,OrderDate datetime not null)on PS_Orders(OrderDate)go-- 創(chuàng)建聚集分區(qū)索引create clustered index IXC_Orders_OrderDate on dbo.Orders(OrderDate)go-- 為分區(qū)表設(shè)置主鍵alter table dbo.Orders add constraint PK_Orders primary key (OrderID, CustomerID, OrderDate)go-- 導(dǎo)入數(shù)據(jù)到分區(qū)表insert into dbo.Ordersselect OrderID, CustomerID, EmployeeID, OrderDate from dbo.Orders_From_SQL2000_Northwind --(注:數(shù)據(jù)來源于 SQL Server 2000 示例數(shù)據(jù)庫)go-- 查看分區(qū)表每個分區(qū)的數(shù)據(jù)分布情況select partition = $partition.PF_Orders_OrderDateRange(OrderDate) ,rows = count(*) ,minval = min(OrderDate) ,maxval = max(OrderDate) from dbo.Orders group by $partition.PF_Orders_OrderDateRange(OrderDate) order by partitiongo
一、切換分區(qū)表的一個分區(qū)到普通數(shù)據(jù)表中:Partition to Table
首先建立普通數(shù)據(jù)表 Orders_1998,該表用來存放訂單日期為 1998 年的所有數(shù)據(jù)。
create table dbo.Orders_1998( OrderID int not null ,CustomerID varchar(10) not null ,EmployeeID int not null ,OrderDate datetime not null) on [primary]gocreate clustered index IXC_Orders1998_OrderDate on dbo.Orders_1998(OrderDate)goalter table dbo.Orders_1998 add constraint PK_Orders_1998 primary key nonclustered (OrderID, CustomerID, OrderDate)go
開始切換分區(qū)表 Orders 第三個分區(qū)的數(shù)據(jù)(1998年的數(shù)據(jù))到普通表 Orders_1998
alter table dbo.Orders switch partition 3 to dbo.Orders_1998
值得注意的是,如果你想順利地進行分區(qū)到普通表的切換,最好滿足以下的前提條件:
1. 普通表必須建立在分區(qū)表切換分區(qū)所在的文件組上。
2. 普通表的表結(jié)構(gòu)跟分區(qū)表的一致;
3. 普通表上的索引要跟分區(qū)表一致。
4. 普通表必須是空表,不能有任何數(shù)據(jù)。
二、切換普通表數(shù)據(jù)到分區(qū)表的一個分區(qū)中:Table to Partition
上面我們已經(jīng)把分區(qū)表 Orders 第三個分區(qū)的數(shù)據(jù)切換到普通表 Orders_1998 中了,現(xiàn)在我們再切換回來:
alter table dbo.Orders_1998 switch to dbo.Orders partition 3
但是,此時有錯誤發(fā)生:
Msg 4982, Level 16, State 1, Line 1ALTER TABLE SWITCH statement failed.Check constraints of source table 'Sales.dbo.Orders_1998' allow valuesthat are not allowed by range defined by partition 3 on target table 'Sales.dbo.Orders'.
這就奇怪了,能把數(shù)據(jù)從分區(qū)切換進來卻切換不出去。出錯信息中提示我們是普通表的 check constraint 跟分區(qū)表不一致。于是在普通表上建立 check constraint:
alter table dbo.Orders_1998 add constraint CK_Orders1998_OrderDate check (OrderDate>='1998-01-01' and OrderDate<'1999-01-01')
再次進行切換,成功!
看來,切換普通表數(shù)據(jù)到分區(qū),除了滿足上面的 4 個條件外,還要加上一條:
普通表必須加上和分區(qū)數(shù)據(jù)范圍一致的 check 約束條件。
三、切換分區(qū)表的分區(qū)到另一分區(qū)表:Partition to Partition
首先建立分區(qū)表 OrdersArchive,這個表用來存放訂單歷史數(shù)據(jù)。
-- 創(chuàng)建分區(qū)函數(shù)create partition function PF_OrdersArchive_OrderDateRange(datetime)asrange right for values ('1997-01-01','1998-01-01','1999-01-01')go-- 創(chuàng)建分區(qū)方案create partition scheme PS_OrdersArchiveASPartition PF_OrdersArchive_OrderDateRangeto ([primary], [primary], [primary], [primary])go-- 創(chuàng)建分區(qū)表create table dbo.OrdersArchive( OrderID int not null ,CustomerID varchar(10) not null ,EmployeeID int not null ,OrderDate datetime not null)on PS_OrdersArchive(OrderDate)go-- 創(chuàng)建聚集分區(qū)索引create clustered index IXC_OrdersArchive_OrderDate on dbo.OrdersArchive(OrderDate)go-- 為分區(qū)表設(shè)置主鍵alter table dbo.OrdersArchive add constraint PK_OrdersArchive primary key (OrderID, CustomerID, OrderDate)go
然后,切換分區(qū)表 Orders 分區(qū)數(shù)據(jù)到 OrdersArchive 分區(qū):
alter table dbo.Orders switch partition 1 to dbo.OrdersArchive partition 1 alter table dbo.Orders switch partition 2 to dbo.OrdersArchive partition 2 alter table dbo.Orders switch partition 3 to dbo.OrdersArchive partition 3
最后,查看分區(qū)表 OrdersArchive 各分區(qū)數(shù)據(jù)分布情況:
-- 查看分區(qū)表每個分區(qū)的數(shù)據(jù)分布情況select partition = $partition.PF_OrdersArchive_OrderDateRange(OrderDate) ,rows = count(*) ,minval = min(OrderDate) ,maxval = max(OrderDate) from dbo.OrdersArchive group by $partition.PF_OrdersArchive_OrderDateRange(OrderDate) order by partition
實際上,分區(qū)表分區(qū)切換并沒有真正去移動數(shù)據(jù),而是 SQL Server 在系統(tǒng)底層改變了表的元數(shù)據(jù)。因此分區(qū)表分區(qū)切換是高效、快速、靈活的。利用分區(qū)表的分區(qū)切換功能,我們可以快速加載數(shù)據(jù)到分區(qū)表。卸載分區(qū)數(shù)據(jù)到普通表,然后 truncate 普通表,以實現(xiàn)快速刪除分區(qū)表數(shù)據(jù)。快速歸檔不活躍數(shù)據(jù)到歷史表。
it知識庫:SQL Server 2005 分區(qū)表實踐——分區(qū)切換,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。