前言
在SQL Server中,如何找到一張表或某個索引擁有那些頁面(page)呢? 有時候,我們在分析和研究(例如,死鎖分析)的時候還真有這樣的需求,那么如何做呢? SQL Server 2012提供了一個無文檔的DMF(sys.dm_db_database_page_allocations)可以實(shí)現(xiàn)我們的需求,sys.dm_db_database_page_allocations有下面幾個參數(shù):
對于大表而言,如果選擇“DETAILED”參數(shù),則消耗的資源和時間非常長,這個時候非常有必要選擇“LIMITED”參數(shù)。
為了更好的理解sys.dm_db_database_page_allocations輸出的數(shù)據(jù),其實(shí)我們有必要簡單了解、回顧一下SQL Server中數(shù)據(jù)存儲的相關(guān)知識點(diǎn)。 這就涉及到頁(Page)和區(qū)(Extent)的概念了。SQL Server中數(shù)據(jù)存儲的基本單位是頁,磁盤I/O操作在頁級執(zhí)行。也就是說,SQL Server讀取或?qū)懭霐?shù)據(jù)的最小單位就是以8 KB為單位的頁。
區(qū)是管理空間的基本單位。 一個區(qū)是8個物理上連續(xù)的頁的集合(64KB),所有頁都存儲在區(qū)中。區(qū)用來有效地管理頁所有頁都存儲在區(qū)中。 SQL Server中有兩種類型的區(qū):
SQL Server中頁也有很多類型,具體參考下面表格。
注意事項(xiàng):有些Page Type比較少見,暫時有些資料沒有補(bǔ)充完善
| PAGE_TYPE | 頁類型 | 頁類型碼 | 描述 |
| 1 | Data Page | DATA_PAGE | 數(shù)據(jù)頁(Data Page)用來存放數(shù)據(jù) l 堆中的數(shù)據(jù)頁 l 聚集索引中“葉子“頁 |
| 2 | Index Page | INDEX_PAGE | 索引頁(Index Page),聚集索引的非葉子節(jié)點(diǎn)和非聚集索引的所有索引記錄 |
| 3 | Text Mixed Page | TEXT_MIX_PAGE | 一個文本頁面,其中包含小塊的LOB值以及text tree的內(nèi)部,這些可以在索引或堆的同一分區(qū)中的LOB值之間共享。 A text page that holds small chunks of LOB values plus internal parts of text tree. These can be shared between LOB values in the same partition of an index or heap. |
| 4 | Text Tree Page | TEXT_TREE_PAGE | A text page that holds large chunks of LOB values from a single column value |
| 7 | Sort Page | | 在排序操作期間存儲中間結(jié)果的頁面 |
| 8 | Global Allocation Map Page | GAM_PAGE | GAM在數(shù)據(jù)文件中第三個頁上,文件和頁的編號為(1:2),它用bit位來標(biāo)識相應(yīng)的區(qū)(extents)是否已經(jīng)被分配。它差不多能標(biāo)識約64000個區(qū)(8k pages * 8 bits per byte),也就是4G的空間,如果數(shù)據(jù)空間超過4G,那么數(shù)據(jù)庫會用另外一個GAM頁來標(biāo)識下一個4G空間 Bit=1: 標(biāo)識當(dāng)前的區(qū)是空閑的,可以用來分配 Bit=0: 標(biāo)識當(dāng)前的區(qū)已經(jīng)被數(shù)據(jù)使用了 |
| 9 | Shared Global Allocation Map Page | SGAM_PAGE | SGAM在數(shù)據(jù)文件的第四個頁上,文件和頁編號為(1:3),它的結(jié)構(gòu)和GAM是一樣的,區(qū)別在于Bit位的含義不同: Bit=1:區(qū)是混合區(qū),且區(qū)內(nèi)至少有一個頁是可以被用來分配的 Bit=0:區(qū)是統(tǒng)一區(qū), 或者是混合區(qū)但是區(qū)內(nèi)所有的頁都是在被使用的 |
| 10 | Index Allocation Map Page | IAM_PAGE | 表或索引所使用的區(qū)的信息。 |
| 11 | Page Free Space Page | PFS_PAGE | 存儲本數(shù)據(jù)文件里所有頁分配和頁的可用空間的信息 |
| 13 | Boot Page | BOOT_PAGE | 包含有關(guān)數(shù)據(jù)庫的相關(guān)信息。 數(shù)據(jù)庫中有且只有一個。它位于文件1中的第9頁。 |
| 15 | File header page | FILEHEADER_PAGE | 文件標(biāo)題頁。 包含有關(guān)文件的信息。 每個文件一個,文件的第0頁。 |
| 16 | Differential Changed Map | DIFF_MAP_PAGE | 自最后一條BACKUP DATABASE語句之后更改的區(qū)的信息 |
| 17 | Bulk Changed Map | | 自最后一條BACKUP LOG語句之后的大容量操作鎖修改的區(qū)的信息 |
| 18 | | | a page that's be deallocated by during a repair operation |
| 19 | | | the temporary page that (or DBCC INDEXDEFRAG) uses when working on an index |
| 20 | | | a page pre-allocated as part of a bulk load operation, which will eventually be formatted as a ‘real' page |
另外,關(guān)于sys.dm_db_database_page_allocations的輸出字段信息如下所示(搜索相關(guān)資料結(jié)合自己的理解,如果錯誤,敬請指出):
| 字段 | 中文字段描述 | 英文描述 |
| database_id | 數(shù)據(jù)庫ID | ID of the database |
| object_id | 表或視圖對象的ID | Object ID For the table or view |
| index_id | 索引ID | ID for the index |
| partition_id | 索引的分區(qū)號 | Partition number for the index |
| rowset_id | 索引的Partition ID | Partition ID for the index |
| allocation_unit_id | 分配單元的 ID | ID of the allocation unit |
| allocation_unit_type | 分配單元的類型 | Type of allocation unit |
| allocation_unit_type_desc | 分配單元的類型描述 | Description for the allocation unit |
| data_clone_id | | ? |
| clone_state | | ? |
| clone_state_desc | | ? |
| extent_file_id | 區(qū)的文件ID | File ID of the extend |
| extent_page_id | 區(qū)的文件ID | Page ID for the extend |
| allocated_page_iam_file_id | 與頁面關(guān)聯(lián)的索引分配映射頁面的文件ID | File ID for the index allocation map page associate to the page |
| allocated_page_iam_page_id | 與頁面關(guān)聯(lián)的索引分配映射頁面的頁面ID | Page ID for the index allocation map page associated to the page |
| allocated_page_file_id | 分配頁面的File ID | File ID of the allocated page |
| allocated_page_page_id | 分配頁面的Page ID | Page ID for the allocated page |
| is_allocated | 該頁是否被分配出去了 | Indicates whether a page is allocated |
| is_iam_page | 是否為IAM頁 | Indicates whether a page is the index allocation page |
| is_mixed_page_allocation | 是否分配的混合頁面 | Indicates whether a page is allocated |
| page_free_space_percent | 頁面的空閑比例 | Percentage of space free on the page |
| page_type | 頁面的類型(數(shù)字描述) | Description of the page type |
| page_type_desc | 頁面的類型描述 | |
| page_level | 頁的層數(shù) | |
| next_page_file_id | 下一個頁的 Fiel ID | File ID for the next page |
| next_page_page_id | 下一個頁的Page ID | Page ID for the next page |
| previous_page_file_id | 前一個頁的File ID | File ID for the previous page |
| previous_page_page_id | 前一個頁的Page ID | Page ID for the previous Page |
| is_page_compressed | 頁是否壓縮 | Indicates whether the page is compressed |
| has_ghost_records | 是否存虛影記錄記錄 | Indicates whether the page have ghost records |
簡單了解了上面知識點(diǎn)后,我們在使用這個DMF找出表或索引相關(guān)的頁面,基本上可以讀懂這些輸出信息了。
USE AdventureWorks2014GOSELECT DB_NAME(pa.database_id) AS [database_name] , OBJECT_NAME(pa.object_id) AS [table_name] , id.name AS [index_name] , pa.partition_id AS [partition_id], pa.is_allocated AS [is_allocated], pa.allocated_page_file_id AS [file_id] , pa.allocated_page_page_id AS [page_id] , pa.page_type_desc , pa.page_level , pa.previous_page_page_id AS [previous_page_id] , pa.next_page_page_id AS [next_page_id] , pa.is_mixed_page_allocation AS [is_mixed_page_allocation], pa.is_iam_page AS [is_iam_page], pa.allocation_unit_id AS [allocation_unit_id], pa.has_ghost_records AS [has_ghost_records]FROM sys.dm_db_database_page_allocations(DB_ID('AdventureWorks2014'), OBJECT_ID('TestDeadLock'), NULL, NULL, 'DETAILED') pa LEFT OUTER JOIN sys.indexes id ON id.object_id = pa.object_id AND id.index_id = pa.index_idORDER BY page_level DESC , is_allocated DESC , previous_page_page_id;參考資料:
https://www.sqlskills.com/blogs/paul/inside-the-storage-engine-anatomy-of-a-page/
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選