天天看點

MSSQL2008分區表

概念就不多說了,網上大把.  

--分區函數的作用是告訴SQL Server,如何将資料進行分區,例如按時間,按年齡,按省.

--而分區方案的作用則是告訴SQL Server,将已分區的資料放在哪個檔案組中. 多個硬碟上分開選擇的檔案組更好. 

一,用語句

建立,分區方案:

CREATE PARTITION SCHEME YearCustomerFollow AS PARTITION  

YearCustomerFollowFunction TO([PRIMARY],[PRIMARY],[PRIMARY],[PRIMARY],[PRIMARY]) 

建立,分區函數:

CREATE PARTITION FUNCTION YearCustomerFollowFunction(datetime) 

 AS RANGE RIGHT FOR VALUES('20100101','20110101','20120101','20130101')  

删除一個分區:

ALTER PARTITION FUNCTION YearCustomerFollowFunction() MERGE RANGE ('20100101')   

添加一個分區:

ALTER PARTITION SCHEME YearCustomerFollow NEXT USED [PRIMARY]     -- 分區方案   

ALTER PARTITION FUNCTION YearCustomerFollowFunction() SPLIT RANGE ('20090101')  -- 分界值  

建立分區:(這裡的語句可以用向導生成更友善)

USE [SQL_ZZW] 

GO 

BEGIN TRANSACTION 

CREATE CLUSTERED INDEX [ClusteredIndex_on_YearCustomerFollow_01] ON [dbo].[ZZW_test]  

    [birthyday] 

)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [YearCustomerFollow]([birthyday]) 

DROP INDEX [ClusteredIndex_on_YearCustomerFollow_01] ON [dbo].[ZZW_test] WITH ( ONLINE = OFF ) 

 查詢分區表行數.

select $PARTITION.YearCustomerFollowFunction(birthyday) as 分區編号,count(id) as 記錄數  

from zzw_test  

group by $PARTITION.YearCustomerFollowFunction(birthyday)   

二,用向導

1,在要修改的表上右鍵->存儲->建立分區.

<a href="http://blog.51cto.com/attachment/201303/174636257.jpg" target="_blank"></a>

2,選擇分區列.

<a href="http://blog.51cto.com/attachment/201303/174815541.jpg" target="_blank"></a>

3,建立分區函數或選擇以後的分區函數.

<a href="http://blog.51cto.com/attachment/201303/175832623.jpg" target="_blank"></a>

4,建立分區方案或選擇現有分區方案.

<a href="http://blog.51cto.com/attachment/201303/175952329.jpg" target="_blank"></a>

5,映射分區,先設定邊界.

<a href="http://blog.51cto.com/attachment/201303/175905366.jpg" target="_blank"></a>

6,可以看到邊界值都一下子已經設定好了.

<a href="http://blog.51cto.com/attachment/201303/180054607.jpg" target="_blank"></a>

7,建立完成.

<a href="http://blog.51cto.com/attachment/201303/180156929.jpg" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201303/180205117.jpg" target="_blank"></a>

檢視:表右鍵-&gt;存儲-管理壓縮.可以看到各分區的行數,空間.

<a href="http://blog.51cto.com/attachment/201303/180302286.jpg" target="_blank"></a>

在控制台庫-&gt;存儲下可以直接看到剛建立的分區方案和分區函數.

<a href="http://blog.51cto.com/attachment/201303/180608296.jpg" target="_blank"></a>

本文轉自 idyllic_cyty 51CTO部落格,原文連結:http://blog.51cto.com/aizzw/1160531