详解SQL Server中创建数据仓库已分区表
在本練習中,您將創建一個分區數據倉庫事實數據表。非常大的表經常需要跨幾個磁盤卷存儲數據。ServerSecurity/Database/'>SQL?Server?表無法放置在特定文件中。但是,文件組可以放置在文件中,而表可以分配給文件組。這樣您就可以控制?ServerSecurity/Database/'>SQL?Server?中非常大的表中的數據的存儲。而且,如果表跨幾個文件組,定義哪些數據放置在哪個文件組中會非常有用。分區函數通過基于特定列中的值沿水平方向拆分表提供了此功能。
注意: 您可以復制此練習中所用的腳本,這些腳本位于 C:ServerSecurity/Database/'>SQLHOLSPartitioningSolutionPartition Processing 文件夾中的 Partition Processing.ssmssln 解決方案中。
1.新建?ServerSecurity/Database/'>SQL?Server?腳本項目
(1)從開始->所有程序菜單中的 Microsoft?ServerSecurity/Database/'>SQL?Server?2008 程序組中啟動ServerSecurity/Database/'>SQL?Server?Management Studio。
(2)在連接到服務器對話框中,驗證下列設置無誤后單擊連接:
· 服務器類型:數據庫引擎
· 服務器名稱:(local)
· 身份驗證:Windows 身份驗證
(3)在文件菜單上,指向新建,然后單擊項目。
(4)確保選中?ServerSecurity/Database/'>SQL?Server?腳本,然后輸入下列設置:
· 名稱:Partition Processing
· 位置:C:ServerSecurity/Database/'>SQLHOLsPartitioningStarter
· 解決方案名稱:Partition Processing
(5)確保選中創建解決方案的目錄,然后單擊確定。
(6)在解決方案資源管理器中,右鍵單擊連接,然后單擊新建連接。
(7)在連接到服務器對話框中,驗證下列設置無誤后單擊確定:
· 服務器名稱:(local)
· 身份驗證:Windows 身份驗證
2.創建文件組和文件
(1)在解決方案資源管理器中,右鍵單擊在前面步驟中添加的連接,然后單擊新建查詢。
(2)右鍵單擊?ServerSecurity/Database/'>SQLQuery1.sql,然后單擊重命名。
(3)鍵入 Files and Filegroups.sql,然后按 Enter。
(4)鍵入下面的代碼(每個 FILENAME 參數都應單占一行)。
USE[master] ALTERDATABASE[AdventureWorksDW]ADDFILEGROUP [fg2001] GO ALTERDATABASE[AdventureWorksDW]ADDFILEGROUP [fg2002] GO ALTERDATABASE[AdventureWorksDW]ADDFILEGROUP [fg2003] GO ALTERDATABASE[AdventureWorksDW]ADDFILEGROUP [fg2004] GO ALTERDATABASE[AdventureWorksDW]ADDFILE (NAME =N'AdventureWorksDW_Data2001',
FILENAME =N'C:Program FilesMicrosoft?ServerSecurity/Database/'>SQLServerMSServerSecurity/Database/'>SQL10.MSServerSecurity/Database/'>SQLSERVERMSServerSecurity/Database/'>SQLDATAAdventureWorksDW_Data2001.ndf',
SIZE =2048KB , FILEGROWTH =1024KB ) TOFILEGROUP [fg2001] GO ALTERDATABASE[AdventureWorksDW]ADDFILE (NAME =N'AdventureWorksDW_Data2002',
FILENAME =N'C:Program FilesMicrosoft?ServerSecurity/Database/'>SQLServerMSServerSecurity/Database/'>SQL10.MSServerSecurity/Database/'>SQLSERVERMSServerSecurity/Database/'>SQLDATAAdventureWorksDW_Data2002.ndf',
SIZE =2048KB , FILEGROWTH =1024KB ) TOFILEGROUP [fg2002] GO ALTERDATABASE[AdventureWorksDW]ADDFILE (NAME =N'AdventureWorksDW_Data2003',
FILENAME =N'C:Program FilesMicrosoft?ServerSecurity/Database/'>SQLServerMSServerSecurity/Database/'>SQL10.MSServerSecurity/Database/'>SQLSERVERMSServerSecurity/Database/'>SQLDATAAdventureWorksDW_Data2003.ndf',
SIZE =2048KB , FILEGROWTH =1024KB) TOFILEGROUP [fg2003] GO ALTERDATABASE[AdventureWorksDW]ADDFILE (NAME =N'AdventureWorksDW_Data2004',
FILENAME =N'C:Program FilesMicrosoft?ServerSecurity/Database/'>SQLServerMSServerSecurity/Database/'>SQL10.MSServerSecurity/Database/'>SQLSERVERMSServerSecurity/Database/'>SQLDATAAdventureWorksDW_Data2004.ndf',
SIZE =2048KB , FILEGROWTH =1024KB ) TOFILEGROUP [fg2004] GO
(5)單擊執行。
3.創建分區函數
(1)在解決方案資源管理器中,右鍵單擊該連接,然后單擊新建查詢。
(2)右鍵單擊?ServerSecurity/Database/'>SQLQuery1.sql,然后單擊重命名。
(3)鍵入 Create Partition Function.sql,然后按 Enter。
(4)鍵入下面的代碼。
USEAdventureWorksDW
CREATEPARTITION FUNCTIONpf_OrderDateKey(int)
ASRANGE RIGHT FORVALUES(185,550)
GO
(5)單擊執行。
注意:分區函數提供了兩個文件組之間的邊界。在本例中,值是與 1 月 1 日對應的鍵。
4.創建分區方案
(1)在解決方案資源管理器中,右鍵單擊該連接,然后單擊新建查詢。
(2)右鍵單擊?ServerSecurity/Database/'>SQLQuery1.sql,然后單擊重命名。
(3)鍵入 Create Partition Scheme.sql,然后按 Enter。
(4)鍵入下面的代碼。單擊執行。
USEAdventureWorksDW
CREATEPARTITION SCHEME ps_OrderDateKey
ASPARTITION pf_OrderDateKey
TO(fg2001,fg2002,fg2003,fg2004)
GO
注意:雖然分區函數中僅列出了兩個邊界,但卻有四個文件組在分區函數中列出。第四個文件組是作為供將來的文件組拆分使用的下一個文件組提供的。
?
5.創建已分區表
(1)在解決方案資源管理器中,右鍵單擊該連接,然后單擊新建查詢。
(2)右鍵單擊?ServerSecurity/Database/'>SQLQuery1.sql,然后單擊重命名。
(3)鍵入 Create Table.sql,然后按 Enter。
(4)鍵入下面的代碼。
USEAdventureWorksDW
CREATETABLE[dbo].[FactInternetSalesPartitioned] (
[InternetSalesID][int]IDENTITY(1,1) NOTNULL,
[ProductKey][int]NOTNULL,
[OrderDateKey][int]NOTNULL,
[DueDateKey][int]NOTNULL,
[ShipDateKey][int]NOTNULL,
[CustomerKey][int]NOTNULL,
[PromotionKey][int]NOTNULL,
[CurrencyKey][int]NOTNULL,
[SalesTerritoryKey][int]NOTNULL,
[SalesOrderNumber][nvarchar](20) NOTNULL,
[OrderQuantity][smallint]NULL,
[UnitPrice][money]NULL,
CONSTRAINT[PK_ FactInternetSalesPartitioned]PRIMARYKEYCLUSTERED (
[InternetSalesID],
[ProductKey],
[OrderDateKey],
[DueDateKey],
[ShipDateKey],
[CustomerKey],
[PromotionKey],
[CurrencyKey],
[SalesTerritoryKey] )
)
ONps_OrderDateKey(OrderDateKey)
GO
(5)單擊執行。
6.將數據插入已分區表中
(1)在解決方案資源管理器中,右鍵單擊該連接,然后單擊新建查詢。
(2)右鍵單擊?ServerSecurity/Database/'>SQLQuery1.sql,然后單擊重命名。
(3)鍵入 Load Data.sql,然后按 Enter。
(4)鍵入下面的代碼。
USEAdventureWorksDW
INSERTINTO[dbo].[FactInternetSalesPartitioned] (
[ProductKey],
[OrderDateKey],
[DueDateKey],
[ShipDateKey],
[CustomerKey],
[PromotionKey],
[CurrencyKey],
[SalesTerritoryKey],
[SalesOrderNumber],
[OrderQuantity],
[UnitPrice] )
SELECT [ProductKey],
[OrderDateKey],
[DueDateKey],
[ShipDateKey],
[CustomerKey],
[PromotionKey],
[CurrencyKey],
[SalesTerritoryKey],
[SalesOrderNumber],
[OrderQuantity],
[UnitPrice] FROM[dbo].[FactInternetSales] GO
(5)單擊執行。
7.查看分區數據
(1)在解決方案資源管理器中,右鍵單擊該連接,然后單擊新建查詢。
(2)右鍵單擊?ServerSecurity/Database/'>SQLQuery1.sql,然后單擊重命名。
(3)鍵入 View Partitioned Data.sql,然后按 Enter。
(4)鍵入下面的代碼。
USEAdventureWorksDW
SELECTProductKey,
OrderDateKey,
$PARTITION.pf_OrderDateKey (OrderDateKey) ASPartitionNo
FROMFactInternetSalesPartitioned
GO SELECT$PARTITION.pf_OrderDateKey (OrderDateKey) ASPartitionNo,
COUNT(*) ASRows FROMFactInternetSalesPartitioned
GROUPBY$PARTITION.pf_OrderDateKey (OrderDateKey)
ORDERBYPartitionNo
GO
(5)單擊執行。
(6)待查詢完成后,查看結果。
注意:第一個結果集顯示表中每行的產品密鑰和訂單日期密鑰以及存儲各行的相應分區。
第二個結果集顯示各分區中的行數。
(7)保持?ServerSecurity/Database/'>SQL?Server?Management Studio 打開,下一個練習還要使用此程序。
總結
以上是生活随笔為你收集整理的详解SQL Server中创建数据仓库已分区表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 古希腊历史的五个阶段
- 下一篇: 精益生产管理的核心思想