Dynamics 365 CE中AsyncOperationBase表記錄太多,影響系統性能怎麼辦?

微軟動態CRM專家羅勇 ,回覆311或者20190311可方便獲取本文,同時能夠在第一間獲得我發佈的最新博文信息,follow me!個人網站是 www.luoyong.me 。html

本文主要是根據微軟官方2018年4月17日更新的知識文章 Performance is slow if the AsyncOperationBase table becomes too large in Microsoft Dynamics CRM 而寫,原文是英文的,我就摘取其部份內容,加入本身的理解簡述下,更加具體的請參考原文。本人不對參考本文後執行的操做而致使的任何後果承擔任何責任或者連帶責任,且看,且珍惜。數據庫

假若這個表記錄太多,好比過千萬條,極可能影響整個Dynamics 365 Customer Engagement的性能,爲了快速刪除,這裏提供了SQL進行刪除的方法,對於Dynamics 365 Customer Engagement 本地部署版本(On-Premise),本身根據這個知識文章進行操做就能夠,如果Dynamics 365 Customer Engagement Online,能夠開個Case給微軟客戶服務技術支持中心請他們來執行。固然啦,你還能夠本身寫個程序,其多個線程來利用ExecuteMultipleRequest消息刪除這個實體中的記錄。異步

這裏講一下SQL刪除的方法,這個方法比本身寫程序調用消息來刪除要快不少。async

執行前的準備工做:ide

1. 先執行以下的T-SQL腳本添加索引。工具

CREATE NONCLUSTERED INDEX CRM_WorkflowLog_AsyncOperationID ON [dbo].[WorkflowLogBase] ([AsyncOperationID]) GO 

CREATE NONCLUSTERED INDEX CRM_DuplicateRecord_AsyncOperationID ON [dbo].[DuplicateRecordBase] ([AsyncOperationID]) GO

CREATE NONCLUSTERED INDEX CRM_BulkDeleteOperation_AsyncOperationID ON [dbo].[BulkDeleteOperationBase] (AsyncOperationID) GO

2.中止名稱爲 Microsoft Dynamics 365 異步處理服務 (英文名稱爲Microsoft Dynamics CRM Asynchronous Processing Service)的異步服務,非必須。性能

3.若是可能更改數據庫的恢復模式(Recovery model)爲簡單(Simple),這樣能減小寫入的數據庫日誌。右擊數據庫,選擇屬性,更改以下:生產環境不要作這個步驟網站

 

而後就到了真正執行的時候了,執行以下的T-SQL腳本:spa

IF EXISTS (SELECT name from sys.indexes WHERE name = N'CRM_AsyncOperation_CleanupCompleted') DROP Index AsyncOperationBase.CRM_AsyncOperation_CleanupCompleted GO
CREATE NONCLUSTERED INDEX CRM_AsyncOperation_CleanupCompleted ON [dbo].[AsyncOperationBase] ([StatusCode],[StateCode],[OperationType]) GO

while(1=1) begin
declare @DeleteRowCount int = 10000
declare @rowsAffected int
declare @DeletedAsyncRowsTable table (AsyncOperationId uniqueidentifier not null primary key) insert into @DeletedAsyncRowsTable(AsyncOperationId) Select top (@DeleteRowCount) AsyncOperationId from AsyncOperationBase where OperationType in (1, 9, 12, 25, 27, 10) AND StateCode = 3 
  AND StatusCode in (30, 32) select @rowsAffected = @@rowcount 
 delete poa from PrincipalObjectAccess poa join WorkflowLogBase wlb on poa.ObjectId = wlb.WorkflowLogId join @DeletedAsyncRowsTable dart on wlb.AsyncOperationId = dart.AsyncOperationId delete WorkflowLogBase from WorkflowLogBase W, @DeletedAsyncRowsTable d where W.AsyncOperationId = d.AsyncOperationId delete BulkDeleteFailureBase From BulkDeleteFailureBase B, @DeletedAsyncRowsTable d where B.AsyncOperationId = d.AsyncOperationId delete BulkDeleteOperationBase From BulkDeleteOperationBase O, @DeletedAsyncRowsTable d where O.AsyncOperationId = d.AsyncOperationId delete WorkflowWaitSubscriptionBase from WorkflowWaitSubscriptionBase WS, @DeletedAsyncRowsTable d where WS.AsyncOperationId = d.AsyncOperationID delete AsyncOperationBase From AsyncOperationBase A, @DeletedAsyncRowsTable d where A.AsyncOperationId = d.AsyncOperationId /*If not calling from a SQL job, use the WAITFOR DELAY*/
if(@DeleteRowCount > @rowsAffected) return
else
  WAITFOR DELAY '00:00:02.000'
end

說明:Opertaion Type的說明以下:插件

  • Workflow Expansion Task (1)
  • Collect SQM data (9)
  • PersistMatchCode (12)
  • FullTextCatalogIndex (25)
  • UpdateContractStates (27)
  • Workflow (10)

執行完畢後,也有些收尾工做,以下:

1. 建議刪除以前臨時增長的索引(原文未提到此步驟,個人建議是恢復原狀):

Drop INDEX dbo.WorkflowLogBase.CRM_WorkflowLog_AsyncOperationID; GO 

Drop INDEX dbo.DuplicateRecordBase.CRM_DuplicateRecord_AsyncOperationID; GO

Drop INDEX dbo.BulkDeleteOperationBase.CRM_BulkDeleteOperation_AsyncOperationID; GO

2. 使用T-SQL更新下表的統計信息:

UPDATE STATISTICS [dbo].[AsyncOperationBase] WITH FULLSCAN UPDATE STATISTICS [dbo].[DuplicateRecordBase] WITH FULLSCAN UPDATE STATISTICS [dbo].[BulkDeleteOperationBase] WITH FULLSCAN --下面這個表在Dynamcis 365 Customer Engagement中不存在 --UPDATE STATISTICS [dbo].[WorkflowCompletedScopeBase] WITH FULLSCAN
UPDATE STATISTICS [dbo].[WorkflowLogBase] WITH FULLSCAN UPDATE STATISTICS [dbo].[WorkflowWaitSubscriptionBase] WITH FULLSCAN

 3. 如有更改數據庫的恢復模式,請恢復原狀。

值得提醒你們注意的是,部署到生產環境的工做流應該選擇 【自動刪除已完成的工做流做業(以節省磁盤空間)】,部署到生產環境的SDK插件步驟要選擇【Delete AsyncOperation if StatusCode = Successful】。固然啦,也沒有必要一個個手工去看,我本身寫了個工具能夠檢查出來沒有選擇的,請參考個人文章:Dynamics 365檢查工做流、SDK插件步驟是否選中運行成功後自動刪除系統做業記錄 

原文出處:https://www.cnblogs.com/luoyong0201/p/Dynamics_365_performance_is_slow_if_the_asyncoperationbase_table_becomes_too_large.html

相關文章
相關標籤/搜索