在sp_who2中有一个或多个blkby列具有当前spid是什么意思?

时间:2019-05-07 20:18:35

标签: sql-server sql-server-2012 blocking

例如,我目前正在查看一个sp_who2报告,其中spid 140大约有40行,其中blkby列中大约有30行包含140。这是什么意思?为什么140会多排一行,而spid会如何自身阻塞呢?

1 个答案:

答案 0 :(得分:0)

当sql服务器通过管道将大量数据假脱机传送到客户端并且客户端只能缓冲和读取这么多内容时,我已经看到了这一点。因此,您可能开始看到多个ASYNC_NETWORK_IO等待。您是否可以使用以下查询发布该流程的等待类型:

SELECT 
    S.Text, R.Session_id,R.Status,R.Command,R.CPU_Time,R.Total_Elapsed_Time
FROM    
    sys.dm_exec_requests R
    CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS S 

--EXEC sp_who2
SELECT  W.session_id,ot.task_state,W.wait_type, W.wait_duration_ms, W.blocking_session_id, W.resource_description, S.host_name, S.program_name
FROM  
    sys.dm_os_waiting_tasks  W  
    INNER  JOIN sys.dm_os_tasks ot ON ot.task_address = W.waiting_task_address 
    INNER JOIN sys.dm_exec_sessions S ON S.session_id = W.session_id 
WHERE 
    S.is_user_process =  1

SELECT 
    SessionID = es.session_id,
    Login =es.original_login_name,
    Host = es.host_name,
    RequestStart=es.last_request_start_time,
    RequerstEnd=es.last_request_end_time,
    Status =es.status,
    BockedBy =er.blocking_session_id,
    WaitType =er.wait_type, 
    WaitTime = er.wait_time,
    LastWaitType = er.last_wait_type,
    WaitResource = er.wait_resource,
    DatabaseID = DB_NAME(er.database_id),
    Command = er.command,
    --,sql_text=st.text
    TransactionIsolation =
        CASE es.transaction_isolation_level
            WHEN 0 THEN 'Unspecified'
            WHEN 1 THEN 'Read Uncommitted'
            WHEN 2 THEN 'Read Committed'
            WHEN 3 THEN 'Repeatable'
            WHEN 4 THEN 'Serializable'
            WHEN 5 THEN 'Snapshot'
        END,
    CPUTime = COALESCE(es.cpu_time,0) + COALESCE(er.cpu_time,0),
    IOTime = COALESCE(es.reads,0) + COALESCE(es.writes,0) + COALESCE(er.reads,0) + COALESCE(er.writes,0),
    TransactioCount = COALESCE(er.open_transaction_count,-1),
    ProgramName = COALESCE(es.program_name,''),
    LoginTme = es.login_time
FROM 
    sys.dm_exec_sessions es
    LEFT OUTER JOIN sys.dm_exec_connections ec ON es.session_id = ec.session_id
    LEFT OUTER JOIN sys.dm_exec_requests er ON es.session_id = er.session_id
    LEFT OUTER JOIN sys.server_principals sp ON es.security_id = sp.sid
    LEFT OUTER JOIN sys.dm_os_tasks ota ON es.session_id = ota.session_id
    LEFT OUTER JOIN sys.dm_os_threads oth ON ota.worker_address = oth.worker_address
    CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS st
WHERE 
    es.is_user_process = 1 
    and 
    es.session_id <> @@spid
ORDER BY
     es.session_id