您好,登錄后才能下訂單哦!
今天小編給大家分享的是創(chuàng)建、查看分區(qū)表的Metadata的詳細(xì)介紹,相信大部分人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,話不多說,一起往下看吧。
未分區(qū)的表,只能存儲在一個FileGroup中;對table進(jìn)行分區(qū)后,每一個分區(qū)都存儲在一個FileGroup中。表分區(qū)是將邏輯上一個完整的表,按照特定的字段拆分成Partition set,分散到(相同或不同的)FileGroup中,每一個Partition在FileGroup中都獨立存儲,每一個parititon都屬于唯一的表對象,每一個Partition 都有唯一的ID。
在創(chuàng)建表時,使用On 子句指定table存儲的邏輯位置:
On filegroup | "default" 表示邏輯存儲位置是單一的FileGroup;
ON partition_scheme_name ( partition_column_name ) 表示邏輯存儲位置是多個FileGroup,按照partition_column_name將table拆分成多個partition,每一個partition都存儲在一個指定的Filegroup中。
CREATE TABLE [schema_name . ] table_name ( <column_definition> )[ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] [ WITH ( <table_option> [ ,...n ] ) ][ ; ]
Partition的含義就是table的一部分邏輯存儲空間。
跟邏輯存儲空間相對應(yīng)的是物理存儲空間,物理存儲空間是由File指定的,F(xiàn)ileGroup是File的集合,每一個File都屬于唯一的FileGroup。將table的存儲空間拆分到不同的FileGroup中,邏輯上是將table的存儲管理體系增加了一層 Partition,介于Table和FileGroup中間,Table的數(shù)據(jù)存儲在Partition,Partition存儲在FileGroup中,F(xiàn)ileGroup管理著File,F(xiàn)ile是實際存儲data的物理文件。
table為什么要增加Parition?因為FileGroup是所有的Table共享,Partition是由一個table獨占,每一個Partition都唯一屬于一個table。這樣,對某個parititon進(jìn)行操作,而不影響table的其他parition,也不會影響其他table。
一:創(chuàng)建分區(qū)表的步驟
Step1, 創(chuàng)建分區(qū)函數(shù)
分區(qū)函數(shù)的作用是提供分區(qū)字段的類型和分區(qū)的邊界值
CREATE PARTITION FUNCTION [pf_int](int) AS RANGE LEFT FOR VALUES (10, 20)
pf_int 的含義是按照int類型分區(qū),分區(qū)的邊界值是10,20,left表示邊界值屬于左邊界。兩個邊界值能夠分成三個分區(qū),別是(-infinite,10],(10,20],(20,+infinite)。
Step2,創(chuàng)建分區(qū)scheme
分區(qū)scheme的作用是為Parition分配FileGroup,Partition Scheme和FileGroup在邏輯上等價,都是數(shù)據(jù)存儲的邏輯空間,只不過Partition Scheme指定的是多個FileGroup。
CREATE PARTITION SCHEME [ps_int] AS PARTITION [pf_int] TO ([PRIMARY], [db_fg1], [db_fg1])
不管是在不同的FileGroup中,還是在相同的FileGroup中,分區(qū)都是獨立存儲的。
Step3,創(chuàng)建分區(qū)表
創(chuàng)建分區(qū)表,實際上是使用on子句指定table存儲的邏輯位置。
create table dbo.dt_test ( ID int, code int)on [ps_int] (id)
二,查看Partition的Metadata
1, 查看partition function
select *from sys.partition_functions
查看partition function定義的邊界值
select * from sys.partition_range_values
查看partition function 定義的parmeter,這個Parmeter是一個data type,system_type_id標(biāo)識該data type。
select *from sys.partition_parameters
根據(jù)system_type_id查看data type
select * from sys.typeswhere system_type_id=56
2, 查看Partition scheme和 filegroup
select *from sys.partition_schemes
data_space_ID 是數(shù)據(jù)空間ID,每一個Parition Scheme都有一個ID。
select *from sys.filegroups
data_space_ID 是數(shù)據(jù)空間ID,每一個FileGroup都有一個ID。
3, 查看Data Space
select *from sys.data_spaces
Each filegroup has one row, and each partition scheme has one row. If the row refers to a partition scheme, data_space_id can be joined with sys.partition_schemes.data_space_id. If the row referes to a file, data_space_id can be joined with sys.filegroups.data_space_id.
sys.data_spaces 是sys.filegroups 和 sys.partition_schemes 結(jié)果的交集,充分說明,partition scheme和filegroup都是數(shù)據(jù)存儲的邏輯空間。
4,partition scheme和filegroup 之間的關(guān)系
一個partition scheme能夠使用多個filegroup存儲數(shù)據(jù),同時一個filegroup可以被多個partition scheme使用,partition scheme和filegroup 之間的關(guān)系是many-to-many,sql server使用 sys.destination_data_spaces 提供partition scheme和filegroup 之間的關(guān)系。
select *from sys.destination_data_spaces
partition_scheme_id 是 sys.partition_schemes的data_space_id,標(biāo)識一個Partition Scheme。
data_space_id 是 sys.filegroups的data_space_id,標(biāo)識partition scheme使用的filegroup。
destination_id 是 Partition number。Partition Number是一個數(shù)字,從1開始,標(biāo)識table的parition的編號。表的partition number從左向右開始編號,最左邊的分區(qū),其partition number 是1。
5,查看分區(qū)的信息
select *from sys.partitionswhere object_id=object_id('dbo.dt_test')
partition_id:每一個partition都有一個ID,唯一標(biāo)識該分區(qū)。
rows:分區(qū)包含的數(shù)據(jù)行數(shù)目
data_compression和data_compression_desc:partition 使用的數(shù)據(jù)壓縮類型
6,查看分區(qū)的統(tǒng)計信息
select *from sys.dm_db_partition_statswhere object_id=object_id('dbo.dt_test')
used_page_count | bigint | Total number of pages used for the partition. Computed as in_row_used_page_count + lob_used_page_count +row_overflow_used_page_count. |
reserved_page_count | bigint | Total number of pages reserved for the partition. Computed as in_row_reserved_page_count + lob_reserved_page_count +row_overflow_reserved_page_count. |
row_count | bigint | The approximate number of rows in the partition. |
sys.dm_db_partition_stats displays information about the space used to store and manage in-row data, LOB data, and row-overflow data for all partitions in a database. One row is displayed per partition.
The counts on which the output is based are cached in memory or stored on disk in various system tables.
In-row data, LOB data, and row-overflow data represent the three allocation units that make up a partition. The sys.allocation_units catalog view can be queried for metadata about each allocation unit in the database.
If a heap or index is not partitioned, it is made up of one partition (with partition number = 1); therefore, only one row is returned for that heap or index. Thesys.partitions catalog view can be queried for metadata about each partition of all the tables and indexes in a database.
The total count for an individual table or an index can be obtained by adding the counts for all relevant partitions.
以上就是創(chuàng)建、查看分區(qū)表的Metadata的方法介紹,詳細(xì)使用情況還得要大家自己使用過才能知道具體要領(lǐng)。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。