SQLSERVER中的鬼影索引

SQLSERVER中的鬼影索引

看這篇文章以前能夠先看一下鬼影記錄html

瞭解瞭解一下SQLSERVER裏的鬼影記錄
關於鬼影記錄的翻譯一
關於鬼影記錄的翻譯二ide

當刪除表中的某一條記錄的時候,索引頁面的對應記錄並非立刻刪除,而是標記爲鬼影,當提交事務的時候才真正刪除索引記錄,性能

或者回滾事務,鬼影索引記錄纔會恢復爲正常索引記錄,這樣作的目的就是提升了性能spa

鬼影索引只會出如今非彙集索引頁,彙集索引頁是沒有鬼影索引的翻譯

 


創建環境code

 1 --ghost index record
 2 USE [pratice]
 3 GO
 4 --建表
 5 CREATE TABLE testghostindexnoncluster(id INT IDENTITY(1,1),NAME VARCHAR(20))
 6 GO
 7 --插入記錄
 8 INSERT INTO testghostindexnoncluster(name)
 9 SELECT '1' UNION ALL
10 SELECT '2'
11 GO
12 
13 CREATE  INDEX IX_testghostindexnoncluster ON testghostindexnoncluster([Id] ASC)
14 
15 SELECT * FROM [dbo].[testghostindexnoncluster]
16 GO

1 --TRUNCATE TABLE DBCCResult
2 INSERT INTO DBCCResult EXEC ('DBCC IND(pratice,testghostindexnoncluster,-1) ')
3 
4 SELECT * FROM [dbo].[DBCCResult] ORDER BY [PageType] DESC 
5 
6 DBCC TRACEON(3604,-1)
7 GO
8 DBCC PAGE([pratice],1,15663,1)  --索引頁
9 GO

正常狀況下的索引頁面,Record Type = INDEX_RECORD htm

 1 DBCC 執行完畢。若是 DBCC 輸出了錯誤信息,請與系統管理員聯繫。
 2 
 3 PAGE: (1:15663)
 4 
 5 
 6 BUFFER:
 7 
 8 
 9 BUF @0x03E53304
10 
11 bpage = 0x1A468000                   bhash = 0x00000000                   bpageno = (1:15663)
12 bdbid = 5                            breferences = 0                      bUse1 = 7847
13 bstat = 0xc0000b                     blog = 0x1212121b                    bnext = 0x00000000
14 
15 PAGE HEADER:
16 
17 
18 Page @0x1A468000
19 
20 m_pageId = (1:15663)                 m_headerVersion = 1                  m_type = 2
21 m_typeFlagBits = 0x0                 m_level = 0                          m_flagBits = 0x4000
22 m_objId (AllocUnitId.idObj) = 539    m_indexId (AllocUnitId.idInd) = 256  
23 Metadata: AllocUnitId = 72057594073251840                                 
24 Metadata: PartitionId = 72057594061062144                                 Metadata: IndexId = 2
25 Metadata: ObjectId = 1463676262      m_prevPage = (0:0)                   m_nextPage = (0:0)
26 pminlen = 13                         m_slotCnt = 2                        m_freeCnt = 8066
27 m_freeData = 135                     m_reservedCnt = 0                    m_lsn = (3046:350:12)
28 m_xactReserved = 0                   m_xdesId = (0:11665608)              m_ghostRecCnt = 0
29 m_tornBits = 0                       
30 
31 Allocation Status
32 
33 GAM (1:2) = ALLOCATED                SGAM (1:3) = ALLOCATED               
34 PFS (1:8088) = 0x60 MIXED_EXT ALLOCATED   0_PCT_FULL                      DIFF (1:6) = CHANGED
35 ML (1:7) = NOT MIN_LOGGED            
36 
37 DATA:
38 
39 
40 Slot 0, Offset 0x7a, Length 13, DumpStyle BYTE
41 
42 Record Type = INDEX_RECORD           Record Attributes =                  
43 Memory Dump @0x0849C07A
44 
45 00000000:   06010000 00b52000 00010000 00††††††††...... ......            
46 
47 Slot 1, Offset 0x6d, Length 13, DumpStyle BYTE
48 
49 Record Type = INDEX_RECORD           Record Attributes =                  
50 Memory Dump @0x0849C06D
51 
52 00000000:   06020000 00b52000 00010001 00††††††††...... ......            
53 
54 OFFSET TABLE:
55 
56 Row - Offset                         
57 1 (0x1) - 109 (0x6d)                 
58 0 (0x0) - 122 (0x7a)                 
59 
60 
61 DBCC 執行完畢。若是 DBCC 輸出了錯誤信息,請與系統管理員聯繫。
View Code


咱們刪除id=1的記錄blog

1 SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
2 GO
3 BEGIN TRAN
4 DELETE FROM  testghostindexnoncluster WHERE [id]=1
5 --ROLLBACK TRAN

再看一下索引頁面索引

1 DBCC TRACEON(3604,-1)
2 GO
3 DBCC PAGE([pratice],1,15663,1)  --索引頁
4 GO

 

 1 DATA:
 2 
 3 
 4 Slot 0, Offset 0x7a, Length 13, DumpStyle BYTE
 5 
 6 Record Type = GHOST_INDEX_RECORD     Record Attributes =                  
 7 Memory Dump @0x0849C07A
 8 
 9 00000000:   0a010000 00b52000 00010000 00††††††††...... ......            
10 
11 Slot 1, Offset 0x6d, Length 13, DumpStyle BYTE
12 
13 Record Type = INDEX_RECORD           Record Attributes =                  
14 Memory Dump @0x0849C06D
15 
16 00000000:   06020000 00b52000 00010001 00††††††††...... ......            
17 
18 OFFSET TABLE:
19 
20 Row - Offset                         
21 1 (0x1) - 109 (0x6d)                 
22 0 (0x0) - 122 (0x7a)                 
23 
24 
25 DBCC 執行完畢。若是 DBCC 輸出了錯誤信息,請與系統管理員聯繫。


能夠看到第一行記錄被標記爲Record Type = GHOST_INDEX_RECORD事務

 

咱們回滾事務

1 ROLLBACK TRAN

恢復正常了,索引頁面中第一行記錄的Record Type = INDEX_RECORD

 1 DATA:
 2 
 3 
 4 Slot 0, Offset 0x94, Length 13, DumpStyle BYTE
 5 
 6 Record Type = INDEX_RECORD           Record Attributes =                  
 7 Memory Dump @0x0849C094
 8 
 9 00000000:   06010000 00b52000 00010000 00††††††††...... ......            
10 
11 Slot 1, Offset 0x6d, Length 13, DumpStyle BYTE
12 
13 Record Type = INDEX_RECORD           Record Attributes =                  
14 Memory Dump @0x0849C06D
15 
16 00000000:   06020000 00b52000 00010001 00††††††††...... ......            
17 
18 OFFSET TABLE:
19 
20 Row - Offset                         
21 1 (0x1) - 109 (0x6d)                 
22 0 (0x0) - 148 (0x94)                 
23 
24 
25 DBCC 執行完畢。若是 DBCC 輸出了錯誤信息,請與系統管理員聯繫。

 

1 DROP TABLE testghostindexnoncluster
2 GO

 

若有不對的地方,歡迎你們拍磚o(∩_∩)o

相關文章
相關標籤/搜索