forked from carlos-sierra/cscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs_blocked_sessions_ash_awr_pie.sql
More file actions
232 lines (232 loc) · 8.27 KB
/
cs_blocked_sessions_ash_awr_pie.sql
File metadata and controls
232 lines (232 loc) · 8.27 KB
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
----------------------------------------------------------------------------------------
--
-- File name: cs_blocked_sessions_ash_awr_pie.sql
--
-- Purpose: Top Session Blockers as per ASH from AWR (summary pie chart)
--
-- Author: Carlos Sierra
--
-- Version: 2020/12/06
--
-- Usage: Execute connected to CDB or PDB
--
-- Enter range of dates and filters when requested.
--
-- Example: $ sqlplus / as sysdba
-- SQL> @cs_blocked_sessions_ash_awr_pie.sql
--
-- Notes: Developed and tested on 12.1.0.2.
--
---------------------------------------------------------------------------------------
--
@@cs_internal/cs_primary.sql
@@cs_internal/cs_set.sql
@@cs_internal/cs_def.sql
@@cs_internal/cs_file_prefix.sql
--
DEF cs_script_name = 'cs_blocked_sessions_ash_awr_pie';
DEF cs_hours_range_default = '24';
--
@@cs_internal/cs_sample_time_from_and_to.sql
@@cs_internal/cs_snap_id_from_and_to.sql
--
--ALTER SESSION SET container = CDB$ROOT;
--
SELECT '&&cs_file_prefix._&&cs_script_name.' cs_file_name FROM DUAL;
--
DEF report_title = 'Root Blocker Status and its contribution to Blocked Sessions between &&cs_sample_time_from. and &&cs_sample_time_to. UTC';
DEF chart_title = '&&report_title.';
DEF xaxis_title = '';
DEF vaxis_title = '';
--
-- (isStacked is true and baseline is null) or (not isStacked and baseline >= 0)
DEF is_stacked = "isStacked: false,";
--DEF is_stacked = "isStacked: true,";
--DEF vaxis_baseline = ", baseline:&&cs_num_cpu_cores., baselineColor:'red'";
DEF vaxis_baseline = "";
DEF chart_foot_note_2 = '<br>2) ROOT BLOCKER SESSION STATUS "INACTIVE" means: Database is waiting for Application Host to release LOCK, while "UNKNOWN" could be a BACKGROUND session on CDB$ROOT.';
DEF chart_foot_note_3 = "<br>";
--DEF chart_foot_note_3 = "";
DEF chart_foot_note_4 = "";
DEF report_foot_note = 'SQL> @&&cs_script_name..sql "&&cs_sample_time_from." "&&cs_sample_time_to."';
--
DEF chart_foot_note_0 = '';
DEF chart_foot_note_1 = '';
-- [Line|Area|SteppedArea|Scatter]
DEF cs_chart_type = 'Pie';
DEF cs_chart_width = '900px';
DEF cs_chart_height = '450px';
DEF cs_chartarea_height = '80%';
-- disable explorer with "//" when using Pie
DEF cs_chart_option_explorer = '//';
-- enable pie options with "" when using Pie
DEF cs_chart_option_pie = '';
-- pieSliceText [{percentage}|value|label|none]
DEF cs_chart_pie_slice_text = "// pieSliceText: 'percentage',";
-- use oem colors
DEF cs_oem_colors_series = '//';
DEF cs_oem_colors_slices = '//';
-- for line charts
DEF cs_curve_type = '//';
--
@@cs_internal/cs_spool_head_chart.sql
--
PRO ,'VALUE'
PRO ]
--
SET HEA OFF PAGES 0;
/****************************************************************************************/
WITH
ash AS (
SELECT /*+ MATERIALIZE NO_MERGE */
h.sample_id,
h.sample_time,
h.machine,
h.session_id,
h.session_serial#,
h.blocking_session,
h.blocking_session_serial#,
h.session_state,
h.wait_class,
h.event,
h.sql_id,
h.top_level_sql_id
FROM dba_hist_active_sess_history h
WHERE h.sample_time >= TO_TIMESTAMP('&&cs_sample_time_from.', '&&cs_datetime_full_format.')
AND h.sample_time < TO_TIMESTAMP('&&cs_sample_time_to.', '&&cs_datetime_full_format.')
AND h.dbid = TO_NUMBER('&&cs_dbid.')
AND h.instance_number = TO_NUMBER('&&cs_instance_number.')
AND h.snap_id BETWEEN TO_NUMBER('&&cs_snap_id_from.') AND TO_NUMBER('&&cs_snap_id_to.')
),
inactive_sessions AS (
SELECT /*+ MATERIALIZE NO_MERGE */
DISTINCT
i.sample_id,
CAST(i.sample_time AS DATE) sample_time,
i.blocking_session session_id,
i.blocking_session_serial# session_serial#
FROM ash i
WHERE i.blocking_session IS NOT NULL
AND i.blocking_session_serial# IS NOT NULL
AND NOT EXISTS (
SELECT /*+ MATERIALIZE NO_MERGE */
NULL
FROM ash a
WHERE a.sample_id = i.sample_id
AND a.session_id = i.blocking_session
AND a.session_serial# = i.blocking_session_serial#
)),
all_sessions AS (
SELECT /*+ MATERIALIZE NO_MERGE */
a.sample_id, CAST(a.sample_time AS DATE) sample_time, a.machine, a.session_id, a.session_serial#, a.blocking_session, a.blocking_session_serial#,
'ACTIVE' status, session_state, wait_class, event, sql_id, top_level_sql_id
FROM ash a
UNION ALL
SELECT /*+ MATERIALIZE NO_MERGE */
i.sample_id, i.sample_time, NULL machine, i.session_id, i.session_serial#, TO_NUMBER(NULL), TO_NUMBER(NULL),
'INACTIVE or UNKNOWN' status, NULL session_state, NULL wait_class, NULL event, NULL sql_id, NULL top_level_sql_id
FROM inactive_sessions i
),
sess_history AS (
SELECT /*+ MATERIALIZE NO_MERGE */
sample_id, sample_time, machine, session_id, session_serial#, status, session_state, wait_class, event, sql_id, top_level_sql_id,
LEVEL lvl,
CONNECT_BY_ROOT machine blocker_machine,
CONNECT_BY_ROOT session_id blocker_session,
CONNECT_BY_ROOT session_serial# blocker_session_serial#,
CONNECT_BY_ISLEAF AS leaf
FROM all_sessions
START WITH blocking_session IS NULL AND blocking_session_serial# IS NULL
CONNECT BY sample_id = PRIOR sample_id AND blocking_session = PRIOR session_id AND blocking_session_serial# = PRIOR session_serial#
),
blockers AS (
SELECT /*+ MATERIALIZE NO_MERGE */
sample_id, sample_time, status, session_state, wait_class, event, sql_id, top_level_sql_id, session_id, session_serial#
FROM sess_history
WHERE lvl = 1
),
blockees AS (
SELECT /*+ MATERIALIZE NO_MERGE */
sample_id, sample_time, status, wait_class, event, blocker_session, blocker_session_serial#, COUNT(*) cnt
FROM sess_history
WHERE lvl > 1
GROUP BY
sample_id, sample_time, status, wait_class, event, blocker_session, blocker_session_serial#
),
machines AS (
SELECT /*+ MATERIALIZE NO_MERGE */
session_id, session_serial#, MAX(machine) machine
FROM sess_history
WHERE machine IS NOT NULL
GROUP BY
session_id, session_serial#
UNION
SELECT /*+ MATERIALIZE NO_MERGE */
sid session_id, serial# session_serial#, machine
FROM v$session
),
blockers_and_blockees AS (
SELECT /*+ MATERIALIZE NO_MERGE */
b.sample_id,
b.sample_time time,
a.wait_class||' - '||a.event AS wait_class_event,
b.status blocker_status,
b.session_state blocker_session_state,
b.wait_class blocker_wait_class,
b.event blocker_event,
COALESCE(b.sql_id, b.top_level_sql_id) AS blocker_sql_id,
NVL(m.machine, 'unknown') machine,
b.session_id blocker_session_id,
b.session_serial# blocker_session_serial#,
NVL(a.cnt, 0) sessions_blocked
FROM blockers b,
blockees a,
machines m
WHERE a.sample_id(+) = b.sample_id
AND a.sample_time(+) = b.sample_time
AND a.blocker_session(+) = b.session_id
AND a.blocker_session_serial#(+) = b.session_serial#
AND m.session_id(+) = b.session_id
AND m.session_serial#(+) = b.session_serial#
)
/****************************************************************************************/
,
detail AS (
SELECT /*+ MATERIALIZE NO_MERGE */
b.time,
b.sessions_blocked blocked,
b.blocker_session_id||','||b.blocker_session_serial# blocker,
b.machine blocker_machine,
CASE b.blocker_status WHEN 'INACTIVE or UNKNOWN' THEN (CASE b.machine WHEN '&&cs_host_name.' THEN 'UNKNOWN' ELSE 'INACTIVE' END) ELSE ('ACTIVE '||CASE b.blocker_session_state WHEN 'ON CPU' THEN b.blocker_session_state ELSE 'WAITING ON '||b.blocker_wait_class||' - '||b.blocker_event END) END blocker_status,
b.blocker_sql_id,
(SELECT s.sql_text FROM v$sql s WHERE s.sql_id = b.blocker_sql_id AND ROWNUM = 1) blocker_sql_text
FROM blockers_and_blockees b
WHERE b.sessions_blocked > 0
),
summary AS (
SELECT /*+ MATERIALIZE NO_MERGE */
blocker_status slice,
' ('||TRIM(TO_CHAR(100 * SUM(blocked) / SUM(SUM(blocked)) OVER (), '990.0'))||'%)' percent,
SUM(blocked) value
FROM detail
GROUP BY
blocker_status
)
SELECT ', ['''||slice||percent||''','||value||']'
FROM summary
ORDER BY
value DESC
/
/****************************************************************************************/
SET HEA ON PAGES 100;
--
@@cs_internal/cs_spool_id_chart.sql
@@cs_internal/cs_spool_tail_chart.sql
PRO
PRO &&report_foot_note.
--
--ALTER SESSION SET CONTAINER = &&cs_con_name.;
--
@@cs_internal/cs_undef.sql
@@cs_internal/cs_reset.sql
--