如何檢視distirbution agent的執行進度

stswordman發表於2013-08-07

transactional replication troubleshooting的過程中,經常會遇到下面的場景:

客戶在釋出端執行了一個幾百萬行的更新,結果導致效能下降。 客戶很想知道目前distribution agent的進度,完成的百分比,決定是等下去還是跳過這個過程。如果已經完成了90%,那麼貿然停止就非常可惜了,並且rollback的操作也是要很長時間的。

   

下面介紹如何檢視進度。

如果distribution agent已經啟用了verbose log,可以透過verbose log來檢視進度. Command id代表已經執行過的數量;transaction seqno表示正在進行的事務的xact_seqno 然後在distribution資料庫執行select count(*) From distribution..msrepl_commands with(nolock) where xact_seqno=@xact_seqno.

對比結果就可以知道進度了。

   

如果沒有啟用verbose log,就比較麻煩了,下面是具體的步驟。

  1. 找到相應的distribution agent 名稱和publisher_database_id

    select *From distribution..msdistribution_agents

  2. 透過名稱就可以找到distribution agent進行的process id. distributor上執行下面的語句。

    select hostprocess from sys.sysprocesses where program_name=@mergeAgentName

  3. 同一個distribution agent程式的process id是相同的,所以可以透過這個process id(對應trace裡的client process id),使用sql server trace得到distribution agent正在subscriber端執行的語句.
  4. 假設我們得到了下面這個語句exec [dbo].[sp_MSupd_dbota] default,511,4,0x02
  5. 根據這個儲存過程,我們可以得到相應的aritlce_id
    1. subscription database 執行sp_helptext,得到表的名稱
    2. distribution資料庫查詢得出article_id. select article_id from msarticles where destination_object=@tablename
  6. subscriber上執行下面的語句,得到subscription資料庫當前當xact_seqno. (請將第一步得到的distribution name帶入@distribution_agent

    select transaction_timestamp,* From MSreplication_subscriptions where distribution_agent=@distribution_agent

  7. 接下來就可找到distribution agent當前正在執行的xact_seqno. 將第一步得到的publisher_database_id,5步得到的article_id和上一步得到的xact_seqno帶入下面的查詢

    select xact_seqno,count(*) as number From distribution..msrepl_commands with(nolock)

    where publisher_database_id=@publisher_database_id and article_id=@article_id

    xact_seqno>@xact_seqno group by xact_seqno order by xact_seqno

  8. 順序靠前,並且number較大的就是正在執行的事務了。 您可能會問,為什麼不是第六步得到的xact_seqno的下一個呢(select min(xact_seqno)From distribution..msrepl_commands with(nolock)where publisher_database_id=@publisher_database_id and xact_seqno>@xact_seqno).

    因為distribution 並不是每一個事務都單獨提交的,而是根據CommitBatchSize CommitBatchThreshold來提交的,這樣可以提高效能。 具體請參見http://www.kendalvandyke.com/2008/11/how-commitbatchsize-and.html

  9. distribution資料執行sp_browsereplcmds @xact_seqno, @xact_seqno

  10. 用第四步得到的語句去查詢,這樣就可以知道當前執行到了什麼位置

相關文章