代码改变世界

YourSQLDba将数据库置于紧急模式的原因浅析

2013-08-10 21:20  潇湘隐者  阅读(1227)  评论(0编辑  收藏  举报

  从SQL SERVER 2000 上迁移了一个数据库到SQL SERVER 2008 R2上,暂且用DataBaseName代替迁移的真实的数据库名(后面的资料也会将数据库真实的名字用DataBaseName代替),迁移过程中也没有啥问题,配置了YourDataBase,作业“YourSQLDba_FullBackups_And_Maintenance”每天凌晨12点整运行,结果第一天晚上YourDatabase将数据库DataBaseName置于紧急模式,检查错误日志情况如下:

错误现象:

  YourSQLDba对数据库做一致性检查时,发现有错误,所以它将该数据库置于紧急模式,具体内容如下所

clipboard

查看YourSQLDba的具体错误日志信息

clipboard[4]

 

单击Action字段,看到的内容如下(太多内容,部分省略)

错误日志具体内容
  1. <Exec>
  2.   <ctx>yMaint.IntegrityTesting</ctx>
  3.   <cmd>DBCC checkDb('DataBaseName')</cmd>
  4.   <err>Error 2508, Severity 16, level 3 : The In-row data RSVD page count for object "bMaterialIn79", index ID 0, partition ID 4563097419776, alloc unit ID 4563097419776(type In-row data) is incorrect. Run DBCC UPDATEUSAGE.</err>
  5.   <err>Error 2508, Severity 16, level 3 : The In-row data RSVD page count for object "bMaterialIn93", index ID 0, partition ID 70442742317056, alloc unit ID 70442742317056(type In-row data) is incorrect. Run DBCC UPDATEUSAGE.</err>
  6.   <msg>DBCC results for 'LibraryMS'.</msg>
  7.   <msg>Service Broker Msg 9675, State 1: Message Types analyzed: 14.</msg>
  8.   <msg>Service Broker Msg 9676, State 1: Service Contracts analyzed: 6.</msg>
  9.   <msg>Service Broker Msg 9667, State 1: Services analyzed: 3.</msg>
  10.   <msg>Service Broker Msg 9668, State 1: Service Queues analyzed: 3.</msg>
  11.   <msg>Service Broker Msg 9669, State 1: Conversation Endpoints analyzed: 0.</msg>
  12.   <msg>Service Broker Msg 9674, State 1: Conversation Groups analyzed: 0.</msg>
  13.   <msg>Service Broker Msg 9670, State 1: Remote Service Bindings analyzed: 0.</msg>
  14.   <msg>Service Broker Msg 9605, State 1: Conversation Priorities analyzed: 0.</msg>
  15.   <msg>DBCC results for 'sys.sysrscols'.</msg>
  16.   <msg>There are 6701 rows in 63 pages for object "sys.sysrscols".</msg>
  17.   <msg>DBCC results for 'sys.sysrowsets'.</msg>
  18.   <msg>There are 819 rows in 12 pages for object "sys.sysrowsets".</msg>
  19.   <msg>DBCC results for 'sys.sysallocunits'.</msg>
  20.   <msg>There are 848 rows in 17 pages for object "sys.sysallocunits".</msg>
  21.   
  22.   .................................................................................
  23.   .................................................................................
  24.  
  25.   <msg>DBCC results for 'bMaterialIn79'.</msg>
  26.   <msg>There are 1346 rows in 18 pages for object "bMaterialIn79".</msg>
  27.   <msg>CHECKDB found 0 allocation errors and 1 consistency errors in table 'bMaterialIn79'(object ID 69627341).</msg>
  28.  
  29.    ...............................................................................
  30.    ...............................................................................
  31.  
  32.   <msg>DBCC results for 'bMaterialIn93'.</msg>
  33.   <msg>There are 2162 rows in 23 pages for object "bMaterialIn93".</msg>
  34.   <msg>CHECKDB found 0 allocation errors and 1 consistency errors in table 'bMaterialIn93'(object ID 1074870946).</msg>
  35.   
  36.   <msg>CHECKDB found 0 allocation errors and 2 consistency errors in database 'DataBaseName'.</msg>
  37.   <msg>DBCC execution completed. If DBCC printed error messages, contact your system administrator.</msg>
  38. </Exec>

对数据库执行DBCC  CHECKDB('DataBaseName')得到的内容跟YourSQLDba的错误日志信息一致

DCC CHECKDB
  1. DBCC results for 'DataBaseName'.
  2. Service Broker Msg 9675, State 1: Message Types analyzed: 14.
  3. Service Broker Msg 9676, State 1: Service Contracts analyzed: 6.
  4. Service Broker Msg 9667, State 1: Services analyzed: 3.
  5. Service Broker Msg 9668, State 1: Service Queues analyzed: 3.
  6. Service Broker Msg 9669, State 1: Conversation Endpoints analyzed: 0.
  7. Service Broker Msg 9674, State 1: Conversation Groups analyzed: 0.
  8. Service Broker Msg 9670, State 1: Remote Service Bindings analyzed: 0.
  9. Service Broker Msg 9605, State 1: Conversation Priorities analyzed: 0.
  10. DBCC results for 'sys.sysrscols'.
  11. There are 6701 rows in 63 pages for object "sys.sysrscols".
  12. DBCC results for 'sys.sysrowsets'.
  13. There are 819 rows in 12 pages for object "sys.sysrowsets".
  14.  
  15. ...................................................................
  16. ..................................................................
  17.  
  18. DBCC results for 'bMaterialIn79'.
  19. Msg 2508, Level 16, State 3, Line 1
  20. The In-row data RSVD page count for object "bMaterialIn79", index ID 0, partition ID 4563097419776, alloc unit ID 4563097419776(type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
  21. There are 1346 rows in 18 pages for object "bMaterialIn79".
  22. CHECKDB found 0 allocation errors and 1 consistency errors in table 'bMaterialIn79'(object ID 69627341).
  23.  
  24. .....................................................................
  25. .....................................................................
  26.  
  27.  
  28. The In-row data RSVD page count for object "bMaterialIn93", index ID 0, partition ID 70442742317056, alloc unit ID 70442742317056(type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
  29. There are 2162 rows in 23 pages for object "bMaterialIn93".
  30. CHECKDB found 0 allocation errors and 1 consistency errors in table 'bMaterialIn93'(object ID 1074870946).
  31. DBCC results for 'bMaterialApply40'.
  32.  
  33. ....................................................................
  34. ....................................................................
  35.  
  36.  
  37. CHECKDB found 0 allocation errors and 2 consistency errors in database 'DataBaseName'.
  38. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

分析原因:

这些错误提示数据页在保留空间中不正确的值,在SQL 2000中,这些错误也有可能是数据条目或者数据页的数目跟索引或表中记录的不一致所导致的。CheckDB 不会修复类似错误,在SQL 2005 中CheckDB只会给出一个警告信息。这不是什么严重的错误,按照提示运行DBCC UPDATEUSAGE行了,这通常发生在SQL 2000升级为2005/2008后,在SQL 2005/2008中一般不会遇到。刚好我这个数据库DataBaseName是从SQL 2000升级到SQL 2008,其实这确实不是一个什么严重错误,但是YourSQLDba意识到了“严重性”将数据库出于紧急模式,不能让系统或用户继续操作该数据库了,那么接下来用DBCC UPDATEUSAGE 解决该问题。

Code Snippet
  1. DBCC UPDATEUSAGE(LibraryMS,"dbo.bMaterialIn79");
  2. GO
  3.  
  4. DBCC UPDATEUSAGE: Usage counts updated for table 'bMaterialIn79'(index 'bMaterialIn79', partition 1):
  5.         USED pages(In-row Data): changed from (21) to (19) pages.
  6.         RSVD pages(In-row Data): changed from (-107) to (73) pages.
  7. DBCC UPDATEUSAGE: Usage counts updated for table 'bMaterialIn79'(index 'PK_bMaterialIn79', partition 1):
  8.         RSVD pages(In-row Data): changed from (8) to (17) pages.
  9. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
  10.  
  11. DBCC UPDATEUSAGE(LibraryMS,"dbo.bMaterialIn93");
  12. GO
  13.  
  14. DBCC UPDATEUSAGE: Usage counts updated for table 'bMaterialIn93'(index 'bMaterialIn93', partition 1):
  15.         USED pages(In-row Data): changed from (38) to (24) pages.
  16.         RSVD pages(In-row Data): changed from (-18) to (49) pages.
  17. DBCC UPDATEUSAGE: Usage counts updated for table 'bMaterialIn93'(index 'PK_bMaterialIn93', partition 1):
  18.         RSVD pages(In-row Data): changed from (16) to (17) pages.
  19. DBCC execution completed. If DBCC printed error messages, contact your system administrator.

 

关于DBCC UPDATEUSAGE的MSDN解释如下:

-----------------------------------------------------------------------------------------------------------------------

备注

      DBCC UPDATEUSAGE 将针对表或索引中的每个分区更正行、已用页、保留页、叶级页和数据页的计数。如果系统表中没有错误,则 DBCC UPDATEUSAGE 不返回数据。如果发现错误,并对其进行更正,同时没有使用 WITH NO_INFOMSGS,DBCC UPDATEUSAGE 将返回系统表中更新的行和列。

   使用 DBCC UPDATEUSAGE 同步空间使用计数器。因为 DBCC UPDATEUSAGE 在大型表或大型数据库中运行可能会需要一些时间,所以通常只在怀疑 sp_spaceused 返回的值不正确时使用。sp_spaceused 在返回表或索引的空间信息之前接受可选参数以运行 DBCC UPDATEUSAGE。

升级数据库

在 SQL Server 的早期版本中,用于表和索引行计数以及页计数的值可能不正确。根据 SQL Server 2005 之前的版本创建的数据库可能包含错误的计数。因此,我们建议在升级之后运行 DBCC UPDATEUSAGE,以便更正所有的无效计数。

DBCC CHECKDB 已得到增强,可以检测页计数或行计数变为负值的情况。检测到上述问题后,DBCC CHECKDB 的输出会包含一个警告和一个建议,建议运行 DBCC UPDATEUSAGE 解决该问题。

------------------------------------------------------------------------------------------------------------------------