1. 現象
某天收到一數據庫活動會話數劇增不降,業務系統部分功能緩慢。
2. 分析
2.1 分析活動會話數變化趨勢
select to_char(ash.sample_time, 'YYYY-MM-DD HH24:MI:SS') SAMPLE_TIME ,count(*) cnt from dba_hist_active_sess_history ash where ash.instance_number=1 and ash.wait_class <> 'Idle' /* 非空閑回話 **/ and ash.sample_time between sysdate -1/2 and sysdate group by SAMPLE_TIME having count(*) >80 order by SAMPLE_TIME ;
2.2 抽一個時間點分析等待事件及SQL執行情況
select to_char(ash.sample_time, 'YYYY-MM-DD HH24:MI:SS') SAMPLE_TIME ,ash.sql_id ,ash.event ,count(*) CNT /* SQL的數量 **/ ,TRUNC(SUM(TIME_WAITED) / 1000000,2) SECONDS_IN_WAIT /* SQL的等待時間 **/ ,SUM(to_number(CAST(ash.sample_time AS DATE)-ash.sql_exec_start) *24*60*60 ) SECONDS_IN_EXECUTE /* SQL的執行時間 **/ from dba_hist_active_sess_history ash where ash.instance_number=1 and ash.wait_class <> 'Idle' and to_char(ash.sample_time,'YYYY-MM-DD HH24:MI:SS') = '2016-12-01 09:28:27' group by ash.sample_time ,ash.sql_id ,ash.event having count(*) > 5 order by SAMPLE_TIME,4 ;
-- 發現174r8w7amsr17,83zyfck594vfk SQL執行時間較長,特別是174r8w7amsr17這條語句執行5+分鍾
2.3 分析語句執行過程
-- 83zyfck594vfk 語句執行情況 col event for a32 col p1text for a18 col p2text for a18 col p3text for a18 select to_char(ash.sample_time, 'YYYY-MM-DD HH24:MI:SS') SAMPLE_TIME ,ash.event ,ash.sql_id ,ash.blocking_inst_id ,ash.blocking_session ,ash.blocking_session_serial# ,count(*) cnt from dba_hist_active_sess_history ash where ash.instance_number=1 and ash.wait_class <> 'idle' and ash.sql_id = '83zyfck594vfk' and to_char(ash.sample_time,'YYYY-MM-DD HH24:MI:SS') = '2016-12-01 09:28:27' group by sample_time ,ash.event ,ash.sql_id ,ash.blocking_inst_id ,ash.blocking_session ,ash.blocking_session_serial# order by sample_time,7 ;
-- 發現session(1:4448,58047)阻塞了5個會話,下圖
2.4 分析下4448,58047會話的歷史執行情況
select to_char(ash.sample_time, 'YYYY-MM-DD HH24:MI:SS') sample_time ,to_char(ash.sql_exec_start,'YYYY-MM-DD HH24:MI:SS') SQL_START_TIME ,ash.instance_number inst_id ,ash.session_id sid ,ash.session_serial# serial ,ash.blocking_inst_id b_inst_id ,ash.blocking_session b_sid ,ash.blocking_session_serial# b_serial ,ash.sql_id ,ash.event ,to_number(CAST(ash.sample_time AS DATE)-ash.sql_exec_start) *24*60*60 SECONDS_IN_EXECUTE ,ash.xid from dba_hist_active_sess_history ash where and ash.instance_number=1 and ash.session_id = 4448 and ash.session_serial# = 58047 order by sample_time ;
-- session(1:4448,58047) 從2016-12-01 09:25:33 建立了一個事務 (xid = '02FB001B0000619A') 持續到 2016-12-01 09:47:49 , 至少22分鍾里面執行 83zyfck594vfk 語句134次才提交,而每次執行時間挺小的,但整個事務執行時間過長,持續20多分鍾時間。而且83zyfck594vfk 語句執行存在“BY LOCAL INDEX ROWID”回表或是“INDEX SKIP SCAN ”操作,而索引跳躍掃描的性能不好。需要優化。
2.5 分析174r8w7amsr17語句是被前面這類多個83zyfck594vfk SQL長時間事務阻塞
3. 處理方案
3.1 優化83zyfck594vfk語句的索引;
3.2 檢查業務83zyfck594vfk功能模塊,分割大事務成多個小事務,減少阻塞。