SQL SERVER 事务执行情况跟踪分析

[sql] view plain copy
---查看现在所有的事务  
  
select '正在运行事务的会话的 ID'=session_id,                     --session_id与transaction_id的对应关系    
       '事务的 ID'=transaction_id,  
       '正在处理事务的会话中的活动请求数'=enlist_count,    
        '用户or系统事务'=case is_user_transaction when 1 then '事务由用户请求启动'  
                                                    when 0 then '系统事务'  
                                                    end,  
        '本地or分布式事务'= case is_local when 0 then '分布式事务或登记的绑定会话事务'  
                                                    when 1 then '本地事务'  
                                                    end,  
        '分布式事务类型'=case is_enlisted when 0 then '非登记的分布式事务'  
                                                    when 1 then '登记的分布式事务'  
                                                    end,  
        '绑定会话中处于状态'=case is_enlisted when 0 then '事务在通过绑定会话的会话中处于非活动状态。'  
                                                    when 1 then '事务在通过绑定会话的会话中处于活动状态'  
                                                    end        
        from sys.dm_tran_session_transactions  --会话中的事务,识别所有打开的事务     
        where is_user_transaction =1    
  
  
----活动事务的具体信息    
select dt.transaction_id,    
       dt.name,                            
       dt.transaction_begin_time,    
       case dt.transaction_type           
           when 1 then '读/写事务'    
           when 2 then '只读事务'    
           when 3 then '系统事务'    
           when 4 then '分布式事务'    
       end 'transaction type',    
           
       case dt.transaction_state    
           when 0 then '事务尚未完全初始化'    
           when 1 then '事务已初始化但尚未启动'    
           when 2 then '事务处于活动状态'    
           when 3 then '事务已结束。该状态用于只读事务'    
           when 4 then '已对分布式事务启动提交进程'    
           when 5 then '事务处于准备就绪状态且等待解析'    
           when 6 then '事务已提交'    
           when 7 then '事务正在被回滚'    
           when 8 then '事务已回滚'    
       end  'transaction state',  
       case dt.dtc_state  
            when 1 then '活动'  
            when 2 then '准备就绪'  
            when 3 then '已提交'  
            when 4 then '中止'  
            when 5 then '已恢复'  
            end dtc_state  
                
from sys.dm_tran_active_transactions dt    --活动的事务    
where transaction_id = 123    
  
  
---根据事务ID 和其对应的session_id 找到活动事务对应的执行语句  
  
select  dc.session_id,  
        ds.login_name,  
        ds.login_time,                 
        dc.connect_time,  
        dc.client_net_address,   
        ds.host_name,  
        ds.program_name,  
        case ds.status when 'sleeping' then '睡眠 - 当前没有运行任何请求 '  
                        when 'running' then '正在运行 - 当前正在运行一个或多个请求 '  
                        when 'Dormancy' then '休眠 – 会话因连接池而被重置,并且现在处于登录前状态'  
                        when 'Pre-connected' then '预连接 - 会话在资源调控器分类器中'  
                        end as status ,  
        ds.cpu_time as cpu_time_ms,  
        ds.memory_usage*8 as memory_kb,  
        ds.total_elapsed_time as total_elapsed_time_ms,  
        case ds.transaction_isolation_level when 0 then '未指定'  
                                        when 1 then '未提交读取'  
                                        when 2 then '已提交读取'  
                                        when 3 then '可重复'  
                                        when 4 then '可序列化'  
                                        when 5 then '快照'  
                                        end '会话的事务隔离级别',   
        dt.text                
from sys.dm_exec_connections  dc        --执行连接,最近执行的查询信息    
cross apply sys.dm_exec_sql_text(dc.most_recent_sql_handle) dt  
join sys.dm_exec_sessions ds  on dc.session_id=ds.session_id  
where dc.session_id = 55  
[sql] view plain copy
 
  1. ---查看现在所有的事务  
  2.   
  3. select '正在运行事务的会话的 ID'=session_id,                     --session_id与transaction_id的对应关系    
  4.        '事务的 ID'=transaction_id,  
  5.        '正在处理事务的会话中的活动请求数'=enlist_count,    
  6.         '用户or系统事务'=case is_user_transaction when 1 then '事务由用户请求启动'  
  7.                                                     when 0 then '系统事务'  
  8.                                                     end,  
  9.         '本地or分布式事务'case is_local when 0 then '分布式事务或登记的绑定会话事务'  
  10.                                                     when 1 then '本地事务'  
  11.                                                     end,  
  12.         '分布式事务类型'=case is_enlisted when 0 then '非登记的分布式事务'  
  13.                                                     when 1 then '登记的分布式事务'  
  14.                                                     end,  
  15.         '绑定会话中处于状态'=case is_enlisted when 0 then '事务在通过绑定会话的会话中处于非活动状态。'  
  16.                                                     when 1 then '事务在通过绑定会话的会话中处于活动状态'  
  17.                                                     end        
  18.         from sys.dm_tran_session_transactions  --会话中的事务,识别所有打开的事务     
  19.         where is_user_transaction =1    
  20.   
  21.   
  22. ----活动事务的具体信息    
  23. select dt.transaction_id,    
  24.        dt.name,                            
  25.        dt.transaction_begin_time,    
  26.        case dt.transaction_type           
  27.            when 1 then '读/写事务'    
  28.            when 2 then '只读事务'    
  29.            when 3 then '系统事务'    
  30.            when 4 then '分布式事务'    
  31.        end 'transaction type',    
  32.            
  33.        case dt.transaction_state    
  34.            when 0 then '事务尚未完全初始化'    
  35.            when 1 then '事务已初始化但尚未启动'    
  36.            when 2 then '事务处于活动状态'    
  37.            when 3 then '事务已结束。该状态用于只读事务'    
  38.            when 4 then '已对分布式事务启动提交进程'    
  39.            when 5 then '事务处于准备就绪状态且等待解析'    
  40.            when 6 then '事务已提交'    
  41.            when 7 then '事务正在被回滚'    
  42.            when 8 then '事务已回滚'    
  43.        end  'transaction state',  
  44.        case dt.dtc_state  
  45.             when 1 then '活动'  
  46.             when 2 then '准备就绪'  
  47.             when 3 then '已提交'  
  48.             when 4 then '中止'  
  49.             when 5 then '已恢复'  
  50.             end dtc_state  
  51.                 
  52. from sys.dm_tran_active_transactions dt    --活动的事务    
  53. where transaction_id = 123    
  54.   
  55.   
  56. ---根据事务ID 和其对应的session_id 找到活动事务对应的执行语句  
  57.   
  58. select  dc.session_id,  
  59.         ds.login_name,  
  60.         ds.login_time,                 
  61.         dc.connect_time,  
  62.         dc.client_net_address,   
  63.         ds.host_name,  
  64.         ds.program_name,  
  65.         case ds.status when 'sleeping' then '睡眠 - 当前没有运行任何请求 '  
  66.                         when 'running' then '正在运行 - 当前正在运行一个或多个请求 '  
  67.                         when 'Dormancy' then '休眠 – 会话因连接池而被重置,并且现在处于登录前状态'  
  68.                         when 'Pre-connected' then '预连接 - 会话在资源调控器分类器中'  
  69.                         end as status ,  
  70.         ds.cpu_time as cpu_time_ms,  
  71.         ds.memory_usage*8 as memory_kb,  
  72.         ds.total_elapsed_time as total_elapsed_time_ms,  
  73.         case ds.transaction_isolation_level when 0 then '未指定'  
  74.                                         when 1 then '未提交读取'  
  75.                                         when 2 then '已提交读取'  
  76.                                         when 3 then '可重复'  
  77.                                         when 4 then '可序列化'  
  78.                                         when 5 then '快照'  
  79.                                         end '会话的事务隔离级别',   
  80.         dt.text                
  81. from sys.dm_exec_connections  dc        --执行连接,最近执行的查询信息    
  82. cross apply sys.dm_exec_sql_text(dc.most_recent_sql_handle) dt  
  83. join sys.dm_exec_sessions ds  on dc.session_id=ds.session_id  
  84. where dc.session_id = 55  

 

原文地址:https://www.cnblogs.com/micro-chen/p/7722702.html