Tuesday, June 23, 2015

DBCC Inputbuffer

USE master;
GO
SELECT  p.spid AS [SPID],
       DB_NAME(p.dbid) AS [DBName],
       CASE
           WHEN txt.encrypted = 1
           THEN N'encrypted'
           WHEN r.session_id IS NULL
           THEN txt.text
           ELSE LTRIM(SUBSTRING(txt.text, r.statement_start_offset / 2 + 1, (( CASE
                                                                                   WHEN r.statement_end_offset = -1
                                                                                   THEN DATALENGTH(txt.text)
                                                                                   ELSE r.statement_end_offset
                                                                               END ) - r.statement_start_offset ) / 2))
       END AS [Query],
       txt.text,
       p.status AS [Status],
       ISNULL(r.percent_complete, 0) AS [Percent_complete],
       p.blocked AS [BlkBy],
       p.open_tran,
       c.[connect_time],
       [estimated_finish_time] = DATEADD(MILLISECOND, r.[estimated_completion_time], CURRENT_TIMESTAMP),
       p.loginame AS [Login],
       p.hostname AS [Host Name],
       p.cpu AS [CPU],
       p.physical_io AS [IO],
       p.program_name,
       w.wait_type,
       w.resource_description,
 TS.session_internal_objects_dealloc_page_count,
 TS.session_internal_objects_alloc_page_count
FROM sys.sysprocesses AS p
     LEFT JOIN sys.dm_exec_connections AS c( NOLOCK ) ON c.session_id = p.spid
     LEFT JOIN sys.dm_exec_requests AS r ON c.session_id = r.session_id
     LEFT JOIN sys.dm_os_waiting_tasks AS w ON w.session_id = r.session_id
     CROSS APPLY sys.dm_exec_sql_text( c.most_recent_sql_handle ) AS txt
                 LEFT OUTER JOIN(
                                  SELECT session_id,
                                         database_id = MAX(database_id),
                                         trancount = COUNT(*)
                                  FROM sys.dm_tran_session_transactions AS t
                                       INNER JOIN sys.dm_tran_database_transactions AS dt ON t.transaction_id = dt.transaction_id
                                  GROUP BY session_id ) AS t ON t.session_id = p.spid
                 LEFT OUTER JOIN(
                                  SELECT request_session_id,
                                         database_id = MAX(resource_database_id),
                                         lockcount = COUNT(*)
                                  FROM sys.dm_tran_locks WITH ( NOLOCK )
                                  GROUP BY request_session_id ) AS tl ON p.spid = tl.request_session_id
left outer join (SELECT R1.session_id
     , R1.internal_objects_alloc_page_count + R2.task_internal_objects_alloc_page_count AS session_internal_objects_alloc_page_count
     , R1.internal_objects_dealloc_page_count + R2.task_internal_objects_dealloc_page_count AS session_internal_objects_dealloc_page_count
FROM sys.dm_db_session_space_usage AS R1
     INNER JOIN(
                 SELECT session_id
                      , SUM(internal_objects_alloc_page_count) AS task_internal_objects_alloc_page_count
                      , SUM(internal_objects_dealloc_page_count) AS task_internal_objects_dealloc_page_count
                 FROM sys.dm_db_task_space_usage
                 GROUP BY session_id ) AS R2 ON R1.session_id = R2.session_id
) TS on TS.session_id = p.spid
--WHERE DATEDIFF(MI, c.connect_time, DATEADD(MILLISECOND, r.[estimated_completion_time], CURRENT_TIMESTAMP)) > 20
ORDER BY  r.percent_complete DESC,blocked desc,p.spid DESC, p.open_tran DESC;
GO

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home