forked from DavidWiseman/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get_latches_Objects.sql
83 lines (79 loc) · 3.05 KB
/
Get_latches_Objects.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
IF OBJECT_ID('tempdb..#owt', 'U') IS NOT NULL DROP TABLE #owt;
CREATE TABLE #owt
(
session_id INT NOT NULL,
wait_type NVARCHAR(100) NOT NULL,
wait_duration_ms BIGINT NOT NULL,
rd NVARCHAR(100) NOT NULL,
database_num INT NOT NULL,
file_num INT NOT NULL,
page_num INT NOT NULL,
resource_type NVARCHAR(100) NOT NULL
);
INSERT INTO #owt
(session_id,
wait_type,
wait_duration_ms,
owt.rd,
database_num,
file_num,
page_num,
resource_type)
SELECT session_id,
owt.wait_type,
owt.wait_duration_ms,
owt.rd,
LEFT(owt.rd, owt.pos1 - 1) AS
database_num,
Substring(owt.rd, owt.pos1 + 1, Len(owt.rd) - ( owt.pos1 + owt.pos2 )) AS
file_num,
RIGHT(owt.rd, owt.pos2 - 1) AS
page_num,
CASE
WHEN RIGHT(owt.rd, owt.pos2 - 1) - 1 % 8088 = 0 THEN 'Is PFS Page'
WHEN RIGHT(owt.rd, owt.pos2 - 1) - 2 % 511232 = 0 THEN 'Is GAM Page'
WHEN RIGHT(owt.rd, owt.pos2 - 1) - 3 % 511232 = 0 THEN 'Is SGAM Page'
ELSE 'Is Not PFS, GAM, or SGAM page'
END AS
resource_type
FROM (SELECT owt.session_id,
owt.wait_type,
owt.wait_duration_ms,
owt.blocking_session_id,
owt.resource_description AS rd,
Charindex(':', owt.resource_description) AS pos1,
Charindex(':', Reverse(owt.resource_description)) AS pos2,
Len(owt.resource_description) AS ln
FROM sys.dm_os_waiting_tasks owt
WHERE owt.wait_type LIKE 'PAGELATCH%') owt
SELECT *
FROM #owt
IF EXISTS(SELECT 1 FROM #owt)
BEGIN
SELECT owt.session_id,
owt.wait_type,
owt.wait_duration_ms,
owt.rd,
owt.resource_type,
s.NAME + '.' + o.NAME AS table_name,
i.NAME AS index_name,
obd.page_level,
obd.row_count
FROM sys.dm_os_buffer_descriptors obd
INNER JOIN #owt owt
ON obd.database_id = owt.database_num
AND obd.[file_id] = owt.file_num
AND obd.page_id = owt.page_num
INNER JOIN sys.allocation_units au
ON obd.allocation_unit_id = au.allocation_unit_id
INNER JOIN sys.partitions p
ON au.container_id = p.[partition_id]
INNER JOIN sys.indexes i
ON p.index_id = i.index_id
AND p.[object_id] = i.[object_id]
INNER JOIN sys.objects o
ON i.[object_id] = o.[object_id]
INNER JOIN sys.schemas s
ON o.[schema_id] = s.[schema_id]
OPTION(force ORDER)
END;