forked from carlos-sierra/cscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs_ash_mem_sample_report.sql
More file actions
365 lines (365 loc) · 15.8 KB
/
cs_ash_mem_sample_report.sql
File metadata and controls
365 lines (365 loc) · 15.8 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
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
----------------------------------------------------------------------------------------
--
-- File name: am.sql | cs_ash_mem_sample_report.sql
--
-- Purpose: Detailed List of ASH Samples from MEM
--
-- Author: Carlos Sierra
--
-- Version: 2021/11/07
--
-- Usage: Execute connected to CDB or PDB.
--
-- Enter optional parameters when requested.
--
-- Example: $ sqlplus / as sysdba
-- SQL> @cs_ash_mem_sample_report.sql
--
-- Notes: *** Requires Oracle Diagnostics Pack License ***
--
-- Developed and tested on 12.1.0.2.
--
---------------------------------------------------------------------------------------
--
@@cs_internal/cs_primary.sql
@@cs_internal/cs_cdb_warn.sql
@@cs_internal/cs_set.sql
SET PAGES 5000;
@@cs_internal/cs_def.sql
@@cs_internal/cs_file_prefix.sql
--
DEF cs_script_name = 'cs_ash_mem_sample_report';
DEF cs_script_acronym = 'am.sql | ';
--
SELECT '&&cs_file_prefix._&&cs_script_name.' cs_file_name FROM DUAL;
--
DEF cs_hours_range_default = '3';
@@cs_internal/cs_sample_time_from_and_to.sql
@@cs_internal/cs_snap_id_from_and_to.sql
--
PRO 3. Machine (opt):
DEF cs2_machine = '&3.';
UNDEF 3;
--
PRO
PRO 4. SQL_ID (opt):
DEF cs_sql_id = '&4.';
UNDEF 4;
--
PRO
PRO 5. SID,SERIAL (opt):
DEF cs_sid_serial = '&5.';
UNDEF 5;
--
PRO
PRO 6. Only LOB DEDUP TX 4 waiting sessions [{N}|Y]:
DEF cs_only_dedup = '&6.';
UNDEF 6;
COL cs_only_dedup NEW_V cs_only_dedup NOPRI;
SELECT CASE WHEN SUBSTR(TRIM(UPPER('&&cs_only_dedup.')), 1, 1) IN ('N', 'Y') THEN SUBSTR(TRIM(UPPER('&&cs_only_dedup.')), 1, 1) ELSE 'N' END AS cs_only_dedup FROM DUAL
/
--
@@cs_internal/cs_spool_head.sql
PRO SQL> @&&cs_script_name..sql "&&cs_sample_time_from." "&&cs_sample_time_to." "&&cs2_machine." "&&cs_sql_id." "&&cs_sid_serial." "&&cs_only_dedup."
@@cs_internal/cs_spool_id.sql
--
@@cs_internal/cs_spool_id_sample_time.sql
--
PRO MACHINE : "&&cs2_machine."
PRO SQL_ID : "&&cs_sql_id."
PRO SID,SERIAL : "&&cs_sid_serial."
PRO ONLY_DEDUP : "&&cs_only_dedup."
--
DEF times_cpu_cores = '1';
DEF include_hist = 'N';
DEF include_mem = 'Y';
PRO
PRO Sum of Active Sessions per sampled time (spikes greater than &&cs_num_cpu_cores. CPU Cores)
PRO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SET SERVEROUT ON;
@@cs_internal/cs_active_sessions_peaks_internal_v5.sql
@@cs_internal/cs_active_sessions_peaks_internal_v6.sql
--
DEF times_cpu_cores = '0';
DEF include_hist = 'N';
DEF include_mem = 'Y';
PRO
PRO Sum of Active Sessions per sampled time
PRO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SET SERVEROUT ON;
@@cs_internal/cs_active_sessions_peaks_internal_v5.sql
--
CL BREAK
COL sql_text FOR A80 HEA 'SQL Text' TRUNC;
COL module_action_program FOR A80 HEA 'Module Action Program' TRUNC;
COL sample_date_time FOR A23 HEA 'Sample Date and Time';
COL samples FOR 9999,999 HEA 'Active|Sessions';
COL on_cpu_or_wait_class FOR A14 HEA 'ON CPU or|Wait Class';
COL on_cpu_or_wait_event FOR A50 HEA 'ON CPU or Timed Event';
COL session_serial FOR A16 HEA 'Session,Serial';
COL machine FOR A60 HEA 'Application Server';
COL con_id FOR 999999;
COL plans FOR 99999 HEA 'Plans';
COL sessions FOR 9999,999 HEA 'Sessions|this SQL';
COL pdb_name FOR A30 HEA 'PDB Name' TRUNC;
COL sql_id FOR A13 HEA 'Top|SQL_ID';
-- --
-- PRO
-- PRO ASH spikes by sample time and top SQL (spikes higher than &&cs_num_cpu_cores. cpu cores)
-- PRO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- WITH
-- ash_by_sample_and_sql AS (
-- SELECT /*+ MATERIALIZE NO_MERGE */
-- h.sample_time,
-- h.sql_id,
-- c.name AS pdb_name,
-- h.con_id,
-- COUNT(*) samples,
-- COUNT(DISTINCT h.sql_plan_hash_value) plans,
-- ROW_NUMBER () OVER (PARTITION BY h.sample_time ORDER BY COUNT(*) DESC NULLS LAST, h.sql_id) row_number
-- FROM v$active_session_history h, v$containers c
-- 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 ('&&cs2_machine.' IS NULL OR h.machine LIKE '%&&cs2_machine.%')
-- AND ('&&cs_sql_id.' IS NULL OR h.sql_id = '&&cs_sql_id.')
-- AND ('&&cs_sid_serial.' IS NULL OR INSTR('&&cs_sid_serial.', h.session_id||','||h.session_serial#) > 0)
-- AND ('&&cs_only_dedup.' = 'N' OR (h.event = 'enq: TX - row lock contention' AND h.p1text LIKE 'name|mode%' AND h.p1 > 0 AND TO_CHAR(BITAND(h.p1, 65535)) = '4' AND h.current_obj# > 0 AND h.xid IS NOT NULL))
-- AND c.con_id(+) = h.con_id
-- GROUP BY
-- h.sample_time,
-- h.sql_id,
-- c.name,
-- h.con_id
-- )
-- SELECT TO_CHAR(h.sample_time, '&&cs_timestamp_full_format.') sample_date_time,
-- SUM(h.samples) samples,
-- '|' AS "|",
-- MAX(CASE h.row_number WHEN 1 THEN h.sql_id END) sql_id,
-- SUM(CASE h.row_number WHEN 1 THEN h.samples ELSE 0 END) sessions,
-- MAX(CASE WHEN h.row_number = 1 AND h.sql_id IS NOT NULL THEN h.plans END) plans,
-- MAX(CASE h.row_number WHEN 1 THEN h.con_id END) con_id,
-- MAX(CASE h.row_number WHEN 1 THEN h.pdb_name END) AS pdb_name,
-- MAX(CASE WHEN h.row_number = 1 AND h.sql_id IS NOT NULL THEN (SELECT SUBSTR(q.sql_text, 1, 100) FROM v$sqlstats q WHERE q.sql_id = h.sql_id AND ROWNUM = 1) END) sql_text
-- FROM ash_by_sample_and_sql h
-- GROUP BY
-- h.sample_time
-- HAVING SUM(h.samples) >= &&cs_num_cpu_cores.
-- ORDER BY
-- h.sample_time
-- /
-- --
-- PRO
-- PRO ASH by sample time and top SQL
-- PRO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- WITH
-- ash_by_sample_and_sql AS (
-- SELECT /*+ MATERIALIZE NO_MERGE */
-- h.sample_time,
-- h.sql_id,
-- c.name AS pdb_name,
-- h.con_id,
-- COUNT(*) samples,
-- COUNT(DISTINCT h.sql_plan_hash_value) plans,
-- ROW_NUMBER () OVER (PARTITION BY h.sample_time ORDER BY COUNT(*) DESC NULLS LAST, h.sql_id) row_number
-- FROM v$active_session_history h, v$containers c
-- 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 ('&&cs2_machine.' IS NULL OR h.machine LIKE '%&&cs2_machine.%')
-- AND ('&&cs_sql_id.' IS NULL OR h.sql_id = '&&cs_sql_id.')
-- AND ('&&cs_sid_serial.' IS NULL OR INSTR('&&cs_sid_serial.', h.session_id||','||h.session_serial#) > 0)
-- AND ('&&cs_only_dedup.' = 'N' OR (h.event = 'enq: TX - row lock contention' AND h.p1text LIKE 'name|mode%' AND h.p1 > 0 AND TO_CHAR(BITAND(h.p1, 65535)) = '4' AND h.current_obj# > 0 AND h.xid IS NOT NULL))
-- AND c.con_id(+) = h.con_id
-- GROUP BY
-- h.sample_time,
-- h.sql_id,
-- c.name,
-- h.con_id
-- )
-- SELECT TO_CHAR(h.sample_time, '&&cs_timestamp_full_format.') sample_date_time,
-- SUM(h.samples) samples,
-- '|' AS "|",
-- MAX(CASE h.row_number WHEN 1 THEN h.sql_id END) sql_id,
-- SUM(CASE h.row_number WHEN 1 THEN h.samples ELSE 0 END) sessions,
-- MAX(CASE WHEN h.row_number = 1 AND h.sql_id IS NOT NULL THEN h.plans END) plans,
-- MAX(CASE h.row_number WHEN 1 THEN h.con_id END) con_id,
-- MAX(CASE h.row_number WHEN 1 THEN h.pdb_name END) AS pdb_name,
-- MAX(CASE WHEN h.row_number = 1 AND h.sql_id IS NOT NULL THEN (SELECT SUBSTR(q.sql_text, 1, 100) FROM v$sqlstats q WHERE q.sql_id = h.sql_id AND ROWNUM = 1) END) sql_text
-- FROM ash_by_sample_and_sql h
-- GROUP BY
-- h.sample_time
-- ORDER BY
-- h.sample_time
-- /
--
COL sql_id FOR A13 HEA 'SQL_ID';
COL blocking_session_status FOR A11 HEA 'Blocker|Session|Status';
COL sql_plan_hash_value FOR 9999999999 HEA 'Plan|Hash Value';
COL sql_plan_line_id FOR 9999 HEA 'Plan|Line';
COL sql_child_number FOR 999999 HEA 'Child|Number';
COL sql_exec_id FOR 99999999 HEA 'Exec ID';
COL xid FOR A16 HEA 'Transaction ID';
COL current_obj# FOR 9999999999 HEA 'Current|Obj#';
COL current_file# FOR 9999999999 HEA 'Current|File#';
COL current_block# FOR 9999999999 HEA 'Current|Block#';
COL current_row# FOR 9999999999 HEA 'Current|Row#';
COL in_connection_mgmt FOR A6 HEA 'In|Connec|Mgmt';
COL in_parse FOR A6 HEA 'In|Parse';
COL in_hard_parse FOR A6 HEA 'In|Hard|Parse';
COL in_sql_execution FOR A6 HEA 'In|SQL|Exec';
COL in_plsql_execution FOR A6 HEA 'In|PLSQL|Exec';
COL in_plsql_rpc FOR A6 HEA 'In|PLSQL|RPC';
COL in_plsql_compilation FOR A6 HEA 'In|PLSQL|Compil';
COL in_java_execution FOR A6 HEA 'In|Java|Exec';
COL in_bind FOR A6 HEA 'In|Bind';
COL in_cursor_close FOR A6 HEA 'In|Cursor|Close';
COL in_sequence_load FOR A6 HEA 'In|Seq|Load';
COL top_level_sql_id FOR A13 HEA 'Top Level|SQL_ID';
COL is_sqlid_current FOR A4 HEA 'Is|SQL|Exec';
COL blocking_session_serial FOR A16 HEA 'Blocker|Session,Serial';
COL blocking2_session_serial FOR A16 HEA 'Blocker(2)|Session,Serial';
COL blocking3_session_serial FOR A16 HEA 'Blocker(3)|Session,Serial';
COL blocking4_session_serial FOR A16 HEA 'Blocker(4)|Session,Serial';
COL blocking5_session_serial FOR A16 HEA 'Blocker(5)|Session,Serial';
COL blocking_machine FOR A60 HEA 'Application Server (blocker)';
COL deadlock FOR A4 HEA 'Dead|Lock';
COL lock_type FOR A4 HEA 'Lock';
COL lock_mode FOR A4 HEA 'Mode';
COL p1_p2_p3 FOR A100 HEA 'P1, P2, P3';
COL current_object_name FOR A40 HEA 'Current|Object Name (Object Type)' TRUNC;
COL secs_waited FOR 990.000 HEA 'Secs|Waited';
COL spid FOR A6;
COL pname FOR A5;
--
BREAK ON sample_date_time SKIP PAGE ON machine SKIP 1;
PRO
PRO ASH by sample time, appl server, session and SQL_ID
PRO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WITH
sess_proc AS (
SELECT /*+ MATERIALIZE NO_MERGE */ DISTINCT s.sid, s.serial#, p.spid, p.pname FROM v$session s, v$process p WHERE p.addr = s.paddr
),
sqlstats AS (
SELECT /*+ MATERIALIZE NO_MERGE */ DISTINCT s.sql_id, s.sql_text FROM v$sqlstats s
),
ash AS (
SELECT /*+ MATERIALIZE NO_MERGE */ h.*, c.name AS pdb_name
FROM v$active_session_history h, v$containers c
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 ('&&cs2_machine.' IS NULL OR h.machine LIKE '%&&cs2_machine.%')
AND ('&&cs_sql_id.' IS NULL OR h.sql_id = '&&cs_sql_id.')
AND ('&&cs_only_dedup.' = 'N' OR (h.event = 'enq: TX - row lock contention' AND h.p1text LIKE 'name|mode%' AND h.p1 > 0 AND TO_CHAR(BITAND(h.p1, 65535)) = '4' AND h.current_obj# > 0 AND h.xid IS NOT NULL))
AND c.con_id(+) = h.con_id
),
sess AS (
SELECT /*+ MATERIALIZE NO_MERGE */
session_id,
session_serial#,
MAX(machine) machine
FROM ash
GROUP BY
session_id,
session_serial#
)
SELECT /*+ MONITOR GATHER_PLAN_STATISTICS */
TO_CHAR(h.sample_time, '&&cs_timestamp_full_format.') sample_date_time,
h.machine,
h.con_id,
h.pdb_name,
sp.pname,
sp.spid,
's:'||h.session_id||','||h.session_serial# session_serial,
h.blocking_session_status,
CASE WHEN (h.session_id, h.session_serial#) IN ((b.blocking_session, b.blocking_session_serial#), (b2.blocking_session, b2.blocking_session_serial#)) THEN 'DL?' END deadlock,
CASE WHEN h.blocking_session IS NOT NULL THEN 'b:'||h.blocking_session||','||h.blocking_session_serial# END blocking_session_serial,
CASE WHEN b.blocking_session IS NOT NULL THEN 'b2:'||b.blocking_session||','||b.blocking_session_serial# END blocking2_session_serial,
CASE WHEN b2.blocking_session IS NOT NULL THEN 'b3:'||b2.blocking_session||','||b2.blocking_session_serial# END blocking3_session_serial,
CASE WHEN b3.blocking_session IS NOT NULL THEN 'b4:'||b3.blocking_session||','||b3.blocking_session_serial# END blocking4_session_serial,
CASE WHEN b4.blocking_session IS NOT NULL THEN 'b5:'||b4.blocking_session||','||b4.blocking_session_serial# END blocking5_session_serial,
CASE
WHEN b4.blocking_session IS NOT NULL THEN (SELECT s.machine FROM sess s WHERE s.session_id = b4.blocking_session AND s.session_serial# = b4.blocking_session_serial#)
WHEN b3.blocking_session IS NOT NULL THEN (SELECT s.machine FROM sess s WHERE s.session_id = b3.blocking_session AND s.session_serial# = b3.blocking_session_serial#)
WHEN b2.blocking_session IS NOT NULL THEN (SELECT s.machine FROM sess s WHERE s.session_id = b2.blocking_session AND s.session_serial# = b2.blocking_session_serial#)
WHEN b.blocking_session IS NOT NULL THEN (SELECT s.machine FROM sess s WHERE s.session_id = b.blocking_session AND s.session_serial# = b.blocking_session_serial#)
WHEN h.blocking_session IS NOT NULL THEN (SELECT s.machine FROM sess s WHERE s.session_id = h.blocking_session AND s.session_serial# = h.blocking_session_serial#)
END blocking_machine,
h.sql_id,
h.is_sqlid_current,
h.sql_plan_hash_value,
h.sql_plan_line_id,
h.sql_child_number,
h.sql_exec_id,
h.xid,
h.top_level_sql_id,
CASE h.session_state WHEN 'ON CPU' THEN h.session_state ELSE h.wait_class||' - '||h.event END on_cpu_or_wait_event,
st.sql_text,
SUBSTR(
CASE WHEN TRIM(h.module) IS NOT NULL THEN 'm:'||TRIM(h.module)||' ' END||
CASE WHEN TRIM(h.action) IS NOT NULL THEN 'a:'||TRIM(h.action)||' ' END||
CASE WHEN TRIM(h.program) IS NOT NULL THEN 'p:'||TRIM(h.program) END
, 1, 100) AS module_action_program,
h.current_obj#,
h.current_file#,
h.current_block#,
h.current_row#,
h.in_connection_mgmt,
h.in_parse,
h.in_hard_parse,
h.in_sql_execution,
h.in_plsql_execution,
h.in_plsql_rpc,
h.in_plsql_compilation,
h.in_java_execution,
h.in_bind,
h.in_cursor_close,
h.in_sequence_load,
CASE WHEN h.event LIKE 'enq:%' AND h.p1text LIKE 'name|mode%' AND h.p1 > 0 THEN CHR(BITAND(h.p1,-16777216)/16777215)||CHR(BITAND(h.p1, 16711680)/65535) END AS lock_type,
CASE WHEN h.event LIKE 'enq:%' AND h.p1text LIKE 'name|mode%' AND h.p1 > 0 THEN TO_CHAR(BITAND(h.p1, 65535)) END AS lock_mode,
NVL2(TRIM(h.p1text), h.p1text||':'||h.p1, NULL)||NVL2(TRIM(h.p2text), ', '||h.p2text||':'||h.p2, NULL)||NVL2(TRIM(h.p3text), ', '||h.p3text||':'||h.p3, NULL) p1_p2_p3
FROM ash h,
ash b,
ash b2,
ash b3,
ash b4,
sess_proc sp,
sqlstats st
WHERE b.sample_id(+) = h.sample_id
AND b.session_id(+) = h.blocking_session
AND b.session_serial#(+) = h.blocking_session_serial#
AND b2.sample_id(+) = b.sample_id
AND b2.session_id(+) = b.blocking_session
AND b2.session_serial#(+) = b.blocking_session_serial#
AND b3.sample_id(+) = b2.sample_id
AND b3.session_id(+) = b2.blocking_session
AND b3.session_serial#(+) = b2.blocking_session_serial#
AND b4.sample_id(+) = b3.sample_id
AND b4.session_id(+) = b3.blocking_session
AND b4.session_serial#(+) = b3.blocking_session_serial#
AND ( '&&cs_sid_serial.' IS NULL
OR 's:'||h.session_id||','||h.session_serial# LIKE '%&&cs_sid_serial.%'
OR 'b:'||h.blocking_session||','||h.blocking_session_serial# LIKE '%&&cs_sid_serial.%'
OR 'b2:'||b.blocking_session||','||b.blocking_session_serial# LIKE '%&&cs_sid_serial.%'
OR 'b3:'||b2.blocking_session||','||b2.blocking_session_serial# LIKE '%&&cs_sid_serial.%'
OR 'b4:'||b3.blocking_session||','||b3.blocking_session_serial# LIKE '%&&cs_sid_serial.%'
OR 'b5:'||b4.blocking_session||','||b4.blocking_session_serial# LIKE '%&&cs_sid_serial.%'
)
AND sp.sid(+) = h.session_id
AND sp.serial#(+) = h.session_serial#
AND st.sql_id(+) = h.sql_id
ORDER BY
h.sample_time,
CASE WHEN h.machine LIKE '%iod-%' THEN 1 ELSE 2 END,
h.machine,
h.con_id,
h.session_id,
h.session_serial#,
h.sql_id
/
--
PRO
PRO SQL> @&&cs_script_name..sql "&&cs_sample_time_from." "&&cs_sample_time_to." "&&cs2_machine." "&&cs_sql_id." "&&cs_sid_serial." "&&cs_only_dedup."
--
@@cs_internal/cs_spool_tail.sql
@@cs_internal/cs_undef.sql
@@cs_internal/cs_reset.sql
--