Oracle導致Redo日誌暴增的SQL語句排查
阿新 • • 發佈:2021-11-24
概述
一、Oracle查詢最近幾天每小時歸檔日誌產生數量的指令碼寫法
SELECT SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH:MI:SS'), 1, 5) Day, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '00', 1, 0)) H00, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '01', 1, 0)) H01,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '02', 1, 0)) H02, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '03', 1, 0)) H03, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '04', 1, 0)) H04, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '05', 1, 0)) H05, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '06', 1, 0)) H06, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '07', 1, 0)) H07, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '08', 1, 0)) H08, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '09', 1, 0)) H09, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '10', 1, 0)) H10, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '11', 1, 0)) H11, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '12', 1, 0)) H12, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '13', 1, 0)) H13, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '14', 1, 0)) H14, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '15', 1, 0)) H15, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '16', 1, 0)) H16, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '17', 1, 0)) H17, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '18', 1, 0)) H18, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '19', 1, 0)) H19, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '20', 1, 0)) H20, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '21', 1, 0)) H21, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '22', 1, 0)) H22, SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'), 10, 2), '23', 1, 0)) H23, COUNT(*) TOTAL FROM v$log_history a WHERE first_time >= to_char(sysdate - 10) GROUP BY SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH:MI:SS'), 1, 5) ORDER BY SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH:MI:SS'), 1, 5) DESC;
查詢結果
二、檢視最近2小時"塊改變"最多的segment
redo大量產生必然是由於大量產生"塊改變"。從awr檢視中找到"塊改變"最多的segment。
這是查詢最近2小時(120分鐘)的,begin_interval_time> sysdate - 120/1440,大家也可以自定義修改查詢最近多少分鐘的。
1 select * 2 from (SELECT to_char(begin_interval_time, 'YYYY_MM_DD HH24:MI') snap_time, 3 dhsso.object_name, 4 SUM(db_block_changes_delta) 5 FROM dba_hist_seg_stat dhss, 6 dba_hist_seg_stat_obj dhsso, 7 dba_hist_snapshot dhs 8 WHERE dhs.snap_id = dhss.snap_id 9 AND dhs.instance_number = dhss.instance_number 10 AND dhss.obj# = dhsso.obj# 11 AND dhss.dataobj# = dhsso.dataobj# 12 AND begin_interval_time > sysdate - 120 / 1440 13 GROUP BY to_char(begin_interval_time, 'YYYY_MM_DD HH24:MI'), dhsso.object_name 14 order by 3 desc) 15 where rownum <= 5;
查詢結果:
三、從awr檢視中找出步驟1中排序靠前的物件涉及的SQL
說明:LIKE '%MON_MODS$%'中MON_MODS是步驟1中查詢出來的OBJECT_NAME
1 SELECT to_char(begin_interval_time, 'YYYY_MM_DD HH24:MI'), 2 dbms_lob.substr(sql_text, 4000, 1), 3 dhss.instance_number, 4 dhss.sql_id, 5 executions_delta, 6 rows_processed_delta 7 FROM dba_hist_sqlstat dhss, 8 dba_hist_snapshot dhs, 9 dba_hist_sqltext dhst 10 WHERE UPPER(dhst.sql_text) LIKE '%MON_MODS$%' 11 AND dhss.snap_id = dhs.snap_id 12 AND dhss.instance_Number = dhs.instance_number 13 AND dhss.sql_id = dhst.sql_id;
查詢結果
四、從ASH相關檢視找到執行這些SQL的session、module和machine
1 select * from dba_hist_active_sess_history WHERE sql_id = 'c9n8kv7afchtd'; 2 select * from v$active_session_history where sql_Id = 'c9n8kv7afchtd';
c9n8kv7afchtd是SQL_ID,替換第二步查詢的結果SQL_ID列