---
title: "SQLserver数据文件(MDF)的页面文件头结构剖析"
author: "csstome"
date: "2007-12-19T04:13:00Z"
tags: ["Go", "SQL", "Windows", "数据结构"]
slug: "sqlserver-mdf"
draft: false
lastmod: "2007-12-19T04:13:00Z"
id: 1033163
url: "https://csstome.iteye.com/blog/1033163"
---

先执行一下以下SQL语句，我的测试环境为SQL2005

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)dbcctraceon(3604)

go  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)dbccpage(master,1,0,2)

可以看到MDF文件的一些物理结构信息，其中包括重要的头96个字节。也就是第一个页面的文件头。

........

PAGE HEADER:

Page @0x03FA0000

m\_pageId = (1:0) m\_headerVersion = 1 m\_type = 15  
m\_typeFlagBits = 0x0 m\_level = 0 m\_flagBits = 0x8  
m\_objId (AllocUnitId.idObj) = 99 m\_indexId (AllocUnitId.idInd) = 0 Metadata: AllocUnitId = 6488064  
Metadata: PartitionId = 0 Metadata: IndexId = 0 Metadata: ObjectId = 99  
m\_prevPage = (0:0) m\_nextPage = (0:0) pminlen = 0  
m\_slotCnt = 1 m\_freeCnt = 7937 m\_freeData = 3059  
m\_reservedCnt = 0 m\_lsn = (149:448:1) m\_xactReserved = 0  
m\_xdesId = (0:0) m\_ghostRecCnt = 0 m\_tornBits = -1073741694

........

DATA:

Memory Dump @0x62FEC000

62FEC000: 010f0000 08000000 00000000 00000000 †................  
62FEC010: 00000000 00000100 63000000 011ff30b †........c.......  
62FEC020: 00000000 01000000 95000000 c0010000 †................  
62FEC030: 01000000 00000000 00000000 820000c0 †................  
62FEC040: 00000000 00000000 00000000 00000000 †................  
62FEC050: 00000000 00000000 00000000 00000000 †................

以上蓝色的文字就是文件头的一些信息。如果这些信息损坏将会造成严重的后果。

经过简单的逐个字节分析，中间借助了windows计算器和c#的BitConverter.GetBytes函数。得出了如下文件结构图，其中每行4个字节，一共分析了文件头的前64个字节。

     00:0F m\_headerVersion m\_type m\_typeFlagBits m\_level m\_flagBits m\_indexId m\_prevPage(2) m\_prevPage(1) pminlen 10:1F m\_nextPage(2) m\_nextPage(1) m\_slotCnt AllocUnitId.idObj m\_freeCnt m\_freeData 20:2F m\_pageId（2） m\_pageId（1） m\_reservedCnt m\_lsn(1) m\_lsn(2) 30:3F m\_lsn(3) m\_xactReserved m\_xdesId(2) m\_xdesId(1) m\_ghostRecCnt m\_tornBits

在数据库的头96个字节中第0x40开始直道0x5F应该都是0。

我发现只有测试页的m\_pageId 的冒号前面的数字不为1时才在0x40到0x5f写入数据。但是具体代表什么还没有看出来。

姑且认为数据库第一个页面的0x00-0x3f就如上图所示，0x40-0x5f都为0（不正确的话请纠正一下）

这张图有什么用呢，如果你理解了上述参数的意义，用二进制编辑器打开一个头文件损坏的mdf文件就有可能恢复这个已经损坏的数据库。

偶不是dba也不是专业恢复数据的，只是个普通的开发人员，怎么恢复还请有经验人士补充一下。

**有情提醒，这些东西非常危险，请不要随意测试，最好找一个没用的数据库来研究。**

**参数的意义**

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_pageId  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisidentifiesthefilenumberthepageispartofandthepositionwithinthefile.(1:143)meanspage143infile1.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_headerVersion  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisisthepageheaderversion.Sinceversion7.0thisvaluehasalwaysbeen1.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_typea  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisisthepagetype.Thevaluesyou'relikelytoseeare:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)1-datapage.Thisholdsdatarecordsinaheaporclusteredindexleaf-level.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)2-indexpage.Thisholdsindexrecordsintheupperlevelsofaclusteredindexandalllevelsofnon-clusteredindexes.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)3-textmixpage.AtextpagethatholdssmallchunksofLOBvaluesplusinternalpartsoftexttree.ThesecanbesharedbetweenLOBvaluesinthesamepartitionofanindexorheap.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)4-texttreepage.AtextpagethatholdslargechunksofLOBvaluesfromasinglecolumnvalue.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)7-sortpage.Apagethatstoresintermediateresultsduringasortoperation.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)8-GAMpage.HoldsglobalallocationinformationaboutextentsinaGAMinterval(everydatafileissplitinto4GBchunks-thenumberofextentsthatcanberepresentedinabitmaponasingledatabasepage).Basicallywhetheranextentisallocatedornot.GAM=GlobalAllocationMap.Thefirstoneispage2ineachfile.Moreontheseinalaterpost.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)9-SGAMpage.HoldsglobalallocationinformationaboutextentsinaGAMinterval.Basicallywhetheranextentisavailableforallocatingmixed-pages.SGAM=SharedGAM.thefirstoneispage3ineachfile.Moreontheseinalaterpost.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)10-IAMpage.HoldsallocationinformationaboutwhichextentswithinaGAMintervalareallocatedtoanindexorallocationunit,inSQLServer2000and2005respectively.IAM=IndexAllocationMap.Moreontheseinalaterpost.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)11-PFSpage.HoldsallocationandfreespaceinformationaboutpageswithinaPFSinterval(everydatafileisalsosplitintoapprox64MBchunks-thenumberofpagesthatcanberepresentedinabyte-maponasingledatabasepage.PFS=PageFreeSpace.Thefirstoneispage1ineachfile.Moreontheseinalaterpost.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)13-bootpage.Holdsinformationaboutthedatabase.There'sonlyoneoftheseinthedatabase.It'spage9infile1.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)15-fileheaderpage.Holdsinformationaboutthefile.There'soneperfileandit'spage0inthefile.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)16-diffmappage.HoldsinformationaboutwhichextentsinaGAMintervalhavechangedsincethelastfullordifferentialbackup.Thefirstoneispage6ineachfile.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)17-MLmappage.HoldsinformationaboutwhichextentsinaGAMintervalhavechangedwhileinbulk-loggedmodesincethelastbackup.Thisiswhatallowsyoutoswitchtobulk-loggedmodeforbulk-loadsandindexrebuildswithoutworryingaboutbreakingabackupchain.Thefirstoneispage7ineachfile.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_typeFlagBits  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisismostlyunused.Fordataandindexpagesitwillalwaysbe4.Forallotherpagesitwillalwaysbe0-exceptPFSpages.IfaPFSpagehasm\_typeFlagBitsof1,thatmeansthatatleastoneofthepagesinthePFSintervalmappedbythePFSpagehasatleastoneghostrecord.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_level  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisisthelevelthatthepageispartofintheb-tree.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Levelsarenumberedfrom0attheleaf-levelandincreasetothesingle-pagerootlevel(i.e.thetopoftheb-tree).  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)InSQLServer2000,theleaflevelofaclusteredindex(withdatapages)waslevel0,andthenextlevelup(withindexpages)wasalsolevel0.Thelevelthenincreasedtotheroot.SotodeterminewhetherapagewastrulyattheleaflevelinSQLServer2000,youneedtolookatthem\_typeaswellasthem\_level.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Forallpagetypesapartfromindexpages,thelevelisalways0.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_flagBits  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisstoresanumberofdifferentflagsthatdescribethepage.Forexample,0x200meansthatthepagehasapagechecksumonit(asourexamplepagedoes)and0x100meansthepagehastorn-pageprotectiononit.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)SomebitsarenolongerusedinSQLServer2005.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_objId  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_indexId  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)InSQLServer2000,theseidentifiedtheactualrelationalobjectandindexIDstowhichthepageisallocated.InSQLServer2005thisisnolongerthecase.Theallocationmetadatatotallychangedsotheseinsteadidentifywhat'scalledtheallocationunitthatthepagebelongsto(I'lldoanotherpostthatdescribestheselatertoday).  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_prevPage  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_nextPage  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thesearepointerstothepreviousandnextpagesatthisleveloftheb-treeandstore6-bytepageIDs.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thepagesineachlevelofanindexarejoinedinadoubly-linkedlistaccordingtothelogicalorder(asdefinedbytheindexkeys)oftheindex.Thepointersdonotnecessarilypointtotheimmediatelyadjacentphysicalpagesinthefile(becauseoffragmentation).  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thepagesontheleft-handsideofab-treelevelwillhavethem\_prevPagepointerbeNULL,andthoseontheright-handsidewillhavethem\_nextPagebeNULL.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Inaheap,orifanindexonlyhasasinglepage,thesepointerswillbothbeNULLforallpages.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)pminlen  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisisthesizeofthefixed-lengthportionoftherecordsonthepage.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_slotCnt  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisisthecountofrecordsonthepage.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_freeCnt  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisisthenumberofbytesoffreespaceinthepage.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_freeData  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisistheoffsetfromthestartofthepagetothefirstbyteaftertheendofthelastrecordonthepage.Itdoesn'tmatterifthereisfreespacenearertothestartofthepage.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_reservedCnt  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisisthenumberofbytesoffreespacethathasbeenreservedbyactivetransactionsthatfreedupspaceonthepage.Itpreventsthefreespacefrombeingusedupandallowsthetransactionstoroll-backcorrectly.There'saverycomplicatedalgorithmforchangingthisvalue.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_lsn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ThisistheLogSequenceNumberofthelastlogrecordthatchangedthepage.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_xactReserved  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisistheamountthatwaslastaddedtothem\_reservedCntfield.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_xdesId  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ThisistheinternalIDofthemostrecenttransactionthataddedtothem\_reservedCntfield.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_ghostRecCnt  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Theisthecountofghostrecordsonthepage.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)m\_tornBits  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Thisholdseitherthepagechecksumorthebitsthatweredisplacedbythetorn-pageprotectionbits-dependingonwhatformofpageprotectionisturndeonforthedatabase.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)