表分區(qū)的目的:
1.把歷史數(shù)據(jù)放到另外一個(gè)表里面 可以提高查詢效率 當(dāng)然如果經(jīng)常查詢歷史數(shù)據(jù)和新數(shù)據(jù)的合并結(jié)果集這樣做就大大的不好了
2.通過把一個(gè)表放到不同的文件,不同的文件再存儲到不同的磁盤列陣中,可以提高IO速度?? CPU比硬盤快多了
3.提高可用性,一個(gè)磁盤壞了,另外一個(gè)磁盤上面的文件還能用 這個(gè)對我意義不大
4.便于備份 只需要做一個(gè)分區(qū)的備份就可以了,比如云服務(wù)端,數(shù)據(jù)量比較大的就4個(gè)表單,把這四個(gè)表放在一個(gè)文件里面這樣每天備份基本不花什么時(shí)間,還原也比較方便,這4個(gè)表的數(shù)據(jù)丟了意義也不大, 沒有做測試
?
?? 分區(qū)表的定義大體上分為三個(gè)步驟:
- ??? 定義分區(qū)函數(shù)
- ??? 定義分區(qū)構(gòu)架
- ??? 定義分區(qū)表 插入測試數(shù)據(jù)
- 【把一個(gè)表的數(shù)據(jù)導(dǎo)入到另外一個(gè)表中 這個(gè)速度挺快的】
--創(chuàng)建數(shù)據(jù)庫 這些操作可以在圖形界面完成
--因?yàn)樵谧约弘娔X上面測試 所以都放在同一個(gè)硬盤上面了?
http://www.cnblogs.com/CareySon/archive/2011/12/30/2307766.html

CREATE database Sales on primary ( name = N ' Sales ' , filename = N ' G:\data\Primary\Sales.mdf ' , size = 3MB, maxsize = 100MB, filegrowth = 10 % ), -- 創(chuàng)建文件組 filegroup FG1 ( NAME = N ' File1 ' , FILENAME = N ' G:\data\FG1\File1.ndf ' , SIZE = 1MB, MAXSIZE = 100MB, FILEGROWTH = 10 % ), -- 創(chuàng)建文件組 FILEGROUP FG2 ( NAME = N ' File2 ' , FILENAME = N ' G:\data\FG2\File2.ndf ' , SIZE = 1MB, MAXSIZE = 100MB, FILEGROWTH = 10 % ), -- 創(chuàng)建文件組 FILEGROUP FG3 ( NAME = N ' File3 ' , FILENAME = N ' G:\data\FG3\File3.ndf ' , SIZE = 1MB, MAXSIZE = 100MB, FILEGROWTH = 10 % ) LOG ON ( NAME = N ' Sales_Log ' , FILENAME = N ' G:\data\Primary\Sales_Log.ldf ' , SIZE = 1MB, MAXSIZE = 100MB, FILEGROWTH = 10 % ) GO
--創(chuàng)建分區(qū)函數(shù)

USE Sales GO CREATE PARTITION FUNCTION pf_OrderDate ( datetime ) AS RANGE RIGHT -- 以后默認(rèn)就寫Right 不要記混了 FOR VALUES ( ' 2003/01/01 ' , ' 2004/01/01 ' ) GO
--建立分區(qū)架構(gòu) 比如把1900-01-01-'2003/01/01'之間的數(shù)據(jù)存儲到FG2里面
-- '2003/01/01'- '2004/01/01'之間的數(shù)據(jù)也存儲到FG2里面
-- '2004/01/01'-? 之間的數(shù)據(jù)也存儲到FG3里面

-- 建立分區(qū)架構(gòu) 比如把1900-01-01-'2003/01/01'之間的數(shù)據(jù)存儲到FG2里面 -- '2003/01/01'- '2004/01/01'之間的數(shù)據(jù)也存儲到FG2里面 -- '2004/01/01'- 之間的數(shù)據(jù)也存儲到FG3里面 Use Sales go create partition scheme ps_OrderDate as partition pf_OrderDate to (FG2,FG2,FG3) go
--建立表

-- 建立表 Use Sales go create table Orders -- 訂單表 ( OrderID int identity ( 10000 , 1 ), OrderDate datetime not null , CustomerID int not null , constraint PK_Orders primary key (OrderID,OrderDate) ) on ps_OrderDate(OrderDate) -- 這句話決定這個(gè)表的不同之處 go create table OrdersHistory -- 訂單歷史表 可以把不常用的數(shù)據(jù)放到這個(gè)表里面 -- 這樣對于Orders的查詢就會大大提高 有必要的時(shí)候再通過Union All 查詢所有數(shù)據(jù) ( OrderID int identity ( 10000 , 1 ), OrderDate datetime not null , CustomerID int not null , constraint PK_OrdersHistory primary key (OrderID,OrderDate) ) on ps_OrderDate(OrderDate) go
--向表中插入數(shù)據(jù)

-- 向表中插入數(shù)據(jù) USE Sales GO INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2002/6/25 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2002/8/13 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2002/8/25 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2002/9/23 ' , 1000 ) GO 1000 USE Sales GO INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2003/6/25 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2003/8/13 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2003/8/25 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2003/9/23 ' , 1000 ) GO 1000 GO INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2006/6/25 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2007/8/13 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2008/8/25 ' , 1000 ) INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2009/9/23 ' , 1000 ) GO 11000
--查看結(jié)果

SELECT * FROM Orders SELECT * FROM dbo.OrdersHistory -- 查看各個(gè)分區(qū)的數(shù)據(jù)行數(shù) select $partition.pf_OrderDate(OrderDate) as Patition, COUNT ( * ) countRow from dbo.Orders group by $partition.pf_OrderDate(OrderDate) -- 檢驗(yàn)分區(qū)函數(shù) SELECT $partition.pf_OrderDate( ' 2002 ' ) AS ' 所在分區(qū) ' UNION ALL SELECT $partition.pf_OrderDate( ' 2003 ' ) AS ' 所在分區(qū) ' UNION ALL SELECT $partition.pf_OrderDate( ' 2004 ' ) AS ' 所在分區(qū) '
--把第二部分的數(shù)據(jù)轉(zhuǎn)存到歷史表中 把分區(qū)移動到不同的表,效率比較高消耗時(shí)間比較少
--目標(biāo)分區(qū)只能是空的;必須都存在;
--必須在同一個(gè)文件組內(nèi),分區(qū)號必須相同(是指兩個(gè)表公用同一個(gè)分區(qū)函數(shù)的情況下,不共用分區(qū)函數(shù)就沒有范圍限制了 )

-- 把第二部分的數(shù)據(jù)轉(zhuǎn)存到歷史表中 Use Sales go alter table orders switch partition 2 to ordersHistory partition 2 go
--查看結(jié)果

-- 查看結(jié)果 SELECT * FROM Orders SELECT * FROM dbo.OrdersHistory
?--創(chuàng)建一個(gè)新分區(qū)

-- 創(chuàng)建一個(gè)新分區(qū) ALTER PARTITION SCHEME ps_OrderDate NEXT USED FG3 ALTER PARTITION FUNCTION pf_OrderDate() SPLIT RANGE ( ' 2007/01/01 ' ) -- 檢查效果 INSERT INTO dbo.Orders (OrderDate, CustomerID) VALUES ( ' 2009/9/23 ' , 1000 ) GO 9 -- 查看各個(gè)分區(qū)的數(shù)據(jù)行數(shù) select $partition.pf_OrderDate(OrderDate) as Patition, COUNT ( * ) countRow from dbo.Orders group by $partition.pf_OrderDate(OrderDate)
--刪除一個(gè)分區(qū)

-- 移除分區(qū) ALTER PARTITION FUNCTION pf_OrderDate() MERGE RANGE ( ' 2004/01/01 ' )
--檢查移除效果

-- 檢查移除效果 SELECT $partition.pf_OrderDate( ' 2002 ' ) AS ' 所在分區(qū) ' UNION ALL SELECT $partition.pf_OrderDate( ' 2003 ' ) AS ' 所在分區(qū) ' UNION ALL SELECT $partition.pf_OrderDate( ' 2004 ' ) AS ' 所在分區(qū) ' UNION ALL SELECT $partition.pf_OrderDate( ' 2008 ' ) AS ' 所在分區(qū) '
-- 移除分區(qū)函數(shù) 分區(qū)方案
a。刪除源表 再刪除分區(qū)函數(shù) 分區(qū)方案
truncate table name1
drop table name1
DROP PARTITION SCHEME name1 DROP PARTITION FUNCTION name1
b。如果是沒有聚集索引的表
可以通過重新建立索引而不引用分區(qū)方案的方式來
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
