Get client details and SQL statements for each process

Source: Internet
Author: User
Tags join session id

b When performance drops, many friends want to monitor which client, which user, which client initiated what session SQL statement,

But Microsoft has to use Profiler to achieve, but consider performance issues, many people do not want to!

There are many scripts on the web that can monitor client information, but only the SQL statements of the client process are not available!

I wrote one myself for reference:

--1. New Stored Procedure
--create proc Prtest
--@spid int
--as
--DBCC InputBuffer (@spid)
--go

--2. Save the results to a temporary variable #tmp

SELECT [session ID] as conversation ID,
[Login] As user name,
[Database] As database,
[Task State] As state,
[Command] As command,
[Application] As application software,
[Wait Time (ms)] As wait time,
[Wait Type] As wait type,
[Host Name] As client name,
[Net Address] As IP address into #tmp

From (SELECT [session ID] = s.session_id,
[User Process] = CONVERT (CHAR (1), s.is_user_process),
[Login] = S.login_name,
[Database] = ISNULL (db_name (p.dbid), N "),
[Task State] = ISNULL (t.task_state, N '),
[Command] = ISNULL (R.command, N '),
[Application] = ISNULL (s.program_name, N '),
[Wait Time (ms)] = ISNULL (W.wait_duration_ms, 0),
[Wait Type] = ISNULL (W.wait_type, N '),
[Wait Resource] = ISNULL (w.resource_description, N '),
[Blocked by] = ISNULL (CONVERT (VARCHAR, w.blocking_session_id),
'') ,
[head blocker] = case
When r2.session_id are not NULL
and (r.blocking_session_id = 0
OR r.session_id is NULL
) THEN ' 1 '
ELSE ' "
End,
[Total CPU (ms)] = S.cpu_time,
[Total Physical I/O (MB)] = (s.reads + s.writes) * 8
/1024,
[Memory Use (KB)] = S.memory_usage * 8192/1024,
[Open Transactions] = ISNULL (r.open_transaction_count, 0),
[Login Time] = S.login_time,
[Last Request Start time] = S.last_request_start_time,
[Host Name] = ISNULL (s.host_name, N '),
[Net Address] = ISNULL (c.client_net_address, N '),
[Execution context ID] = ISNULL (t.exec_context_id, 0),
[Request ID] = ISNULL (r.request_id, 0),
[Workload Group] = ISNULL (G.name, N ")
From Sys.dm_exec_sessions s
Left OUTER JOIN sys.dm_exec_connections c on (s.session_id = c.session_id)
Left OUTER JOIN sys.dm_exec_requests r on (s.session_id = r.session_id)
Left OUTER JOIN sys.dm_os_tasks t on (r.session_id = t.session_id
and r.request_id = t.request_id
)
Left OUTER JOIN (
SELECT *,
Row_number () over (PARTITION by waiting_task_address ORDER by Wait_duration_ms DESC) as Row_num
From Sys.dm_os_waiting_tasks
) W on (t.task_address = w.waiting_task_address)
and W.row_num = 1
Left OUTER JOIN sys.dm_exec_requests R2 on (s.session_id = r2.blocking_session_id)
Left OUTER JOIN sys.dm_resource_governor_workload_groups g on (g.group_id = s.group_id)
Left OUTER JOIN sys.sysprocesses p on (s.session_id = p.spid)
) T
WHERE T.command in (' SELECT ', ' UPDATE ', ' DELETE ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.