mssql表分區


1:表分區

什么是表分區
一般情況下,我們建立數據庫表時,表數據都存放在一個文件里。
但是如果是分區表的話,表數據就會按照你指定的規則分放到不同的文件里,把一個大的數據文件拆分為多個小文件,還可以把這些小文件放在不同的磁盤下由多個cpu進行處理。這樣文件的大小隨着拆分而減小,還得到硬件系統的加強,自然對我們操作數據是大大有利的。
所以大數據量的數據表,對分區的需要還是必要的,因為它可以提高select效率,還可以對歷史數據經行區分存檔等。但是數據量少的數據就不要湊這個熱鬧啦,因為表分區會對數據庫產生不必要的開銷,除啦性能還會增加實現對象的管理費用和復雜性。

創建文件組

T-sql語法:
alter database <數據庫名> add filegroup <文件組名>

alter database testdb add filegroup testdbGroup1
alter database testdb add filegroup testdbGroup2
alter database testdb add filegroup testdbGroup3
alter database testdb add filegroup testdbGroup4
alter database testdb add filegroup testdbGroup5
alter database testdb add filegroup testdbGroup6
alter database testdb add filegroup testdbGroup7
alter database testdb add filegroup testdbGroup8
alter database testdb add filegroup testdbGroup9
alter database testdb add filegroup testdbGroup10

創建數據文件到文件組里面

T-sql語法:

alter database <數據庫名稱> add file <數據標識> to filegroup <文件組名稱> --<數據標識> (name:文件名,fliename:物理路徑文件名,size:文件初始大小kb/mb/gb/tb,filegrowth:文件自動增量kb/mb/gb/tb/%,maxsize:文件可以增加到的最大大小kb/mb/gb/tb/unlimited)

alter database testdb add file
(name=N'testdb1',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb1.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup1
alter database testdb add file
(name=N'testdb2',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb2.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup2
alter database testdb add file
(name=N'testdb3',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb3.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup3
alter database testdb add file
(name=N'testdb4',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb4.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup4
alter database testdb add file
(name=N'testdb5',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb5.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup5
alter database testdb add file
(name=N'testdb6',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb6.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup6
alter database testdb add file
(name=N'testdb7',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb7.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup7
alter database testdb add file
(name=N'testdb8',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb8.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup8
alter database testdb add file
(name=N'testdb9',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb9.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup9
alter database testdb add file
(name=N'testdb10',filename=N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\testdbGroup\testdb10.ndf',size=5Mb,filegrowth=5mb)
to filegroup testdbGroup10

插入測試數據

declare @i int  
set @i=0
while @i<100000000
begin
    insert Student([name]) values(CONVERT(varchar(8000),@i)+'名稱')  
    set @i=@i +1
end

 

 

 

 

分區函數,分區方案,分區表,分區索引

1.分區函數

指定分依據區列(依據列唯一),分區數據范圍規則,分區數量,然后將數據映射到一組分區上。

創建語法: 

create partition function 分區函數名(<分區列類型>) as range [left/right] for values (每個分區的邊界值,....) 
--創建分區函數 CREATE PARTITION FUNCTION [bgPartitionFun](int) AS RANGE LEFT FOR VALUES (N'1000000', N'2000000', N'3000000', N'4000000', N'5000000', N'6000000', N'7000000', N'8000000', N'9000000', N'10000000')

然而,分區函數只定義了分區的方法,此方法具體用在哪個表的那一列上,則需要在創建表或索引是指定。 

刪除語法:

--刪除分區語法 drop partition function <分區函數名>
--刪除分區函數 bgPartitionFun drop partition function bgPartitionFun

需要注意的是,只有沒有應用到分區方案中的分區函數才能被刪除。

2.分區方案

指定分區對應的文件組。

創建語法: 

--創建分區方案語法 create partition scheme <分區方案名稱> as partition <分區函數名稱> [all]to (文件組名稱,....) 
--創建分區方案,所有分區在一個組里面 CREATE PARTITION SCHEME [bgPartitionSchema] AS PARTITION [bgPartitionFun] TO ([ByIdGroup1], [ByIdGroup1], [ByIdGroup1], [ByIdGroup1], [ByIdGroup1], [ByIdGroup1], [ByIdGroup1], [ByIdGroup1], [ByIdGroup1], [ByIdGroup1], [ByIdGroup1])

分區函數必須關聯分區方案才能有效,然而分區方案指定的文件組數量必須與分區數量一致,哪怕多個分區存放在一個文件組中。

刪除語法:

--刪除分區方案語法 drop partition scheme<分區方案名稱>
--刪除分區方案 bgPartitionSchema drop partition scheme bgPartitionSchema1

只有沒有分區表,或索引使用該分區方案是,才能對其刪除。

3.分區表

創建語法:

--創建分區表語法 create table <表名> ( <列定義> )on<分區方案名>(分區列名)
復制代碼
--創建分區表 create table BigOrder ( OrderId int identity, orderNum varchar(30) not null, OrderStatus int not null default 0, OrderPayStatus int not null default 0, UserId varchar(40) not null, CreateDate datetime null default getdate(), Mark nvarchar(300) null )on bgPartitionSchema(OrderId)
復制代碼

如果在表中創建主鍵或唯一索引,則分區依據列必須為該列。

4.分區索引

創建語法: 

--創建分區索引語法 create <索引分類> index <索引名稱> on <表名>(列名) on <分區方案名>(分區依據列名)
--創建分區索引 CREATE CLUSTERED INDEX [ClusteredIndex_on_bgPartitionSchema_635342971076448165] ON [dbo].[BigOrder] ( [OrderId] )WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [bgPartitionSchema]([OrderId])

使用分區索引查詢,可以避免多個cpu操作多個磁盤時產生的沖突。

分區表明細信息

這里的語法,我就不寫啦,自己看語句分析吧。簡單的很。。

1.查看分區依據列的指定值所在的分區 

--查詢分區依據列為10000014的數據在哪個分區上 select $partition.bgPartitionFun(2000000) --返回值是2,表示此值存在第2個分區 

2.查看分區表中,每個非空分區存在的行數

--查看分區表中,每個非空分區存在的行數 select $partition.bgPartitionFun(orderid) as partitionNum,count(*) as recordCount from bigorder group by $partition.bgPartitionFun(orderid)

3.查看指定分區中的數據記錄 

---查看指定分區中的數據記錄 select * from bigorder where $partition.bgPartitionFun(orderid)=2

結果:數據從1000001開始到200W結束

分區的拆分與合並以及數據移動

 1.拆分分區

在分區函數中新增一個邊界值,即可將一個分區變為2個。

--分區拆分 alter partition function bgPartitionFun() split range(N'1500000') --將第二個分區拆為2個分區

注意:如果分區函數已經指定了分區方案,則分區數需要和分區方案中指定的文件組個數保持對應一致。

 2.合並分區

 與拆分分區相反,去除一個邊界值即可。

--合並分區 alter partition function bgPartitionFun() merge range(N'1500000') --將第二第三分區合並

3.分區中的數據移動

 你或許會遇到這樣的需求,將普通表數據復制到分區表中,或者將分區表中的數據復制到普通表中。

 那么移動數據這兩個表,則必須滿足下面的要求。

  • 字段數量相同,對應位置的字段相同
  • 相同位置的字段要有相同的屬性,相同的類型。
  • 兩個表在一個文件組中

1.創建表時指定文件組

--創建表 create table <表名> ( <列定義> )on <文件組名>

2.從分區表中復制數據到普通表

--將bigorder分區表中的第一分區數據復制到普通表中 alter table bigorder switch partition 1 to <普通表名>

3.從普通標中復制數據到分區表中

這里要注意的是要先將分區表中的索引刪除,即便普通表中存在跟分區表中相同的索引。

--將普通表中的數據復制到bigorder分區表中的第一分區 alter table <普通表名> switch to bigorder partition 1 

分區視圖

分區視圖是先建立帶有字段約束的相同表,而約束不同,例如,第一個表的id約束為0--100W,第二表為101萬到200萬.....依次類推。

創建完一系列的表之后,用union all 連接起來創建一個視圖,這個視圖就形成啦分區視同。

很簡單的,這里我主要是說分區表,就不說分區視圖啦。。

 查看數據庫分區信息

復制代碼
SELECT OBJECT_NAME(p.object_id) AS ObjectName, i.name AS IndexName, p.index_id AS IndexID, ds.name AS PartitionScheme, p.partition_number AS PartitionNumber, fg.name AS FileGroupName, prv_left.value AS LowerBoundaryValue, prv_right.value AS UpperBoundaryValue, CASE pf.boundary_value_on_right WHEN 1 THEN 'RIGHT' ELSE 'LEFT' END AS Range, p.rows AS Rows FROM sys.partitions AS p JOIN sys.indexes AS i ON i.object_id = p.object_id AND i.index_id = p.index_id JOIN sys.data_spaces AS ds ON ds.data_space_id = i.data_space_id JOIN sys.partition_schemes AS ps ON ps.data_space_id = ds.data_space_id JOIN sys.partition_functions AS pf ON pf.function_id = ps.function_id JOIN sys.destination_data_spaces AS dds2 ON dds2.partition_scheme_id = ps.data_space_id AND dds2.destination_id = p.partition_number JOIN sys.filegroups AS fg ON fg.data_space_id = dds2.data_space_id LEFT JOIN sys.partition_range_values AS prv_left ON ps.function_id = prv_left.function_id AND prv_left.boundary_id = p.partition_number - 1 LEFT JOIN sys.partition_range_values AS prv_right ON ps.function_id = prv_right.function_id AND prv_right.boundary_id = p.partition_number WHERE OBJECTPROPERTY(p.object_id, 'ISMSShipped') = 0 UNION ALL SELECT OBJECT_NAME(p.object_id) AS ObjectName, i.name AS IndexName, p.index_id AS IndexID, NULL AS PartitionScheme, p.partition_number AS PartitionNumber, fg.name AS FileGroupName, NULL AS LowerBoundaryValue, NULL AS UpperBoundaryValue, NULL AS Boundary, p.rows AS Rows FROM sys.partitions AS p JOIN sys.indexes AS i ON i.object_id = p.object_id AND i.index_id = p.index_id JOIN sys.data_spaces AS ds ON ds.data_space_id = i.data_space_id JOIN sys.filegroups AS fg ON fg.data_space_id = i.data_space_id WHERE OBJECTPROPERTY(p.object_id, 'ISMSShipped') = 0 ORDER BY ObjectName, IndexID, PartitionNumber 

 待測試項:

1:多少數據量在一張表中是分區性能的臨界點


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM