forked from aleksey-vitsko/Database-Administrator-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sessions - ViewSessionsConnections.sql
549 lines (379 loc) · 16.3 KB
/
Sessions - ViewSessionsConnections.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
create or alter procedure ViewSessionsConnections (@command varchar(100) = 'all') as begin
/********************************************* INFO *******************************************
Author: Aleksey Vitsko
Created: May 2018
--------------------------------------------------------------------------------------------------------------------------
Accepts arguments (@command):
'all' -- show all sessions / connections (default)
'executing','running' -- show user sessions that are currently running / executing queries
'blocking','blocked' -- show sessions that are blocked / cause blocking
'user' -- show only user sessions
'system' -- show only system sessions
'open tran' -- show sessions that have open transaction
'execute as','run as','impersonate' -- show sessions that have original login <> current login
'memory grant','memory grants' -- show sessions that have memory grants
'tempdb' -- show sessions that currently consume tempdb
'summary' -- show aggregate session counts by login
--------------------------------------------------------------------------------------------------------------------------
Version: 1.14
Change history:
2022-08-11 - Aleksey Vitsko - added command "tempdb" (show only sessions that currently consume tempdb)
2022-08-11 - Aleksey Vitsko - added "tempdb_session_kb" and "tempdb_task_kb" columns (show tempdb consumption by session)
2022-08-10 - Aleksey Vitsko - added output mode "memory grant" (will show only sessions that have memory grants)
2022-08-10 - Aleksey Vitsko - added columns related to query memory grant information
2022-08-10 - Aleksey Vitsko - renamed "memory_usage" column to "memory_usage_pages", other cosmetic changes
2018-05-08 - Aleksey Vitsko - added support for db_user_name
2018-05-07 - Aleksey Vitsko - added support for blocking_sql_text
2018-05-03 - Aleksey Vitsko - created procedure
******************************************************************************************/
-- get session list
if object_id ('tempdb..#SessionsConnections') is not null drop table #SessionsConnections
create table #SessionsConnections (
session_id smallint, -- sys.dm_exec_sessions
kpid smallint, -- sys.sysprocesses (windows thread id)
database_id int, -- sys.dm_exec_sessions
[db_name] varchar(150), -- sys.databases
is_user_process bit, -- sys.dm_exec_sessions
[host_name] nvarchar(128), -- sys.dm_exec_sessions
host_process_id int,
[program_name] nvarchar(128), -- sys.dm_exec_sessions
client_interface_name nvarchar(32), -- sys.dm_exec_sessions
nt_domain nvarchar(128), -- sys.dm_exec_sessions
nt_user_name nvarchar(128), -- sys.dm_exec_sessions
login_name nvarchar(128), -- sys.dm_exec_sessions
original_login_name nvarchar(128), -- sys.dm_exec_sessions
security_id varbinary(85), -- sys.dm_exec_sessions
original_security_id varbinary(85), -- sys.dm_exec_sessions
connect_time datetime, -- connections
login_time datetime, -- sys.dm_exec_sessions
[status] nvarchar(30), -- sys.dm_exec_sessions
[language] nvarchar(128), -- sys.dm_exec_sessions
cmd nvarchar(32), -- sys.sysprocesses
command nvarchar(32), -- exec_requests
[user_id] int, -- exec_requests
[uid] int, -- sys.sysprocesses
db_user_name nvarchar(128), -- sys.users
blocking_session_id smallint, -- exec_requests
blocked smallint, -- sys.sysprocesses
percent_complete real, -- exec_requests
estimated_completion_time bigint, -- exec_requests
con_session_id int, -- connections
most_recent_session_id int, -- connections
connection_id uniqueidentifier, -- connections
net_transport nvarchar(40), -- connections
protocol_type nvarchar(40), -- connections
auth_scheme nvarchar(40), -- connections
net_library nchar(12), -- sys.sysprocesses
client_net_address varchar(48), -- connections
client_tcp_port int, -- connections
local_net_address varchar(48), -- connections
local_tcp_port int, -- connections
cpu_time int, -- sys.dm_exec_sessions
memory_usage_pages int, -- sys.dm_exec_sessions -- Number of 8-KB pages of memory used by this session.
requested_memory_kb bigint, -- sys.dm_exec_query_memory_grants -- Total requested amount of memory in kilobytes.
granted_memory_kb bigint, -- sys.dm_exec_query_memory_grants -- Total amount of memory actually granted in kilobytes. Can be NULL if the memory is not granted yet.
used_memory_kb bigint, -- sys.dm_exec_query_memory_grants -- Physical memory used at this moment in kilobytes.
max_used_memory_kb bigint, -- sys.dm_exec_query_memory_grants -- Maximum physical memory used up to this moment in kilobytes.
tempdb_session_kb bigint, -- sys.dm_db_session_space_usage -- (((user_objects_alloc_page_count - user_objects_dealloc_page_count) + (internal_objects_alloc_page_count - internal_objects_dealloc_page_count)) * 8) > 0
tempdb_task_kb bigint, -- sys.dm_db_task_space_usage -- (((user_objects_alloc_page_count - user_objects_dealloc_page_count) + (internal_objects_alloc_page_count - internal_objects_dealloc_page_count)) * 8) > 0
reads bigint, -- sys.dm_exec_sessions
writes bigint, -- sys.dm_exec_sessions
logical_reads bigint, -- sys.dm_exec_sessions
[deadlock_priority] int, -- sys.dm_exec_sessions
open_transaction_count int, -- sys.dm_exec_sessions
row_count bigint, -- sys.dm_exec_sessions
transaction_isolation_level_id smallint, -- 0 = Unspecified 1 = ReadUncommitted 2 = ReadCommitted 3 = Repeatable 4 = Serializable 5 = Snapshot
transaction_isolation_level varchar(50),
total_scheduled_time int, -- sys.dm_exec_sessions
total_elapsed_time int, -- sys.dm_exec_sessions
last_request_start_time datetime, -- sys.dm_exec_sessions
last_request_end_time datetime, -- sys.dm_exec_sessions
wait_type nvarchar(60), -- exec_requests
wait_time int, -- exec_requests
wait_resource nvarchar(256), -- exec_requests
last_wait_type nvarchar(60), -- exec_requests
[sql_handle] varbinary(64), -- exec_requests
most_recent_sql_handle varbinary(64), -- connections
sql_text nvarchar(max) default '', -- dm_exec_sql_text
most_recent_sql_text nvarchar(max) default '', -- dm_exec_sql_text
blocking_sql_text nvarchar(max) default ''
)
-- clustered index on session ID (not primary key because there might be NULLs)
create clustered index CIX_Session_ID on #SessionsConnections (session_id)
-- get sessions
insert into #SessionsConnections (session_id, database_id, is_user_process, [host_name], host_process_id, [program_name], client_interface_name, nt_domain, nt_user_name, login_name, security_id, original_security_id,
original_login_name, login_time, [status], [language],cpu_time, memory_usage_pages, reads, writes, logical_reads, [deadlock_priority], open_transaction_count, row_count, transaction_isolation_level_id, total_scheduled_time,
total_elapsed_time, last_request_start_time, last_request_end_time)
select
session_id,
database_id,
is_user_process,
[host_name],
host_process_id,
[program_name],
client_interface_name,
nt_domain,
nt_user_name,
login_name,
security_id,
original_security_id,
original_login_name,
login_time,
[status],
[language],
cpu_time,
memory_usage,
reads,
writes,
logical_reads,
[deadlock_priority],
open_transaction_count,
row_count,
transaction_isolation_level,
total_scheduled_time,
total_elapsed_time,
last_request_start_time,
last_request_end_time
from sys.dm_exec_sessions
-- get memory grant info
update s
set s.requested_memory_kb = g.requested_memory_kb,
s.granted_memory_kb = g.granted_memory_kb,
s.used_memory_kb = g.used_memory_kb,
s.max_used_memory_kb = g.max_used_memory_kb
from #SessionsConnections s
join sys.dm_exec_query_memory_grants g on
s.session_id = g.session_id
-- if command is "memory grant", delete other sessions from sp output
if @command in ('memory grant','memory grants') begin
delete from #SessionsConnections
where requested_memory_kb is NULL
end
-- get tempdb usage by session
update s
set tempdb_session_kb = ((user_objects_alloc_page_count - user_objects_dealloc_page_count) + (internal_objects_alloc_page_count - internal_objects_dealloc_page_count)) * 8
from #SessionsConnections s
join sys.dm_db_session_space_usage ssu on
s.session_id = ssu.session_id
and ((user_objects_alloc_page_count - user_objects_dealloc_page_count) + (internal_objects_alloc_page_count - internal_objects_dealloc_page_count)) > 0
-- get tempdb usage by task / session
update s
set tempdb_task_kb = [sum_tempdb_task_pages] * 8
from #SessionsConnections s
join (select
session_id,
sum((user_objects_alloc_page_count - user_objects_dealloc_page_count) + (internal_objects_alloc_page_count - internal_objects_dealloc_page_count)) [sum_tempdb_task_pages]
from sys.dm_db_task_space_usage
where ((user_objects_alloc_page_count - user_objects_dealloc_page_count) + (internal_objects_alloc_page_count - internal_objects_dealloc_page_count)) > 0
group by session_id) tsu on
s.session_id = tsu.session_id
-- if command is "memory grant", delete other sessions from sp output
if @command in ('tempdb') begin
delete from #SessionsConnections
where tempdb_session_kb is NULL
and tempdb_task_kb is NULL
end
-- get sys processes info
update sc
set sc.kpid = p.kpid,
sc.[uid] = p.[uid],
sc.net_library = p.net_library,
sc.cmd = p.cmd,
sc.blocked = p.blocked
from #SessionsConnections sc
join sys.sysprocesses p on
session_id = spid
-- get database name
update sc
set [db_name] = d.[name]
from #SessionsConnections sc
join sys.databases d on
sc.database_id = d.database_id
-- get database users
begin try
exec sp_MSforeachdb 'USE [?]; update #SessionsConnections set db_user_name = [name] from #SessionsConnections join sys.database_principals on [uid] = principal_id where [db_name] = db_name()'
end try
begin catch
update #SessionsConnections
set db_user_name = [name]
from #SessionsConnections
join sys.database_principals on
[uid] = principal_id
where [db_name] = db_name()
end catch
update #SessionsConnections
set db_user_name = 'dbo'
where original_login_name = 'sa'
and db_user_name is NULL
-- transaction isolation level
update #SessionsConnections
set transaction_isolation_level = case transaction_isolation_level_id
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
-- get executing requests info
update sc
set sc.command = r.command,
sc.[user_id] = r.[user_id],
sc.blocking_session_id = r.blocking_session_id,
sc.percent_complete = r.percent_complete,
sc.estimated_completion_time = r.estimated_completion_time,
sc.wait_type = r.wait_type,
sc.wait_time = r.wait_time,
sc.wait_resource = r.wait_resource,
sc.last_wait_type = r.last_wait_type,
sc.[sql_handle] = r.[sql_handle]
from #SessionsConnections sc
join sys.dm_exec_requests r on
sc.session_id = r.session_id
-- get connections info
update sc
set sc.con_session_id = c.session_id,
sc.most_recent_session_id = c.most_recent_session_id,
sc.connection_id = c.connection_id,
sc.connect_time = c.connect_time,
sc.net_transport = c.net_transport,
sc.protocol_type = c.protocol_type,
sc.auth_scheme = c.auth_scheme,
sc.client_net_address = c.client_net_address,
sc.client_tcp_port = c.client_tcp_port,
sc.local_net_address = c.local_net_address,
sc.local_tcp_port = c.local_tcp_port,
sc.most_recent_sql_handle = c.most_recent_sql_handle
from #SessionsConnections sc
join sys.dm_exec_connections c on
sc.session_id = c.session_id
-- get current executing sql text
update #SessionsConnections
set sql_text = [text]
from #SessionsConnections
cross apply sys.dm_exec_sql_text([sql_handle])
-- get most recent executed sql text
update #SessionsConnections
set most_recent_sql_text = [text]
from #SessionsConnections
cross apply sys.dm_exec_sql_text([most_recent_sql_handle])
-- get blocking sql text
update sc
set sc.blocking_sql_text = sc2.most_recent_sql_text
from #SessionsConnections sc
join #SessionsConnections sc2 on
sc.blocking_session_id = sc2.session_id
-- get connections without sessions
insert into #SessionsConnections (connect_time, net_transport, protocol_type, auth_scheme, client_net_address, client_tcp_port, local_net_address, local_tcp_port, connection_id, most_recent_sql_handle)
select
connect_time,
net_transport,
protocol_type,
auth_scheme,
client_net_address,
client_tcp_port,
local_net_address,
local_tcp_port,
connection_id,
most_recent_sql_handle
from sys.dm_exec_connections
where session_id is NULL
-- mark system connections
update #SessionsConnections
set is_user_process = 0
where is_user_process is NULL
-- update blocking/blocked info
update #SessionsConnections
set blocked = 0
where blocked is NULL
update #SessionsConnections
set blocking_session_id = 0
where blocking_session_id is NULL
------------------------------------------------------ Show Data --------------------------------------------------------
-- view all sessions / connections
if @command = 'all' begin
select * from #SessionsConnections
order by db_name, database_id, is_user_process, login_name
end
-- view sessions / connections that are currently executing queries
if @command in ('executing','running') begin
select * from #SessionsConnections
where sql_text <> ''
order by db_name, database_id, is_user_process, login_name
end
-- view all sessions that cause blocking
if @command in ('blocking','blocked') begin
select * from #SessionsConnections
where (blocked <> 0)
or (blocking_session_id <> 0)
or session_id in (select blocked from #SessionsConnections where blocked <> 0)
or session_id in (select blocked from #SessionsConnections where blocking_session_id <> 0)
order by db_name, database_id, is_user_process, login_name
end
-- view all sessions that use impersonation / execute as
if @command in ('execute as','run as','impersonate') begin
select * from #SessionsConnections
where login_name <> original_login_name
or security_id <> original_security_id
order by db_name, database_id, is_user_process, login_name
end
-- view only user sessions
if @command in ('user') begin
select * from #SessionsConnections
where is_user_process = 1
order by db_name, database_id, is_user_process, login_name
end
-- view only system sessions
if @command in ('system') begin
select * from #SessionsConnections
where is_user_process = 0
order by db_name, database_id, is_user_process, login_name
end
-- view open transaction count > 0 sessions
if @command in ('open tran') begin
select * from #SessionsConnections
where open_transaction_count > 0
order by db_name, database_id, is_user_process, login_name
end
-- view memory grant info
if @command in ('memory grant','memory grants') begin
select * from #SessionsConnections
where requested_memory_kb is not NULL
order by requested_memory_kb desc
--order by db_name, database_id, is_user_process, login_name
end
-- view tempdb consumption
if @command in ('tempdb') begin
select * from #SessionsConnections
where tempdb_session_kb is not NULL
or tempdb_task_kb is not NULL
order by tempdb_session_kb desc, tempdb_task_kb desc
end
-- view summary info
if @command in ('summary') begin
-- totals by system and user
select
(select count(*) from #SessionsConnections where is_user_process = 0) [System Processes],
(select count(*) from #SessionsConnections where is_user_process = 1) [User Processes]
-- total by login
select
case is_user_process
when 0 then 'System'
when 1 then 'User'
end [Type],
[db_name],
login_name,
count(*) [Process Count]
from #SessionsConnections
group by case is_user_process
when 0 then 'System'
when 1 then 'User'
end,
[db_name],
login_name
order by [Type], [db_name], [Process Count] desc
end
-- select * from sys.dm_exec_connections
-- select * from sys.sysprocesses
-- select * from sys.dm_exec_connections
end