通过dbcc page来查看SQL Server表中的数据

转自 http://blog.csdn.net/sqlserverdiscovery/article/details/12622793

在SQL Server中,一般我们都会通过 select  * from 表 来查询数据,但有时候,为了探索SQL Server的各种机制,需要看到更原始的数据,或者说是希望从更为底层的方式来看数据,那有没什么办法呢?

其实,通过DBCC PAGE,我们不仅能查看表中的数据,而且还会看到很多内部存储的信息,让你了解SQL Server是如何存储这些数据的。

 

下面就讲讲如何通过DBCC PAGE来查看表中的数据:

 

[sql] view plaincopy
 
  1. --1.先建表  
  2. CREATE TABLE test(idd INT NOT NULL,name VARCHAR(10) NULL)  
  3.   
  4. INSERT INTO TEST  
  5. SELECT 1,'abcdefg'  
  6. UNION ALL  
  7. SELECT 2,'hijklmn'  
  8.   
  9.   
  10. --SELECT * FROM TEST  
  11.   
  12. SELECT *  
  13. FROM sys.tables   
  14. WHERE name = 'test'  
  15.   
  16.   
  17.   
  18. --2.查询元数据  
  19. --hobt_id : 72057594043236352  
  20. SELECT hobt_id  
  21. FROM sys.partitions  
  22. WHERE object_id = object_id('test')  
  23.   
  24.   
  25. /*  
  26. first_page :0x790500000100  
  27.   
  28. 拆分成2部分:0100和79050000  
  29.   
  30. 这2部分要翻转,也就是0001 和 00000579  
  31.   
  32. 前面表示fileId,后面是pageId,都是16机制的表示方法,  
  33.   
  34. 通过calc计算器的转换,就是10进制就是1和1401  
  35.   
  36. */  
  37. SELECT first_page    --转换值的顺序  
  38. FROM sys.system_internals_allocation_units  
  39. WHERE container_id = 72057594043236352  
  40.   
  41.   
  42.   
  43. --3.这里创建一个表,用来存放dbcc page的结果  
  44. if exists(select * from sys.tables where name = 'dbcc_page')  
  45.    drop table dbcc_page  
  46. go  
  47. create table dbcc_page  
  48. (  
  49. ParentObject varchar(500),  
  50. Object       varchar(2000),  
  51. Field        varchar(1000),  
  52. Value        nvarchar(max)  
  53. )  
  54. go  
  55.   
  56.   
  57. --4.参数分别是数据库名,fileid,pageid,显示格式  
  58. --注意:这里是在自己的电脑上实验,用的master数据库,大家不要在正是服务器上尝试  
  59. /*  
  60. --这样会报错,只能采用下面的,建一个存储过程  
  61. insert into dbcc_page(ParentObject,Object,Field,Value)  
  62. DBCC page(master,1,1401,3) with tableresults  
  63. */  
  64. if exists(select * from sys.procedures where name = 'proc_dbcc_page')  
  65.    drop procedure proc_dbcc_page  
  66. go  
  67.   
  68. create procedure proc_dbcc_page  
  69. as  
  70.   
  71. DBCC page(master,1,1401,3) with tableresults  
  72.   
  73. go  
  74.   
  75.   
  76. --5.把dbcc page的结果插入到表中  
  77. insert into dbcc_page(ParentObject,Object,Field,Value)  
  78. exec proc_dbcc_page  
  79.   
  80.   
  81. --6.查看数据  
  82. select *  
  83. from dbcc_page  
  84.   
  85.   
  86. --过滤大部分其他元数据,选出表test中的数据,与最上面的数据相一致  
  87. select OBJECT,  
  88.        Field,  
  89.        value  
  90. from dbcc_page  
  91. where Object like 'Slot%Column%'  
  92. /*  
  93. OBJECT                                                 Field    value  
  94. Slot 0 Column 1 Offset 0x4 Length 4 Length (physical) 4 idd     1  
  95. Slot 0 Column 2 Offset 0xf Length 7 Length (physical) 7 name    abcdefg  
  96. Slot 1 Column 1 Offset 0x4 Length 4 Length (physical) 4 idd     2  
  97. Slot 1 Column 2 Offset 0xf Length 7 Length (physical) 7 name    hijklmn  
  98. */  


 

 

 除了上面所解析的1401页外,我们还可以查看其他的页:

[sql] view plaincopy
 
  1. /*  
  2. DBCC ind:  
  3.   
  4. DBCC IND(  
  5.     ['database name'|database id],   
  6.     tablename,  
  7.     indexid,   
  8.     -1 shows all indexes and IAMs, -2 just show IAMs  
  9. )  
  10.   
  11. PageFID: 页所在的文件号  
  12. PagePID: 数据所在文件内的页号  
  13. IndexID: 在sys.indexes中的index_id,  -1表示所有索引页和IAM页,-2只显示IAM页  
  14. PageType:表示页的类型,1是数据页,2是索引页,10是保存页本身的IAM页。  
  15. IndexLevel: 是IAM结构的级别。如果0,那么这是索引的叶级别页  
  16. */  
  17.   
  18. select name,  
  19.        index_id  -- 0  
  20. from sys.indexes  
  21. where object_id = object_id('test')  
  22.   
  23.   
  24. --1.创建表  
  25. if exists(select * from sys.tables where name = 'dbcc_ind')  
  26.    drop table dbcc_ind  
  27. go  
  28.   
  29. create table dbcc_ind  
  30. (  
  31.     PageFID numeric(20),  
  32.     PagePID numeric(20),  
  33.       
  34.     IAMFID numeric(20),  
  35.     IAMPID numeric(20),  
  36.       
  37.     ObjectID numeric(20),  
  38.     IndexID numeric(20),  
  39.     PartitionNumber numeric(20),  
  40.     PartitionID numeric(20),  
  41.       
  42.     iam_chain_type nvarchar(100),  
  43.       
  44.     PageType numeric(20),  
  45.     IndexLevel numeric(20),  
  46.       
  47.     NextPageFID numeric(20),  
  48.     NextPagePID numeric(20),  
  49.     PrevPageFID numeric(20),  
  50.     PrevPagePID numeric(20)  
  51. )  
  52. go  
  53.   
  54. --2.建存储过程  
  55. if exists(select * from sys.procedures)  
  56.    drop procedure proc_dbcc_ind  
  57. go  
  58.   
  59. create procedure proc_dbcc_ind  
  60. as  
  61.   
  62. dbcc ind(master,test,0)   
  63.   
  64. go  
  65.   
  66.   
  67. insert into dbcc_ind  
  68. exec proc_dbcc_ind  
  69.   
  70.   
  71. --3.一共2页,分别是IAM页1402,数据页1401  
  72. select PageFID,  
  73.        PagePID,  
  74.        OBJECTID,  
  75.        IndexID,  
  76.        PartitionNumber,  
  77.        PartitionID,  
  78.        iam_chain_type,  
  79.        PageType,  
  80.        IndexLevel,  
  81.          
  82.        NextPageFID,  --下一页的文件id  
  83.        NextPagePID,  --下一页的页id  
  84.        PrevPageFID,    
  85.        PrevPagePID  
  86. from dbcc_ind  


 

posted @ 2014-04-25 01:53  princessd8251  阅读(217)  评论(0)    收藏  举报