SQL Server 更新統計資訊出現嚴重錯誤,應放棄任何可能產生的結果
一臺SQL Server 2008 R2版本(具體版本如下所示)的資料庫,最近幾天更新統計資訊的作業出錯,錯誤如下所示:
Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)
Jun 28 2012 08:36:30
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (Hypervisor)
第一次碰到這麼奇葩的問題。檢視錯誤日誌,就會發現更新統計資訊時出現異常,生成了dump檔案。
對錶做DBCC CHECKTABLE發現正常,未有一致性錯誤。
DBCC CHECKTABLE ( 'TBusRetail' )
後面查了一下資料,在官方文件看到有個Bug會導致這個問題,官方文件為:FIX: An access violation may occur when you update the statistics of a table after you enable and then disable conflict detection on a table in in SQL Server 2008 or in SQL Server 2008 R2。剛好我們這個環境的版本也在其中。具體參考下面:
Cause
This issue occurs because the database engine is trying to load dangling statistics. When P2P conflict detection is enabled, an MDColumnIdP2pCdId system column is added to the base index rowset of the table. Replication-related queries may create statistics on the system column automatically. When P2P conflict detection is disabled, the system column is removed from the table. However, the corresponding statistics remain. Therefore, updating statistics causes the access violation exception to occur because the statistics cannot be added to the table.
SQL Server 2008 Service Pack 2
The fix for this issue was first released in Cumulative Update 3 for SQL Server 2008 Service Pack 2. For more information about this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2498535 Cumulative update package 3 for SQL Server 2008 Service Pack 2
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 fix release. Microsoft recommends that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
2402659 The SQL Server 2008 builds that were released after SQL Server 2008 Service Pack 2 was released
Microsoft SQL Server 2008 hotfixes are created for specific SQL Server service packs. You must apply a SQL Server 2008 Service Pack 2 hotfix to an installation of SQL Server 2008 Service Pack 2. By default, any hotfix that is provided in a SQL Server service pack is included in the next SQL Server service pack.
The fix for this issue was first released in Cumulative Update package 6 for SQL Server 2008 R2. For more information about how to obtain this cumulative update package, click the following article number to view the article in the Microsoft Knowledge Base:
2489376 Cumulative Update package 6 for SQL Server 2008 R2
Note Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 R2 fix release. We recommend that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
981356 The SQL Server 2008 R2 builds that were released after SQL Server 2008 R2 was released
但是等我打上補丁後,測試發現問題依然存在,也只有這個表存在這個問題。後面仔細檢查,發現這個表有不少計算列( Computed Column ),剛好以前也遇到過由於計算列導致統計資訊更新出現錯誤的情況,一檢查,發現這表有大量的統計資訊,遂生成刪除統計資訊的指令碼後執行刪除(排除了相關索引的統計資訊)。然後再更新統計資訊,OK,問題解決了。看來又是神奇計算列導致的統計資訊更新異常!
SELECT 'DROP STATISTICS dbo.TBusRetail.' + QUOTENAME ( name ) + ';'
FROM sys . stats
WHERE object_id = OBJECT_ID ( 'dbo.TBusRetail' )
DROP STATISTICS dbo.TBusRetail.[PK_TBUSRETAIL]; -- 排除這個統計資訊
DROP STATISTICS dbo.TBusRetail.[IdxRefNoOpDate]; -- 排除這個統計資訊
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000016_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000015_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000003_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_PayWay_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_Opr_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_Charger_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_RefNo_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_TicketAmount_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_ChargeDate_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_TicketFAmount_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_PayAmount_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_Accepted_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_OpDate_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_Checked_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_CheckDate_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_HangUp_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_TPayAmount_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_Remark_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_RetailType_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_AcceptDate_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_IfNetRetail_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_DisModified_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_Dealed_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_VipScale_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000024_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000032_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000033_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000031_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000030_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_0000002C_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_0000002B_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_0000002A_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000029_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000028_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000027_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000026_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000025_58BCECDB]
DROP STATISTICS dbo . TBusRetail . [_WA_Sys_00000018_58BCECDB]
上一次遇到的問題在這裡: ofollow,noindex">訊息 8134,級別 16,狀態 1,第 1 行 遇到以零作除數錯誤
參考資料:
https://support.microsoft.com/en-us/help/2498796/fix-an-access-violation-may-occur-when-you-update-the-statistics-of-a