【SQL監控】SQL完全監控的指令碼

路途中的人2012發表於2017-05-12

【SQL監控】SQL完全監控的指令碼


  
    有網友詢問我的這篇blog  http://blog.itpub.net/26736162/viewspace-1218671/  中的檢視 vw_sql_rubbish_monitor_lhr 的內容,我在這裡列舉一下,大家共同學習吧。






   由於要做論文,所以,把這篇文章裡的內容又修改了一下,原有檢視vw_sql_rubbish_monitor_lhr 修改為VW_SQL_PP_LHR,並且提供所有的SQL監控指令碼。




   

點選(此處)摺疊或開啟

  1. SET SQLBLANKLINES ON

  2. --------------------------------- 歷史SQL記錄
  3. DROP TABLE XB_SQL_MONITOR_LHR PURGE;
  4. CREATE TABLE XB_SQL_MONITOR_LHR
  5. (
  6.   ID INTEGER PRIMARY KEY,
  7.   INST_ID NUMBER,
  8.   SID NUMBER,
  9.   SERIAL# NUMBER,
  10.   SPID NUMBER,
  11.   SQL_ID VARCHAR2(13),
  12.   SQL_TEXT VARCHAR2(4000),
  13.   SQL_FULLTEXT CLOB,
  14.   SQL_EXEC_START DATE,
  15.   SQL_EXEC_ID NUMBER,
  16.   COMMAND_TYPE VARCHAR2(20),
  17.   ELAPSED_TIME NUMBER,
  18.   ELAPSED_TIME2 VARCHAR2(30),
  19.   STATUS VARCHAR2(19),
  20.   USERNAME VARCHAR2(30),
  21.   OS_USER     VARCHAR2(30),
  22.   SESSION_TYPES VARCHAR2(4000),
  23.   LAST_LOAD_TIME DATE,
  24.   LAST_ACTIVE_TIME DATE,
  25.   EXECUTIONS NUMBER ,
  26.   PX_QCSID NUMBER,
  27.   CPU_TIME NUMBER,
  28.   FETCHES NUMBER,
  29.   BUFFER_GETS NUMBER,
  30.   DISK_READS NUMBER,
  31.   DIRECT_WRITES NUMBER,
  32.   BINDS_XML CLOB,
  33.   USER_IO_WAIT_TIME NUMBER,
  34.   CONCURRENCY_WAIT_TIME NUMBER,
  35.   PHYSICAL_READ_BYTES NUMBER,
  36.   PHYSICAL_WRITE_BYTES NUMBER,
  37.   KEY NUMBER,
  38.   PLAN_OBJECT_OWNER VARCHAR2(50),
  39.   PLAN_OBJECT_NAME VARCHAR2(50),
  40.   IN_DATE DATE
  41. ) NOLOGGING
  42. PARTITION BY RANGE(IN_DATE)
  43.    INTERVAL(NUMTOYMINTERVAL(1,'MONTH'))
  44.    (PARTITION P201704 VALUES LESS THAN(TO_DATE('201705','YYYYMM')));
  45.                                                               
  46. DROP SEQUENCE S_XB_SQL_MONITOR_LHR;
  47. CREATE SEQUENCE S_XB_SQL_MONITOR_LHR START WITH 1 INCREMENT BY 1 CACHE 1000;
  48. SELECT S_XB_SQL_MONITOR_LHR.NEXTVAL FROM DUAL;
  49.  
  50. CREATE INDEX IND_SQL_MONITOR_SQLID ON XB_SQL_MONITOR_LHR(SQL_ID) LOCAL NOLOGGING;
  51. CREATE INDEX IND_SQL_MONITOR_SID ON XB_SQL_MONITOR_LHR(SID,SERIAL#,SPID) LOCAL NOLOGGING;
  52. CREATE INDEX IND_SQL_MONITOR_IN_DATE ON XB_SQL_MONITOR_LHR(IN_DATE,COMMAND_TYPE,PLAN_OBJECT_NAME) LOCAL NOLOGGING;

  53.                           

  54. COMMENT ON TABLE XB_SQL_MONITOR_LHR IS '歷史SQL監控';
  55. COMMENT ON COLUMN XB_SQL_MONITOR_LHR.SQL_EXEC_START IS 'SQL語句開始執行時間';
  56. COMMENT ON COLUMN XB_SQL_MONITOR_LHR.ELAPSED_TIME IS 'SQL語句執行時間(微秒)';
  57. COMMENT ON COLUMN XB_SQL_MONITOR_LHR.EXECUTIONS IS 'SQL語句執行次數';
  58.                                                      
  59. GRANT SELECT ON XB_SQL_MONITOR_LHR TO PUBLIC;


  60. --------------------------------- 歷史SQL執行計劃記錄
  61. DROP TABLE XB_SQL_PLAN_MONITOR_LHR PURGE;
  62. CREATE TABLE XB_SQL_PLAN_MONITOR_LHR (
  63.         ID NUMBER PRIMARY KEY,
  64.     INST_ID NUMBER,
  65.         SQL_MONITOR_ID NUMBER,
  66.         KEY NUMBER,
  67.         STATUS VARCHAR2(25),
  68.         SID NUMBER,
  69.     SERIAL# NUMBER,
  70.     SPID NUMBER,
  71.         SQL_ID VARCHAR2(25),
  72.         SQL_EXEC_START DATE,
  73.     SQL_EXEC_ID NUMBER,
  74.     SQL_PLAN_HASH_VALUE NUMBER ,
  75.     SQL_CHILD_ADDRESS RAW(8),
  76.     CHILD_NUMBER NUMBER,
  77.     PLAN_PARENT_ID NUMBER,
  78.     PLAN_LINE_ID NUMBER,
  79.     PLAN_OPERATION VARCHAR2(30),
  80.     PLAN_OPTIONS VARCHAR2(30),
  81.     OPTIMIZER VARCHAR2(80),
  82.     OBJECT# NUMBER,
  83.     PLAN_OBJECT_OWNER VARCHAR2(30),
  84.     PLAN_OBJECT_NAME VARCHAR2(30),
  85.     PLAN_OBJECT_TYPE VARCHAR2(40),
  86.     OBJECT_ALIAS VARCHAR2(80),
  87.     PLAN_DEPTH NUMBER,
  88.     PLAN_POSITION NUMBER,
  89.     PLAN_COST NUMBER,
  90.     PLAN_CARDINALITY NUMBER,
  91.     PLAN_BYTES NUMBER,
  92.     PLAN_TIME NUMBER,
  93.     PLAN_PARTITION_START VARCHAR2(255) ,
  94.     PLAN_PARTITION_STOP VARCHAR2(255),
  95.     PLAN_CPU_COST NUMBER,
  96.     PLAN_IO_COST NUMBER,
  97.     PLAN_TEMP_SPACE NUMBER,
  98.     STARTS NUMBER,
  99.     OUTPUT_ROWS NUMBER,
  100.     IO_INTERCONNECT_BYTES NUMBER,
  101.     PHYSICAL_READ_REQUESTS NUMBER,
  102.     PHYSICAL_READ_BYTES NUMBER,
  103.     PHYSICAL_WRITE_REQUESTS NUMBER,
  104.     PHYSICAL_WRITE_BYTES NUMBER,
  105.     SEARCH_COLUMNS NUMBER,
  106.         FILTER_PREDICATES VARCHAR2(4000) ,
  107.         ACCESS_PREDICATES VARCHAR2(4000) ,
  108.         PROJECTION VARCHAR2(4000) ,
  109.         OTHER_XML CLOB,
  110.     IN_DATE DATE
  111. ) NOLOGGING
  112. PARTITION BY RANGE(IN_DATE)
  113.     INTERVAL(NUMTOYMINTERVAL(1,'MONTH') )
  114.   (PARTITION P201704 VALUES LESS THAN (TO_DATE('201705', 'YYYYMM'))
  115. );

  116. DROP SEQUENCE S_XB_SQL_PLAN_MONITOR_LHR;
  117. CREATE SEQUENCE S_XB_SQL_PLAN_MONITOR_LHR START WITH 1 INCREMENT BY 1 CACHE 1000;
  118. SELECT S_XB_SQL_PLAN_MONITOR_LHR.NEXTVAL FROM DUAL;
  119.  

  120. CREATE INDEX IND_SQL_MONITOR_ID ON XB_SQL_PLAN_MONITOR_LHR(SQL_MONITOR_ID) NOLOGGING;
  121. CREATE INDEX IND_SQL_PLAN_MONITOR_IN_DATE ON XB_SQL_PLAN_MONITOR_LHR(IN_DATE) LOCAL NOLOGGING;
  122. CREATE INDEX IND_SMONITOR_SQLIDSIDKEY ON XB_SQL_PLAN_MONITOR_LHR(SQL_ID,SID,SERIAL#,SPID,KEY) LOCAL NOLOGGING;





  123. --------監控正在執行的SQL語句
  124. DROP TABLE XB_SQL_MONITOR_PP_LHR;
  125. -- Create table
  126. CREATE TABLE XB_SQL_MONITOR_PP_LHR
  127. (
  128.   ID NUMBER NOT NULL,
  129.   INST_ID NUMBER,
  130.   SID NUMBER,
  131.   SERIAL# NUMBER,
  132.   SPID VARCHAR2(24),
  133.   OSUSER VARCHAR2(30),
  134.   USERNAME VARCHAR2(30),
  135.   SQL_TEXT VARCHAR2(4000),
  136.   SQL_FULLTEXT CLOB,
  137.   PLAN_OPERATION VARCHAR2(61),
  138.   STARTS NUMBER,
  139.   PLAN_PARTITION_START VARCHAR2(128),
  140.   PLAN_PARTITION_STOP VARCHAR2(128),
  141.   EXECUTIONS NUMBER,
  142.   SQL_ID VARCHAR2(13),
  143.   SQL_EXEC_START DATE,
  144.   LOGON_TIME DATE,
  145.   LAST_LOAD_TIME DATE,
  146.   LAST_ACTIVE_TIME DATE,
  147.   ELAPSED_TIME VARCHAR2(500),
  148.   ELAPSED_TIME1 NUMBER,
  149.   MONITOR_TYPES VARCHAR2(500),
  150.   MONITOR_TYPES1 NUMBER,
  151.   MONITOR_VALUE NUMBER,
  152.   TUNING_RESULT CLOB,
  153.   TUNING_TIME DATE,
  154.   SESSION_INFO VARCHAR2(4000),
  155.   SESSION_STATE VARCHAR2(30),
  156.   EVENT VARCHAR2(4000),
  157.   CPU_TIME NUMBER,
  158.   BUFFER_GETS NUMBER,
  159.   PHYSICAL_READ_BYTES NUMBER,
  160.   PHYSICAL_WRITE_BYTES NUMBER,
  161.   USER_IO_WAIT_TIME NUMBER,
  162.   BLOCKING_INSTANCE NUMBER,
  163.   BLOCKING_SESSION NUMBER,
  164.   LAST_CALL_ET NUMBER,
  165.   ASH_COUNTS NUMBER,
  166.   IN_DATE DATE
  167. ) NOLOGGING
  168. PARTITION BY RANGE (IN_DATE) INTERVAL(NUMTOYMINTERVAL(1,'MONTH'))
  169. (
  170.   PARTITION P201704 VALUES LESS THAN (TO_DATE('201705', 'YYYYMM'))
  171. );

  172. CREATE INDEX IND_SQL_MONITOR_PPID ON XB_SQL_MONITOR_PP_LHR(SQL_ID) LOCAL NOLOGGING;
  173. CREATE INDEX IND_SQL_MONITOR_PP_DATE ON XB_SQL_MONITOR_PP_LHR(IN_DATE) LOCAL NOLOGGING;

  174. DROP SEQUENCE S_XB_SQL_MONITOR_PP_LHR;
  175. CREATE SEQUENCE S_XB_SQL_MONITOR_PP_LHR START WITH 1 INCREMENT BY 1 CACHE 1000;



  176. DROP TABLE XB_SQL_PARAMETERS_LHR;
  177. CREATE TABLE XB_SQL_PARAMETERS_LHR
  178. ( ID NUMBER PRIMARY KEY,
  179.   CN_NAME VARCHAR2(100) NOT NULL,
  180.   PARAM_NAME VARCHAR2(50) NOT NULL,
  181.   PARAM_TYPE VARCHAR2(50) ,
  182.   PARAM_VALUE VARCHAR2(50) ,
  183.   PARAM_UNIT VARCHAR2(50) ,
  184.   COMMENTS VARCHAR2(500)
  185. ) NOLOGGING CACHE ;


  186. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  187. VALUES (1, 'SQL佔用UNDO表空間過大', 'V_UNDOSIZE', 'NUMBER', '52428800', 'BYTES', '單條SQL佔用的UNDO表空間大小');
  188. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  189. VALUES (2, 'SQL佔用TMP表空間過大', 'V_TMPSIZE', 'NUMBER', '15204352', 'BYTES', '單條SQL佔用的臨時表空間大小');
  190. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  191. VALUES (3, '執行計劃COST花費過大', 'V_PLAN_COST', 'NUMBER', '114', NULL, 'SQL執行計劃中的COST花費,參照值,無單位');
  192. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  193. VALUES (4, '執行計劃預估行數過大', 'V_PLAN_CARDINALITY', 'NUMBER', '1426', '行', 'SQL執行計劃中的預估行數');
  194. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  195. VALUES (5, 'SQL執行時間過大', 'V_ELAPSED_TIME', 'NUMBER', '29', '秒', 'SQL執行時間,單位為秒,1秒等於1000000微秒');
  196. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  197. VALUES (6, 'SQL執行次數過大', 'V_EXECUTIONS', 'NUMBER', '7616', '次', 'SQL執行次數');
  198. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  199. VALUES (7, 'SQL監控時間間隔', 'V_INTERVALTIME', 'NUMBER', '30', '秒', 'SQL監控時間間隔,最小值20秒,最大值120秒,預設30秒,推薦30秒');
  200. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  201. VALUES (8, '笛卡爾積SQL監控', 'V_MERGEJOIN', NULL, NULL, NULL, 'SQL形成笛卡爾積');
  202. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  203. VALUES (9, '分割槽表全分割槽掃描', 'V_PARTTABLESCAN', NULL, NULL, NULL, '分割槽表全分割槽掃描');
  204. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  205. VALUES (13, 'SQL並行個數過大', 'V_PARALLEL', 'NUMBER', 8, '', 'SQL開並行的最大並行個數');
  206. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  207. VALUES (14, '系統預估的剩餘執行時間過長', 'V_ESTIMATE_TIME', 'NUMBER', 900, '秒', '系統預估的剩餘執行時間過長');
  208. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  209. VALUES (15, '邏輯讀過大', 'V_LOGICAL_READS', 'NUMBER', 1510407, '', '邏輯讀過大');
  210. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  211. VALUES (16, '物理讀過大', 'V_DISK_READS', 'NUMBER', 1510407, '', '物理讀過大');
  212. INSERT INTO XB_SQL_PARAMETERS_LHR (ID, CN_NAME, PARAM_NAME, PARAM_TYPE, PARAM_VALUE, PARAM_UNIT, COMMENTS)
  213. VALUES (17, '等待事件異常', 'V_WAIT_EVENT', '', '', '', '等待事件異常的SQL語句');
  214. COMMIT;
  215.  


  216. --@@PKG_SQL_MONITOR_LHR.PCK
  217. --------------------------------------------------------------
  218. CREATE OR REPLACE PACKAGE PKG_SQL_MONITOR_LHR AUTHID CURRENT_USER AS

  219.   -----------------------------------------------------------------------------------
  220.   -- CREATED ON 2014-07-15 12:19:12 BY LHR
  221.   --CHANGED ON 2013-07-1512:19:12 BY LHR
  222.   -- FUNCTION: 該包記錄所有歷史SQL語句
  223.   ---主要採用了 V$SQL_MONITOR 和 V$SQL_PLAN_MONITOR 2個檢視
  224.   -----------------------------------------------------------------------------------

  225.   -----------------------------存過--------------------------------------
  226.   --已經執行完畢的SQL
  227.   PROCEDURE P_SQL_DONE_LHR;

  228.   -- EXECUTING POOR PERFORMANCE 正在執行的效能差的SQL
  229.   PROCEDURE P_SQL_EPP_LHR;
  230.   PROCEDURE P_SQL_EPP2_LHR;
  231.   PROCEDURE P_TUNING_SQL;
  232.   PROCEDURE P_GET_PPSQL_PARAMETER;

  233.   ------------------------------函式-------------------------------------

  234.   FUNCTION F_GET_RIGHT_SIZE_LHR(P_SIZE IN VARCHAR2) RETURN VARCHAR2;
  235.   FUNCTION F_GET_TOTAL_TIME_LHR(P_TOTAL_SECONDS NUMBER,
  236.                                 P_FLAG VARCHAR2 DEFAULT 'S')
  237.     RETURN VARCHAR2 DETERMINISTIC;

  238.   ---得到當前SQL語句操作的物件
  239.   FUNCTION F_GET_SQL_OBJECT_LHR(P_SQL VARCHAR2,
  240.                                 P_COMMAND VARCHAR2 DEFAULT 'INTO',
  241.                                 PFLAG NUMBER DEFAULT 1) RETURN VARCHAR2;

  242.   -----得到當前SQL語句的命令型別
  243.   FUNCTION F_GET_SQL_COMMAND_LHR(P_SQL VARCHAR2) RETURN VARCHAR2;

  244. END PKG_SQL_MONITOR_LHR;
  245. /
  246. CREATE OR REPLACE PACKAGE BODY PKG_SQL_MONITOR_LHR AS

  247.   PROCEDURE P_SQL_DONE_LHR AS

  248.     V_DATE DATE := SYSDATE;
  249.     V_COMMAND VARCHAR2(50);
  250.     V_SQL VARCHAR2(32767);
  251.     V_OSUSER VARCHAR2(255);
  252.     V_SPID NUMBER;

  253.   BEGIN

  254.     FOR CUR IN (SELECT S_XB_SQL_MONITOR_LHR.NEXTVAL ID,
  255.                        A.SID,
  256.                        A.SESSION_SERIAL# SERIAL#,
  257.                        A.SQL_ID,
  258.                        NVL(A.SQL_TEXT, NS.SQL_TEXT) SQL_TEXT,
  259.                        DECODE(A.IS_FULL_SQLTEXT,
  260.                               'N',
  261.                               NS.SQL_FULLTEXT,
  262.                               A.SQL_TEXT) SQL_FULLTEXT,
  263.                        A.SQL_EXEC_START,
  264.                        A.ELAPSED_TIME,
  265.                        A.STATUS,
  266.                        A.USERNAME,
  267.                        CASE
  268.                          WHEN A.ERROR_MESSAGE IS NOT NULL THEN
  269.                           (A.MODULE || '--' || A.ACTION || '--' || A.PROGRAM || '--' ||
  270.                           A.PROCESS_NAME || '--' || A.CLIENT_IDENTIFIER || '--' ||
  271.                           A.CLIENT_INFO || '--' || A.SERVICE_NAME) || '----【' ||
  272.                           A.ERROR_MESSAGE || '】'
  273.                          ELSE
  274.                           (A.MODULE || '--' || A.ACTION || '--' || A.PROGRAM || '--' ||
  275.                           A.PROCESS_NAME || '--' || A.CLIENT_IDENTIFIER || '--' ||
  276.                           A.CLIENT_INFO || '--' || A.SERVICE_NAME)
  277.                        END AS SESSION_TYPES,
  278.                        CASE
  279.                          WHEN A.PX_QCSID IS NOT NULL THEN
  280.                           A.PX_QCSID
  281.                          WHEN A.PX_QCSID IS NULL AND
  282.                               A.PX_SERVERS_ALLOCATED IS NOT NULL THEN
  283.                           A.SID
  284.                        END AS PX_QCSID,
  285.                        (SELECT NC.COMMAND_NAME
  286.                           FROM V$SQLCOMMAND NC
  287.                          WHERE NS.COMMAND_TYPE = NC.COMMAND_TYPE) COMMAND_TYPE,
  288.                        A.SQL_EXEC_ID,
  289.                        F_GET_TOTAL_TIME_LHR(A.ELAPSED_TIME / 1000000) ELAPSED_TIME2,
  290.                        A.PROCESS_NAME,
  291.                        A.MODULE,
  292.                        A.CPU_TIME,
  293.                        A.FETCHES,
  294.                        A.BUFFER_GETS,
  295.                        A.DISK_READS,
  296.                        A.DIRECT_WRITES,
  297.                        A.BINDS_XML,
  298.                        A.USER_IO_WAIT_TIME,
  299.                        A.CONCURRENCY_WAIT_TIME,
  300.                        A.PHYSICAL_READ_BYTES,
  301.                        A.PHYSICAL_WRITE_BYTES,
  302.                        A.KEY,
  303.                        C.PLAN_OBJECT_OWNER,
  304.                        C.PLAN_OBJECT_NAME,
  305.                        A.INST_ID,
  306.                        NS.EXECUTIONS EXECUTIONS,
  307.                        NS.LAST_LOAD_TIME,
  308.                        NS.LAST_ACTIVE_TIME
  309.                   FROM (SELECT *
  310.                           FROM GV$SQL_MONITOR NA
  311.                          WHERE NA.STATUS LIKE 'DONE%'
  312.                            AND NA.LAST_REFRESH_TIME >= SYSDATE - 1
  313.                            AND NOT EXISTS
  314.                          (SELECT 1
  315.                                   FROM XB_SQL_MONITOR_LHR B
  316.                                  WHERE NA.SID = B.SID
  317.                                    AND NA.SESSION_SERIAL# = B.SERIAL#
  318.                                    AND NA.SQL_ID = B.SQL_ID
  319.                                    AND NA.SQL_EXEC_ID = B.SQL_EXEC_ID
  320.                                    AND B.IN_DATE BETWEEN SYSDATE - 1 AND
  321.                                        SYSDATE
  322.                                    AND B.INST_ID = NA.INST_ID)) A
  323.                   LEFT OUTER JOIN GV$SQL_PLAN_MONITOR C
  324.                     ON (A.KEY = C.KEY AND A.SQL_ID = C.SQL_ID AND
  325.                        C.PLAN_LINE_ID = 1 AND A.SQL_EXEC_ID = C.SQL_EXEC_ID AND
  326.                        C.PLAN_OBJECT_OWNER IS NOT NULL AND
  327.                        C.PLAN_OBJECT_NAME IS NOT NULL AND
  328.                        C.PLAN_OPERATION IN ('UPDATE', 'DELETE', 'MERGE') AND
  329.                        A.INST_ID = C.INST_ID)
  330.                   LEFT OUTER JOIN GV$SQLAREA NS
  331.                     ON (NS.SQL_ID = A.SQL_ID AND NS.INST_ID = A.INST_ID)) LOOP

  332.       V_SQL := SUBSTR(NVL(DBMS_LOB.SUBSTR(CUR.SQL_FULLTEXT, 3900, 1),
  333.                           CUR.SQL_TEXT),
  334.                       1,
  335.                       3900);

  336.       ------ SQL語句的命令型別
  337.       IF CUR.COMMAND_TYPE IS NULL AND V_SQL IS NOT NULL THEN
  338.         BEGIN
  339.           CUR.COMMAND_TYPE := F_GET_SQL_COMMAND_LHR(V_SQL);
  340.         EXCEPTION
  341.           WHEN OTHERS THEN
  342.             NULL;
  343.         END;

  344.       END IF;

  345.       ---- 找出正在操作的物件
  346.       IF (CUR.COMMAND_TYPE IN ('INSERT',
  347.                                'MERGE INTO',
  348.                                'CREATE TABLE',
  349.                                'CREATE INDEX',
  350.                                'ALTER TABLE',
  351.                                'ALTER INDEX')) AND
  352.          (CUR.PLAN_OBJECT_OWNER IS NULL AND CUR.PLAN_OBJECT_NAME IS NULL) AND
  353.          V_SQL IS NOT NULL THEN

  354.         V_COMMAND := CASE
  355.                        WHEN CUR.COMMAND_TYPE IN ('INSERT', 'MERGE INTO') THEN
  356.                         'INTO'
  357.                        WHEN CUR.COMMAND_TYPE IN ('CREATE TABLE', 'ALTER TABLE') THEN
  358.                         'TABLE'
  359.                        WHEN CUR.COMMAND_TYPE IN ('CREATE INDEX', 'ALTER INDEX') THEN
  360.                         'INDEX'
  361.                      END;

  362.         BEGIN

  363.           CUR.PLAN_OBJECT_OWNER := NVL(F_GET_SQL_OBJECT_LHR(V_SQL,
  364.                                                             V_COMMAND,
  365.                                                             2),
  366.                                        CUR.USERNAME);
  367.           CUR.PLAN_OBJECT_NAME := F_GET_SQL_OBJECT_LHR(V_SQL, V_COMMAND);
  368.         EXCEPTION
  369.           WHEN OTHERS THEN
  370.             NULL;
  371.         END;

  372.       END IF;

  373.       INSERT INTO XB_SQL_MONITOR_LHR
  374.         (ID,
  375.          INST_ID,
  376.          SID,
  377.          SERIAL#,
  378.          SQL_ID,
  379.          SQL_TEXT,
  380.          SQL_FULLTEXT,
  381.          SQL_EXEC_START,
  382.          ELAPSED_TIME,
  383.          STATUS,
  384.          USERNAME,
  385.          SESSION_TYPES,
  386.          EXECUTIONS,
  387.          IN_DATE,
  388.          PX_QCSID,
  389.          COMMAND_TYPE,
  390.          SQL_EXEC_ID,
  391.          ELAPSED_TIME2,
  392.          OS_USER,
  393.          CPU_TIME,
  394.          FETCHES,
  395.          BUFFER_GETS,
  396.          DISK_READS,
  397.          DIRECT_WRITES,
  398.          BINDS_XML,
  399.          USER_IO_WAIT_TIME,
  400.          CONCURRENCY_WAIT_TIME,
  401.          PHYSICAL_READ_BYTES,
  402.          PHYSICAL_WRITE_BYTES,
  403.          KEY,
  404.          PLAN_OBJECT_OWNER,
  405.          PLAN_OBJECT_NAME,
  406.          SPID,
  407.          LAST_LOAD_TIME,
  408.          LAST_ACTIVE_TIME)
  409.       VALUES
  410.         (CUR.ID,
  411.          CUR.INST_ID,
  412.          CUR.SID,
  413.          CUR.SERIAL#,
  414.          CUR.SQL_ID,
  415.          CUR.SQL_TEXT,
  416.          CUR.SQL_FULLTEXT,
  417.          CUR.SQL_EXEC_START,
  418.          CUR.ELAPSED_TIME,
  419.          CUR.STATUS,
  420.          CUR.USERNAME,
  421.          CUR.SESSION_TYPES,
  422.          CUR.EXECUTIONS,
  423.          V_DATE,
  424.          CUR.PX_QCSID,
  425.          CUR.COMMAND_TYPE,
  426.          CUR.SQL_EXEC_ID,
  427.          CUR.ELAPSED_TIME2,
  428.          V_OSUSER,
  429.          CUR.CPU_TIME,
  430.          CUR.FETCHES,
  431.          CUR.BUFFER_GETS,
  432.          CUR.DISK_READS,
  433.          CUR.DIRECT_WRITES,
  434.          CUR.BINDS_XML,
  435.          CUR.USER_IO_WAIT_TIME,
  436.          CUR.CONCURRENCY_WAIT_TIME,
  437.          CUR.PHYSICAL_READ_BYTES,
  438.          CUR.PHYSICAL_WRITE_BYTES,
  439.          CUR.KEY,
  440.          CUR.PLAN_OBJECT_OWNER,
  441.          CUR.PLAN_OBJECT_NAME,
  442.          V_SPID,
  443.          CUR.LAST_LOAD_TIME,
  444.          CUR.LAST_ACTIVE_TIME);

  445.       INSERT INTO XB_SQL_PLAN_MONITOR_LHR
  446.         (ID,
  447.          INST_ID,
  448.          SQL_MONITOR_ID,
  449.          KEY,
  450.          STATUS,
  451.          SID,
  452.          SQL_ID,
  453.          SQL_EXEC_START,
  454.          SQL_EXEC_ID,
  455.          SQL_PLAN_HASH_VALUE,
  456.          SQL_CHILD_ADDRESS,
  457.          PLAN_PARENT_ID,
  458.          PLAN_LINE_ID,
  459.          PLAN_OPERATION,
  460.          PLAN_OPTIONS,
  461.          PLAN_OBJECT_OWNER,
  462.          PLAN_OBJECT_NAME,
  463.          PLAN_OBJECT_TYPE,
  464.          PLAN_DEPTH,
  465.          PLAN_POSITION,
  466.          PLAN_COST,
  467.          PLAN_CARDINALITY,
  468.          PLAN_BYTES,
  469.          PLAN_TIME,
  470.          PLAN_PARTITION_START,
  471.          PLAN_PARTITION_STOP,
  472.          PLAN_CPU_COST,
  473.          PLAN_IO_COST,
  474.          PLAN_TEMP_SPACE,
  475.          STARTS,
  476.          OUTPUT_ROWS,
  477.          IO_INTERCONNECT_BYTES,
  478.          PHYSICAL_READ_REQUESTS,
  479.          PHYSICAL_READ_BYTES,
  480.          PHYSICAL_WRITE_REQUESTS,
  481.          PHYSICAL_WRITE_BYTES,
  482.          IN_DATE,
  483.          CHILD_NUMBER,
  484.          OBJECT#,
  485.          OBJECT_ALIAS,
  486.          OPTIMIZER,
  487.          SEARCH_COLUMNS,
  488.          FILTER_PREDICATES,
  489.          ACCESS_PREDICATES,
  490.          PROJECTION,
  491.          OTHER_XML,
  492.          SERIAL#,
  493.          SPID)

  494.         SELECT S_XB_SQL_PLAN_MONITOR_LHR.NEXTVAL ID,
  495.                CUR.INST_ID,
  496.                (SELECT NB.ID
  497.                   FROM XB_SQL_MONITOR_LHR NB
  498.                  WHERE NB.KEY = A.KEY
  499.                    AND NB.SID = CUR.SID
  500.                    AND NB.SQL_EXEC_ID = NB.SQL_EXEC_ID
  501.                    AND NB.IN_DATE = V_DATE
  502.                    AND NB.SQL_ID = CUR.SQL_ID
  503.                    AND NB.INST_ID = CUR.INST_ID) SQL_MONITOR_ID,
  504.                KEY,
  505.                STATUS,
  506.                SID,
  507.                A.SQL_ID,
  508.                SQL_EXEC_START,
  509.                SQL_EXEC_ID,
  510.                SQL_PLAN_HASH_VALUE,
  511.                SQL_CHILD_ADDRESS,
  512.                PLAN_PARENT_ID,
  513.                PLAN_LINE_ID,
  514.                PLAN_OPERATION,
  515.                PLAN_OPTIONS,
  516.                PLAN_OBJECT_OWNER,
  517.                PLAN_OBJECT_NAME,
  518.                PLAN_OBJECT_TYPE,
  519.                PLAN_DEPTH,
  520.                PLAN_POSITION,
  521.                NVL(A.PLAN_COST, B.COST) COST,
  522.                PLAN_CARDINALITY,
  523.                PLAN_BYTES,
  524.                PLAN_TIME,
  525.                PLAN_PARTITION_START,
  526.                PLAN_PARTITION_STOP,
  527.                PLAN_CPU_COST,
  528.                PLAN_IO_COST,
  529.                PLAN_TEMP_SPACE,
  530.                STARTS,
  531.                OUTPUT_ROWS,
  532.                IO_INTERCONNECT_BYTES,
  533.                PHYSICAL_READ_REQUESTS,
  534.                PHYSICAL_READ_BYTES,
  535.                PHYSICAL_WRITE_REQUESTS,
  536.                PHYSICAL_WRITE_BYTES,
  537.                V_DATE IN_DATE,
  538.                B.CHILD_NUMBER,
  539.                B.OBJECT#,
  540.                B.OBJECT_ALIAS,
  541.                B.OPTIMIZER,
  542.                B.SEARCH_COLUMNS,
  543.                B.FILTER_PREDICATES,
  544.                B.ACCESS_PREDICATES,
  545.                B.PROJECTION,
  546.                B.OTHER_XML,
  547.                CUR.SERIAL#,
  548.                V_SPID
  549.           FROM GV$SQL_PLAN_MONITOR A
  550.           LEFT OUTER JOIN GV$SQL_PLAN B
  551.             ON (A.SQL_ID = B.SQL_ID AND
  552.                A.SQL_PLAN_HASH_VALUE = B.PLAN_HASH_VALUE AND
  553.                A.SQL_CHILD_ADDRESS = B.CHILD_ADDRESS AND
  554.                A.PLAN_LINE_ID = B.ID AND B.SQL_ID = CUR.SQL_ID AND
  555.                A.INST_ID = B.INST_ID)
  556.          WHERE A.STATUS LIKE 'DONE%'
  557.            AND A.SQL_ID NOT IN
  558.                (SELECT BB.SQL_ID
  559.                   FROM XB_SQL_PLAN_MONITOR_LHR BB
  560.                  WHERE BB.SID = CUR.SID
  561.                    AND BB.KEY = CUR.KEY
  562.                    AND BB.SQL_ID = CUR.SQL_ID
  563.                    AND BB.SQL_EXEC_ID = CUR.SQL_EXEC_ID
  564.                    AND BB.IN_DATE BETWEEN SYSDATE - 1 AND SYSDATE
  565.                    AND BB.INST_ID = A.INST_ID)
  566.            AND A.SQL_ID = CUR.SQL_ID
  567.            AND A.KEY = CUR.KEY
  568.            AND A.SID = CUR.SID
  569.            AND A.SQL_EXEC_ID = CUR.SQL_EXEC_ID
  570.            AND A.INST_ID = CUR.INST_ID;

  571.     END LOOP;

  572.     COMMIT;

  573.   EXCEPTION
  574.     WHEN OTHERS THEN
  575.       NULL;
  576.   END P_SQL_DONE_LHR;

  577.   ----------------------------------------------------------------------------------------------

  578.   PROCEDURE P_SQL_EPP_LHR AS

  579.     -----------------------------------------------------------------------------------
  580.     -- Created on 2015/11/21 16:49:28 by lhr
  581.     --Changed on 2017/04/03 16:49:28 by lhr
  582.     -- function: 監控正在執行的SQL
  583.     -----------------------------------------------------------------------------------

  584.     V_TMPSIZE NUMBER := 5368709120; --BYTES
  585.     V_UNDOSIZE NUMBER := 5368709120; --BYTES
  586.     V_PLAN_COST NUMBER := 3107523095; --COST 花費
  587.     V_PLAN_CARDINALITY NUMBER := 30748908521460; --預估行數
  588.     V_ELAPSED_TIME NUMBER := 10000000; ---執行時間,單位:微秒,例執行時間設定5小時,則: 5H*60*60*1000000 ,1秒等於1000000微秒
  589.     V_EXECUTIONS NUMBER := 1000; --執行次數
  590.     V_PARALLEL NUMBER := 4; --SQL開並行的個數
  591.     V_ESTIMATE_TIME NUMBER := 900; --系統預估的剩餘執行時間過長

  592.   BEGIN

  593.     SELECT SUM(CASE T.PARAM_NAME
  594.                  WHEN 'V_UNDOSIZE' THEN
  595.                   T.PARAM_VALUE
  596.                END) V_UNDOSIZE,
  597.            SUM(CASE T.PARAM_NAME
  598.                  WHEN 'V_TMPSIZE' THEN
  599.                   T.PARAM_VALUE
  600.                END) V_TMPSIZE,
  601.            SUM(CASE T.PARAM_NAME
  602.                  WHEN 'V_PLAN_COST' THEN
  603.                   T.PARAM_VALUE
  604.                END) V_PLAN_COST,
  605.            SUM(CASE T.PARAM_NAME
  606.                  WHEN 'V_PLAN_CARDINALITY' THEN
  607.                   T.PARAM_VALUE
  608.                END) V_PLAN_CARDINALITY,
  609.            SUM(CASE T.PARAM_NAME
  610.                  WHEN 'V_ELAPSED_TIME' THEN
  611.                   T.PARAM_VALUE
  612.                END) V_ELAPSED_TIME,
  613.            SUM(CASE T.PARAM_NAME
  614.                  WHEN 'V_EXECUTIONS' THEN
  615.                   T.PARAM_VALUE
  616.                END) V_EXECUTIONS,
  617.            SUM(CASE T.PARAM_NAME
  618.                  WHEN 'V_PARALLEL' THEN
  619.                   T.PARAM_VALUE
  620.                END) V_PARALLEL,
  621.            SUM(CASE T.PARAM_NAME
  622.                  WHEN 'V_ESTIMATE_TIME' THEN
  623.                   T.PARAM_VALUE
  624.                END) V_ESTIMATE_TIME
  625.       INTO V_UNDOSIZE,
  626.            V_TMPSIZE,
  627.            V_PLAN_COST,
  628.            V_PLAN_CARDINALITY,
  629.            V_ELAPSED_TIME,
  630.            V_EXECUTIONS,
  631.            V_PARALLEL,
  632.            V_ESTIMATE_TIME
  633.       FROM XB_SQL_PARAMETERS_LHR T
  634.      WHERE T.PARAM_NAME IN ('V_UNDOSIZE',
  635.                             'V_TMPSIZE',
  636.                             'V_PLAN_COST',
  637.                             'V_PLAN_CARDINALITY',
  638.                             'V_ELAPSED_TIME',
  639.                             'V_EXECUTIONS',
  640.                             'V_PARALLEL',
  641.                             'V_ESTIMATE_TIME');

  642.     --------------------------------------------- 垃圾SQL監控
  643.     INSERT INTO XB_SQL_MONITOR_PP_LHR
  644.       (ID,
  645.        INST_ID,
  646.        SID,
  647.        SERIAL#,
  648.        SPID,
  649.        OSUSER,
  650.        USERNAME,
  651.        SQL_TEXT,
  652.        SQL_FULLTEXT,
  653.        PLAN_OPERATION,
  654.        STARTS,
  655.        PLAN_PARTITION_START,
  656.        PLAN_PARTITION_STOP,
  657.        EXECUTIONS,
  658.        SQL_ID,
  659.        SQL_EXEC_START,
  660.        LOGON_TIME,
  661.        LAST_LOAD_TIME,
  662.        LAST_ACTIVE_TIME,
  663.        ELAPSED_TIME,
  664.        ELAPSED_TIME1,
  665.        MONITOR_TYPES,
  666.        MONITOR_TYPES1,
  667.        MONITOR_VALUE,
  668.        SESSION_INFO,
  669.        SESSION_STATE,
  670.        EVENT,
  671.        CPU_TIME,
  672.        BUFFER_GETS,
  673.        PHYSICAL_READ_BYTES,
  674.        PHYSICAL_WRITE_BYTES,
  675.        USER_IO_WAIT_TIME,
  676.        BLOCKING_INSTANCE,
  677.        BLOCKING_SESSION,
  678.        LAST_CALL_ET,
  679.        ASH_COUNTS,
  680.        IN_DATE)
  681.       SELECT S_XB_SQL_MONITOR_PP_LHR.NEXTVAL,
  682.              INST_ID,
  683.              SID,
  684.              SERIAL#,
  685.              SPID,
  686.              OSUSER,
  687.              USERNAME,
  688.              SQL_TEXT,
  689.              (SELECT NB.SQL_FULLTEXT
  690.                 FROM GV$SQLAREA NB
  691.                WHERE NB.INST_ID = T.INST_ID
  692.                  AND T.SQL_ID = NB.SQL_ID) SQL_FULLTEXT,
  693.              PLAN_OPERATION,
  694.              STARTS,
  695.              PLAN_PARTITION_START,
  696.              PLAN_PARTITION_STOP,
  697.              EXECUTIONS,
  698.              SQL_ID,
  699.              SQL_EXEC_START,
  700.              LOGON_TIME,
  701.              '' LAST_LOAD_TIME,
  702.              '' LAST_ACTIVE_TIME,
  703.              F_GET_TOTAL_TIME_LHR(ELAPSED_TIME_S),
  704.              ELAPSED_TIME_S,
  705.              MONITOR_TYPES,
  706.              '' MONITOR_TYPES1,
  707.              '' MONITOR_VALUE,
  708.              SESSION_INFO,
  709.              SESSION_STATE,
  710.              EVENT,
  711.              CPU_TIME,
  712.              BUFFER_GETS,
  713.              PHYSICAL_READ_BYTES,
  714.              PHYSICAL_WRITE_BYTES,
  715.              USER_IO_WAIT_TIME,
  716.              BLOCKING_INSTANCE,
  717.              BLOCKING_SESSION,
  718.              LAST_CALL_ET,
  719.              ASH_COUNTS,
  720.              SYSDATE IN_DATE
  721.         FROM (
  722.              --記錄所有正在執行中的效能差的SQL語句
  723.              WITH TMPS AS (SELECT WB.INST_ID INST_ID,
  724.                                   WB.SID SID,
  725.                                   WB.SERIAL#,
  726.                                   WB.SPID,
  727.                                   WB.OSUSER,
  728.                                   WB.USERNAME,
  729.                                   WA.PLAN_DEPTH,
  730.                                   WA.PLAN_OPERATION PLAN_OPERATION,
  731.                                   WA.PLAN_OPTIONS,
  732.                                   WA.PLAN_PARTITION_START,
  733.                                   WA.PLAN_PARTITION_STOP,
  734.                                   WA.STARTS,
  735.                                   WA.PLAN_COST,
  736.                                   WA.PLAN_CARDINALITY,
  737.                                   NVL(WB.SQL_ID, WA.SQL_ID) SQL_ID,
  738.                                   WB.SQL_EXEC_START,
  739.                                   WA.PX_SERVERS_REQUESTED,
  740.                                   WA.PX_SERVERS_ALLOCATED,
  741.                                   WA.PX_MAXDOP,
  742.                                   WA.ELAPSED_TIME_S ELAPSED_TIME_S,
  743.                                   WA.CPU_TIME CPU_TIME,
  744.                                   WA.BUFFER_GETS,
  745.                                   WA.PHYSICAL_READ_BYTES,
  746.                                   WA.PHYSICAL_WRITE_BYTES,
  747.                                   WA.USER_IO_WAIT_TIME USER_IO_WAIT_TIME,
  748.                                   NVL((SELECT NS.SQL_TEXT
  749.                                         FROM GV$SQLAREA NS
  750.                                        WHERE NS.SQL_ID = WB.SQL_ID
  751.                                          AND NS.INST_ID = WB.INST_ID),
  752.                                       WA.SQL_TEXT) SQL_TEXT,
  753.                                   WB.LOGON_TIME,
  754.                                   WB.SQL_EXEC_ID,
  755.                                   WB.EVENT,
  756.                                   WB.BLOCKING_INSTANCE BLOCKING_INSTANCE,
  757.                                   WB.BLOCKING_SESSION BLOCKING_SESSION,
  758.                                   WB.BLOCKING_SESSION_SERIAL# BLOCKING_SESSION_SERIAL#,
  759.                                   WB.TADDR,
  760.                                   WB.SADDR,
  761.                                   WB.LAST_CALL_ET,
  762.                                   (WB.SESSION_INFO || '--' || WB.SESSION_TYPE || '--' ||
  763.                                   WB.MACHINE) SESSION_INFO,
  764.                                   (SELECT NS.EXECUTIONS
  765.                                      FROM GV$SQLAREA NS
  766.                                     WHERE NS.SQL_ID = WB.SQL_ID
  767.                                       AND NS.INST_ID = WB.INST_ID) EXECUTIONS,
  768.                                   'SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(''' ||
  769.                                   WB.SQL_ID || ''',' || WB.SQL_CHILD_NUMBER ||
  770.                                   ',''advanced''));' SQL_PLAN,
  771.                                   WB.ASH_COUNTS,
  772.                                   WB.SESSION_STATE
  773.                              FROM (SELECT A.INST_ID,
  774.                                           A.SID,
  775.                                           A.PLAN_DEPTH,
  776.                                           A.PLAN_OPERATION PLAN_OPERATION,
  777.                                           A.PLAN_OPTIONS,
  778.                                           A.PLAN_PARTITION_START,
  779.                                           A.PLAN_PARTITION_STOP,
  780.                                           A.STARTS,
  781.                                           MAX(A.PLAN_COST) OVER(PARTITION BY A.INST_ID, A.SID, A.KEY, A.SQL_EXEC_ID, A.SQL_ID) AS PLAN_COST,
  782.                                           MAX(A.PLAN_CARDINALITY) OVER(PARTITION BY A.INST_ID, A.SID, A.KEY, A.SQL_EXEC_ID, A.SQL_ID) AS PLAN_CARDINALITY,
  783.                                           A.SQL_ID,
  784.                                           A.SQL_EXEC_START,
  785.                                           B.PX_SERVERS_REQUESTED,
  786.                                           B.PX_SERVERS_ALLOCATED,
  787.                                           B.PX_MAXDOP,
  788.                                           (B.ELAPSED_TIME / 1000000) ELAPSED_TIME_S,
  789.                                           (B.CPU_TIME / 1000000) CPU_TIME,
  790.                                           B.BUFFER_GETS,
  791.                                           B.PHYSICAL_READ_BYTES,
  792.                                           B.PHYSICAL_WRITE_BYTES,
  793.                                           (B.USER_IO_WAIT_TIME / 1000000) USER_IO_WAIT_TIME,
  794.                                           B.SQL_TEXT SQL_TEXT,
  795.                                           (B.MODULE || '--' || B.ACTION || '--' ||
  796.                                           B.PROGRAM || '--' || B.PROCESS_NAME || '--' ||
  797.                                           B.CLIENT_IDENTIFIER || '--' ||
  798.                                           B.CLIENT_INFO || '--' ||
  799.                                           B.SERVICE_NAME) SESSION_INFO,
  800.                                           A.SQL_EXEC_ID
  801.                                      FROM GV$SQL_PLAN_MONITOR A,
  802.                                           GV$SQL_MONITOR B
  803.                                     WHERE A.SID = B.SID
  804.                                       AND A.KEY = B.KEY
  805.                                       AND A.INST_ID = B.INST_ID
  806.                                       AND A.SQL_EXEC_ID = B.SQL_EXEC_ID
  807.                                       AND A.STATUS IN
  808.                                           ('EXECUTING', 'DONE(ERROR)')
  809.                                       AND B.STATUS IN
  810.                                           ('EXECUTING', 'DONE(ERROR)')
  811.                                       AND B.PROCESS_NAME NOT LIKE 'p%') WA
  812.                             RIGHT OUTER JOIN (SELECT ASH.INST_ID,
  813.                                                     ASH.SESSION_ID SID,
  814.                                                     ASH.SESSION_SERIAL# SERIAL#,
  815.                                                     (SELECT PR.SPID
  816.                                                        FROM GV$PROCESS PR
  817.                                                       WHERE GVS.PADDR =
  818.                                                             PR.ADDR
  819.                                                         AND PR.INST_ID =
  820.                                                             ASH.INST_ID) SPID,
  821.                                                     ASH.SESSION_TYPE,
  822.                                                     ASH.USER_ID,
  823.                                                     ASH.SQL_ID,
  824.                                                     ASH.SQL_CHILD_NUMBER,
  825.                                                     ASH.SQL_OPNAME,
  826.                                                     ASH.SQL_EXEC_ID,
  827.                                                     NVL(ASH.EVENT, GVS.EVENT) EVENT,
  828.                                                     ASH.SESSION_STATE,
  829.                                                     ASH.BLOCKING_SESSION,
  830.                                                     ASH.BLOCKING_SESSION_SERIAL#,
  831.                                                     ASH.BLOCKING_INST_ID BLOCKING_INSTANCE,
  832.                                                     ASH.CLIENT_ID,
  833.                                                     ASH.MACHINE,
  834.                                                     GVS.LAST_CALL_ET,
  835.                                                     GVS.TADDR,
  836.                                                     GVS.SADDR,
  837.                                                     GVS.LOGON_TIME,
  838.                                                     GVS.USERNAME,
  839.                                                     GVS.OSUSER,
  840.                                                     GVS.SQL_EXEC_START,
  841.                                                     (GVS.MODULE || '--' ||
  842.                                                     GVS.ACTION || '--' ||
  843.                                                     GVS.PROGRAM || '--' ||
  844.                                                     GVS.PROCESS || '--' ||
  845.                                                     GVS.CLIENT_IDENTIFIER || '--' ||
  846.                                                     GVS.CLIENT_INFO || '--' ||
  847.                                                     GVS.SERVICE_NAME) SESSION_INFO,
  848.                                                     COUNT(*) ASH_COUNTS
  849.                                                FROM GV$ACTIVE_SESSION_HISTORY ASH,
  850.                                                     GV$SESSION GVS
  851.                                               WHERE ASH.INST_ID = GVS.INST_ID
  852.                                                 AND GVS.SQL_ID = ASH.SQL_ID
  853.                                                 AND GVS.SQL_EXEC_ID =
  854.                                                     ASH.SQL_EXEC_ID
  855.                                                 AND ASH.SESSION_ID = GVS.SID
  856.                                                 AND ASH.SESSION_SERIAL# =
  857.                                                     GVS.SERIAL#
  858.                                                 AND GVS.STATUS = 'ACTIVE'
  859.                                                 AND ASH.SQL_ID IS NOT NULL
  860.                                               GROUP BY ASH.INST_ID,
  861.                                                        ASH.SESSION_ID,
  862.                                                        ASH.SESSION_SERIAL#,
  863.                                                        ASH.SESSION_TYPE,
  864.                                                        ASH.USER_ID,
  865.                                                        ASH.SQL_ID,
  866.                                                        ASH.SQL_CHILD_NUMBER,
  867.                                                        ASH.SQL_OPNAME,
  868.                                                        ASH.SQL_EXEC_ID,
  869.                                                        NVL(ASH.EVENT,
  870.                                                            GVS.EVENT),
  871.                                                        ASH.SESSION_STATE,
  872.                                                        ASH.BLOCKING_SESSION,
  873.                                                        ASH.BLOCKING_SESSION_SERIAL#,
  874.                                                        ASH.BLOCKING_INST_ID,
  875.                                                        ASH.CLIENT_ID,
  876.                                                        ASH.MACHINE,
  877.                                                        GVS.LAST_CALL_ET,
  878.                                                        GVS.TADDR,
  879.                                                        GVS.SADDR,
  880.                                                        GVS.LOGON_TIME,
  881.                                                        GVS.USERNAME,
  882.                                                        GVS.OSUSER,
  883.                                                        GVS.PADDR,
  884.                                                        (GVS.MODULE || '--' ||
  885.                                                        GVS.ACTION || '--' ||
  886.                                                        GVS.PROGRAM || '--' ||
  887.                                                        GVS.PROCESS || '--' ||
  888.                                                        GVS.CLIENT_IDENTIFIER || '--' ||
  889.                                                        GVS.CLIENT_INFO || '--' ||
  890.                                                        GVS.SERVICE_NAME),
  891.                                                        GVS.SQL_EXEC_START
  892.                                              HAVING COUNT(*) > 6) WB
  893.                                ON (WB.SID = WA.SID AND
  894.                                   WB.INST_ID = WA.INST_ID AND
  895.                                   WB.SQL_ID = WA.SQL_ID AND
  896.                                   WB.SQL_EXEC_ID = WA.SQL_EXEC_ID))
  897.              ------------------------------------------ 笛卡爾積
  898.                SELECT DISTINCT T.INST_ID,
  899.                                T.SID,
  900.                                T.SERIAL#,
  901.                                T.SPID,
  902.                                T.OSUSER,
  903.                                T.USERNAME,
  904.                                T.EVENT,
  905.                                T.SESSION_STATE,
  906.                                T.SQL_TEXT,
  907.                                T.EXECUTIONS,
  908.                                T.ELAPSED_TIME_S,
  909.                                T.CPU_TIME,
  910.                                T.USER_IO_WAIT_TIME,
  911.                                T.BUFFER_GETS,
  912.                                T.PLAN_OPERATION,
  913.                                T.STARTS,
  914.                                T.PLAN_PARTITION_START,
  915.                                T.PLAN_PARTITION_STOP,
  916.                                T.PHYSICAL_READ_BYTES,
  917.                                T.PHYSICAL_WRITE_BYTES,
  918.                                T.BLOCKING_INSTANCE,
  919.                                T.BLOCKING_SESSION,
  920.                                T.BLOCKING_SESSION_SERIAL#,
  921.                                T.LAST_CALL_ET,
  922.                                T.SQL_ID,
  923.                                T.SQL_EXEC_START,
  924.                                T.SQL_PLAN,
  925.                                T.LOGON_TIME,
  926.                                T.ASH_COUNTS,
  927.                                T.SESSION_INFO,
  928.                                '笛卡爾積【' || COUNT(*) OVER(PARTITION BY T.INST_ID, T.SID, T.SERIAL#, T.SQL_ID) || '】個' MONITOR_TYPES
  929.                  FROM TMPS T
  930.                 WHERE T.PLAN_OPERATION = 'MERGE JOIN'
  931.                   AND T.PLAN_OPTIONS = 'CARTESIAN'
  932.                   AND T.USERNAME NOT IN ('SYS')

  933.                UNION ALL

  934.                ------------------------------------------ SQL執行時間過大
  935.                SELECT T.INST_ID,
  936.                       T.SID,
  937.                       T.SERIAL#,
  938.                       T.SPID,
  939.                       T.OSUSER,
  940.                       T.USERNAME,
  941.                       T.EVENT,
  942.                       T.SESSION_STATE,
  943.                       T.SQL_TEXT,
  944.                       T.EXECUTIONS,
  945.                       T.ELAPSED_TIME_S,
  946.                       T.CPU_TIME,
  947.                       T.USER_IO_WAIT_TIME,
  948.                       T.BUFFER_GETS,
  949.                       T.PLAN_OPERATION,
  950.                       T.STARTS,
  951.                       T.PLAN_PARTITION_START,
  952.                       T.PLAN_PARTITION_STOP,
  953.                       T.PHYSICAL_READ_BYTES,
  954.                       T.PHYSICAL_WRITE_BYTES,
  955.                       T.BLOCKING_INSTANCE,
  956.                       T.BLOCKING_SESSION,
  957.                       T.BLOCKING_SESSION_SERIAL#,
  958.                       T.LAST_CALL_ET,
  959.                       T.SQL_ID,
  960.                       T.SQL_EXEC_START,
  961.                       T.SQL_PLAN,
  962.                       T.LOGON_TIME,
  963.                       T.ASH_COUNTS,
  964.                       T.SESSION_INFO,
  965.                       '執行時間過大' MONITOR_TYPES
  966.                  FROM TMPS T
  967.                 WHERE T.ELAPSED_TIME_S > V_ELAPSED_TIME --5 * 60 * 60
  968.                   AND (NVL(PLAN_DEPTH, 1) = 1)

  969.                UNION ALL

  970.                ------------------------------------------ 分割槽表全分割槽掃描

  971.                SELECT T.INST_ID,
  972.                       T.SID,
  973.                       T.SERIAL#,
  974.                       T.SPID,
  975.                       T.OSUSER,
  976.                       T.USERNAME,
  977.                       T.EVENT,
  978.                       T.SESSION_STATE,
  979.                       T.SQL_TEXT,
  980.                       T.EXECUTIONS,
  981.                       T.ELAPSED_TIME_S,
  982.                       T.CPU_TIME,
  983.                       T.USER_IO_WAIT_TIME,
  984.                       T.BUFFER_GETS,
  985.                       T.PLAN_OPERATION,
  986.                       T.STARTS,
  987.                       T.PLAN_PARTITION_START,
  988.                       T.PLAN_PARTITION_STOP,
  989.                       T.PHYSICAL_READ_BYTES,
  990.                       T.PHYSICAL_WRITE_BYTES,
  991.                       T.BLOCKING_INSTANCE,
  992.                       T.BLOCKING_SESSION,
  993.                       T.BLOCKING_SESSION_SERIAL#,
  994.                       T.LAST_CALL_ET,
  995.                       T.SQL_ID,
  996.                       T.SQL_EXEC_START,
  997.                       T.SQL_PLAN,
  998.                       T.LOGON_TIME,
  999.                       T.ASH_COUNTS,
  1000.                       T.SESSION_INFO,
  1001.                       '分割槽表全分割槽掃描' MONITOR_TYPES
  1002.                  FROM TMPS T
  1003.                 WHERE T.PLAN_OPERATION LIKE 'PARTITION%'
  1004.                   AND T.PLAN_OPTIONS = 'ALL'
  1005.                -- AND T.ELAPSED_TIME_S >= 0.5 * 60 * 60

  1006.                UNION ALL

  1007.                ------------------------------------------ 執行計劃中COST花費超大

  1008.                SELECT T.INST_ID,
  1009.                       T.SID,
  1010.                       T.SERIAL#,
  1011.                       T.SPID,
  1012.                       T.OSUSER,
  1013.                       T.USERNAME,
  1014.                       T.EVENT,
  1015.                       T.SESSION_STATE,
  1016.                       T.SQL_TEXT,
  1017.                       T.EXECUTIONS,
  1018.                       T.ELAPSED_TIME_S,
  1019.                       T.CPU_TIME,
  1020.                       T.USER_IO_WAIT_TIME,
  1021.                       T.BUFFER_GETS,
  1022.                       T.PLAN_OPERATION,
  1023.                       T.STARTS,
  1024.                       T.PLAN_PARTITION_START,
  1025.                       T.PLAN_PARTITION_STOP,
  1026.                       T.PHYSICAL_READ_BYTES,
  1027.                       T.PHYSICAL_WRITE_BYTES,
  1028.                       T.BLOCKING_INSTANCE,
  1029.                       T.BLOCKING_SESSION,
  1030.                       T.BLOCKING_SESSION_SERIAL#,
  1031.                       T.LAST_CALL_ET,
  1032.                       T.SQL_ID,
  1033.                       T.SQL_EXEC_START,
  1034.                       T.SQL_PLAN,
  1035.                       T.LOGON_TIME,
  1036.                       T.ASH_COUNTS,
  1037.                       T.SESSION_INFO,
  1038.                       '執行計劃中COST花費超大【' || T.PLAN_COST || '】' MONITOR_TYPES
  1039.                  FROM TMPS T
  1040.                 WHERE T.PLAN_COST >= V_PLAN_COST
  1041.                   AND (NVL(PLAN_DEPTH, 1) = 1)
  1042.                            
  1043.                UNION ALL
  1044.                ------------------------------------------ 執行計劃中預估行數超大

  1045.                SELECT T.INST_ID,
  1046.                       T.SID,
  1047.                       T.SERIAL#,
  1048.                       T.SPID,
  1049.                       T.OSUSER,
  1050.                       T.USERNAME,
  1051.                       T.EVENT,
  1052.                       T.SESSION_STATE,
  1053.                       T.SQL_TEXT,
  1054.                       T.EXECUTIONS,
  1055.                       T.ELAPSED_TIME_S,
  1056.                       T.CPU_TIME,
  1057.                       T.USER_IO_WAIT_TIME,
  1058.                       T.BUFFER_GETS,
  1059.                       T.PLAN_OPERATION,
  1060.                       T.STARTS,
  1061.                       T.PLAN_PARTITION_START,
  1062.                       T.PLAN_PARTITION_STOP,
  1063.                       T.PHYSICAL_READ_BYTES,
  1064.                       T.PHYSICAL_WRITE_BYTES,
  1065.                       T.BLOCKING_INSTANCE,
  1066.                       T.BLOCKING_SESSION,
  1067.                       T.BLOCKING_SESSION_SERIAL#,
  1068.                       T.LAST_CALL_ET,
  1069.                       T.SQL_ID,
  1070.                       T.SQL_EXEC_START,
  1071.                       T.SQL_PLAN,
  1072.                       T.LOGON_TIME,
  1073.                       T.ASH_COUNTS,
  1074.                       T.SESSION_INFO,
  1075.                       '執行計劃中預估行數超大【' || T.PLAN_CARDINALITY || '】' MONITOR_TYPES
  1076.                  FROM TMPS T
  1077.                 WHERE T.PLAN_CARDINALITY > V_PLAN_CARDINALITY
  1078.                   AND (NVL(PLAN_DEPTH, 1) = 1)
  1079.                                       
  1080.                UNION ALL
  1081.                ------------------------------------------ SQL請求並行數過多

  1082.                SELECT T.INST_ID,
  1083.                       T.SID,
  1084.                       T.SERIAL#,
  1085.                       T.SPID,
  1086.                       T.OSUSER,
  1087.                       T.USERNAME,
  1088.                       T.EVENT,
  1089.                       T.SESSION_STATE,
  1090.                       T.SQL_TEXT,
  1091.                       T.EXECUTIONS,
  1092.                       T.ELAPSED_TIME_S,
  1093.                       T.CPU_TIME,
  1094.                       T.USER_IO_WAIT_TIME,
  1095.                       T.BUFFER_GETS,
  1096.                       T.PLAN_OPERATION,
  1097.                       T.STARTS,
  1098.                       T.PLAN_PARTITION_START,
  1099.                       T.PLAN_PARTITION_STOP,
  1100.                       T.PHYSICAL_READ_BYTES,
  1101.                       T.PHYSICAL_WRITE_BYTES,
  1102.                       T.BLOCKING_INSTANCE,
  1103.                       T.BLOCKING_SESSION,
  1104.                       T.BLOCKING_SESSION_SERIAL#,
  1105.                       T.LAST_CALL_ET,
  1106.                       T.SQL_ID,
  1107.                       T.SQL_EXEC_START,
  1108.                       T.SQL_PLAN,
  1109.                       T.LOGON_TIME,
  1110.                       T.ASH_COUNTS,
  1111.                       T.SESSION_INFO,
  1112.                       'SQL請求並行數過多【' || PX_MAXDOP || '】' MONITOR_TYPES
  1113.                  FROM TMPS T
  1114.                 WHERE T.PX_MAXDOP >= V_PARALLEL
  1115.                   AND (NVL(PLAN_DEPTH, 1) = 1)
  1116.                       
  1117.                UNION ALL
  1118.                ------------------------------------------ 系統預估的剩餘執行時間過長

  1119.                SELECT T.INST_ID,
  1120.                       T.SID,
  1121.                       T.SERIAL#,
  1122.                       T.SPID,
  1123.                       T.OSUSER,
  1124.                       T.USERNAME,
  1125.                       T.EVENT,
  1126.                       T.SESSION_STATE,
  1127.                       T.SQL_TEXT,
  1128.                       T.EXECUTIONS,
  1129.                       T.ELAPSED_TIME_S,
  1130.                       T.CPU_TIME,
  1131.                       T.USER_IO_WAIT_TIME,
  1132.                       T.BUFFER_GETS,
  1133.                       T.PLAN_OPERATION,
  1134.                       T.STARTS,
  1135.                       T.PLAN_PARTITION_START,
  1136.                       T.PLAN_PARTITION_STOP,
  1137.                       T.PHYSICAL_READ_BYTES,
  1138.                       T.PHYSICAL_WRITE_BYTES,
  1139.                       T.BLOCKING_INSTANCE,
  1140.                       T.BLOCKING_SESSION,
  1141.                       T.BLOCKING_SESSION_SERIAL#,
  1142.                       T.LAST_CALL_ET,
  1143.                       T.SQL_ID,
  1144.                       T.SQL_EXEC_START,
  1145.                       T.SQL_PLAN,
  1146.                       T.LOGON_TIME,
  1147.                       T.ASH_COUNTS,
  1148.                       T.SESSION_INFO,
  1149.                       '系統預估的剩餘執行時間過長【' || ROUND(D.TIME_REMAINING) || '】' MONITOR_TYPES
  1150.                  FROM TMPS T, GV$SESSION_LONGOPS D
  1151.                 WHERE T.SQL_EXEC_ID = D.SQL_EXEC_ID
  1152.                   AND T.SID = D.SID
  1153.                   AND T.SERIAL# = D.SERIAL#
  1154.                   AND D.TIME_REMAINING > V_ESTIMATE_TIME
  1155.                   AND T.INST_ID = D.INST_ID
  1156.                   AND D.TIME_REMAINING > 0
  1157.                   AND (NVL(PLAN_DEPTH, 1) = 1)
  1158.                                     
  1159.                UNION ALL
  1160.                ------------------------------------------ 等待事件異常

  1161.                SELECT T.INST_ID,
  1162.                       T.SID,
  1163.                       T.SERIAL#,
  1164.                       T.SPID,
  1165.                       T.OSUSER,
  1166.                       T.USERNAME,
  1167.                       T.EVENT,
  1168.                       T.SESSION_STATE,
  1169.                       T.SQL_TEXT,
  1170.                       T.EXECUTIONS,
  1171.                       T.ELAPSED_TIME_S,
  1172.                       T.CPU_TIME,
  1173.                       T.USER_IO_WAIT_TIME,
  1174.                       T.BUFFER_GETS,
  1175.                       T.PLAN_OPERATION,
  1176.                       T.STARTS,
  1177.                       T.PLAN_PARTITION_START,
  1178.                       T.PLAN_PARTITION_STOP,
  1179.                       T.PHYSICAL_READ_BYTES,
  1180.                       T.PHYSICAL_WRITE_BYTES,
  1181.                       T.BLOCKING_INSTANCE,
  1182.                       T.BLOCKING_SESSION,
  1183.                       T.BLOCKING_SESSION_SERIAL#,
  1184.                       T.LAST_CALL_ET,
  1185.                       T.SQL_ID,
  1186.                       T.SQL_EXEC_START,
  1187.                       T.SQL_PLAN,
  1188.                       T.LOGON_TIME,
  1189.                       T.ASH_COUNTS,
  1190.                       T.SESSION_INFO,
  1191.                       '等待事件異常【' || T.EVENT || '】' MONITOR_TYPES
  1192.                  FROM TMPS T
  1193.                 WHERE T.EVENT NOT IN
  1194.                       ('db file sequential read',
  1195.                        'db file scattered read',
  1196.                        'db file parallel write',
  1197.                        'db file parallel read')
  1198.                   AND (NVL(PLAN_DEPTH, 1) = 1)

  1199.                UNION ALL
  1200.                ------------------------------------------ TMP表空間佔用過大

  1201.                SELECT T.INST_ID,
  1202.                       T.SID,
  1203.                       T.SERIAL#,
  1204.                       T.SPID,
  1205.                       T.OSUSER,
  1206.                       T.USERNAME,
  1207.                       T.EVENT,
  1208.                       T.SESSION_STATE,
  1209.                       T.SQL_TEXT,
  1210.                       T.EXECUTIONS,
  1211.                       T.ELAPSED_TIME_S,
  1212.                       T.CPU_TIME,
  1213.                       T.USER_IO_WAIT_TIME,
  1214.                       T.BUFFER_GETS,
  1215.                       T.PLAN_OPERATION,
  1216.                       T.STARTS,
  1217.                       T.PLAN_PARTITION_START,
  1218.                       T.PLAN_PARTITION_STOP,
  1219.                       T.PHYSICAL_READ_BYTES,
  1220.                       T.PHYSICAL_WRITE_BYTES,
  1221.                       T.BLOCKING_INSTANCE,
  1222.                       T.BLOCKING_SESSION,
  1223.                       T.BLOCKING_SESSION_SERIAL#,
  1224.                       T.LAST_CALL_ET,
  1225.                       T.SQL_ID,
  1226.                       T.SQL_EXEC_START,
  1227.                       T.SQL_PLAN,
  1228.                       T.LOGON_TIME,
  1229.                       T.ASH_COUNTS,
  1230.                       T.SESSION_INFO,
  1231.                       'SQL佔用TMP表空間過大【' || C.BYTES || '】Bytes' MONITOR_TYPES
  1232.                  FROM TMPS T,
  1233.                       (SELECT A.INST_ID,
  1234.                               A.SESSION_ADDR,
  1235.                               SUM(A.BLOCKS) * 8 * 1024 BYTES
  1236.                          FROM GV$TEMPSEG_USAGE A
  1237.                         GROUP BY A.INST_ID, A.SESSION_ADDR) C
  1238.                 WHERE C.SESSION_ADDR = T.SADDR
  1239.                   AND C.INST_ID = T.INST_ID
  1240.                   AND C.BYTES > V_TMPSIZE --50 * 1024 * 1024 * 1024
  1241.                   AND (NVL(PLAN_DEPTH, 1) = 1)
  1242.                            
  1243.         
  1244.                UNION ALL
  1245.                ----------------------------------------- SQL佔用UNDO過大,INACTIVE的會話也可能佔用UNDO,但是這裡只記錄正在執行的SQL語句

  1246.                SELECT T.INST_ID,
  1247.                       T.SID,
  1248.                       T.SERIAL#,
  1249.                       T.SPID,
  1250.                       T.OSUSER,
  1251.                       T.USERNAME,
  1252.                       T.EVENT,
  1253.                       T.SESSION_STATE,
  1254.                       T.SQL_TEXT,
  1255.                       T.EXECUTIONS,
  1256.                       T.ELAPSED_TIME_S,
  1257.                       T.CPU_TIME,
  1258.                       T.USER_IO_WAIT_TIME,
  1259.                       T.BUFFER_GETS,
  1260.                       T.PLAN_OPERATION,
  1261.                       T.STARTS,
  1262.                       T.PLAN_PARTITION_START,
  1263.                       T.PLAN_PARTITION_STOP,
  1264.                       T.PHYSICAL_READ_BYTES,
  1265.                       T.PHYSICAL_WRITE_BYTES,
  1266.                       T.BLOCKING_INSTANCE,
  1267.                       T.BLOCKING_SESSION,
  1268.                       T.BLOCKING_SESSION_SERIAL#,
  1269.                       T.LAST_CALL_ET,
  1270.                       T.SQL_ID,
  1271.                       T.SQL_EXEC_START,
  1272.                       T.SQL_PLAN,
  1273.                       T.LOGON_TIME,
  1274.                       T.ASH_COUNTS,
  1275.                       T.SESSION_INFO,
  1276.                       'SQL佔用UNDO過大【' || USED_SIZE_BYTES || '】Bytes' MONITOR_TYPES
  1277.                  FROM TMPS T,
  1278.                       (SELECT ST.ADDR,
  1279.                               ST.INST_ID,
  1280.                               (ST.USED_UBLK * 8 * 1024) USED_SIZE_BYTES
  1281.                          FROM GV$TRANSACTION ST, V$ROLLNAME R, GV$ROLLSTAT G
  1282.                         WHERE ST.XIDUSN = R.USN
  1283.                           AND R.USN = G.USN
  1284.                           AND G.INST_ID = ST.INST_ID) V1
  1285.                 WHERE V1.ADDR = T.TADDR
  1286.                   AND T.INST_ID = V1.INST_ID
  1287.                   AND USED_SIZE_BYTES > V_UNDOSIZE -- 50 * 1024 * 1024 * 1024
  1288.                  
  1289.                UNION ALL
  1290.                ----------------------------------------- 耗費效能SQL

  1291.                SELECT T.INST_ID,
  1292.                       T.SID,
  1293.                       T.SERIAL#,
  1294.                       T.SPID,
  1295.                       T.OSUSER,
  1296.                       T.USERNAME,
  1297.                       T.EVENT,
  1298.                       T.SESSION_STATE,
  1299.                       T.SQL_TEXT,
  1300.                       T.EXECUTIONS,
  1301.                       T.ELAPSED_TIME_S,
  1302.                       T.CPU_TIME,
  1303.                       T.USER_IO_WAIT_TIME,
  1304.                       T.BUFFER_GETS,
  1305.                       T.PLAN_OPERATION,
  1306.                       T.STARTS,
  1307.                       T.PLAN_PARTITION_START,
  1308.                       T.PLAN_PARTITION_STOP,
  1309.                       T.PHYSICAL_READ_BYTES,
  1310.                       T.PHYSICAL_WRITE_BYTES,
  1311.                       T.BLOCKING_INSTANCE,
  1312.                       T.BLOCKING_SESSION,
  1313.                       T.BLOCKING_SESSION_SERIAL#,
  1314.                       T.LAST_CALL_ET,
  1315.                       T.SQL_ID,
  1316.                       T.SQL_EXEC_START,
  1317.                       T.SQL_PLAN,
  1318.                       T.LOGON_TIME,
  1319.                       T.ASH_COUNTS,
  1320.                       T.SESSION_INFO,
  1321.                       'ASH捕獲的次數【' || T.ASH_COUNTS || '】【' || SESSION_STATE || '】' MONITOR_TYPES
  1322.                  FROM TMPS T
  1323.                 WHERE T.ASH_COUNTS >= 4
  1324.                   AND (NVL(PLAN_DEPTH, 1) = 1)

  1325.                 ) T;


  1326.     COMMIT;
  1327.     /*
  1328.     EXCEPTION
  1329.       WHEN OTHERS THEN
  1330.         COMMIT;*/

  1331.   END P_SQL_EPP_LHR;

  1332.   PROCEDURE P_SQL_EPP2_LHR AS
  1333.     -----------------------------------------------------------------------------------
  1334.     -- Created on 2015/11/21 16:49:28 by lhr
  1335.     --Changed on 2017/04/03 16:49:28 by lhr
  1336.     -- function: 監控已經執行完畢的SQL
  1337.     -----------------------------------------------------------------------------------

  1338.     V_TMPSIZE NUMBER := 5368709120; --bytes
  1339.     V_UNDOSIZE NUMBER := 5368709120; --bytes
  1340.     V_PLAN_COST NUMBER := 3107523095; --cost 花費
  1341.     V_PLAN_CARDINALITY NUMBER := 30748908521460; --預估行數
  1342.     V_ELAPSED_TIME NUMBER := 10000000; ---執行時間,單位:微秒,例執行時間設定5小時,則: 5h*60*60*1000000 ,1秒等於1000000微秒
  1343.     V_EXECUTIONS NUMBER := 1000; --執行次數
  1344.     V_PARALLEL NUMBER := 4; --SQL開並行的個數
  1345.     V_ESTIMATE_TIME NUMBER := 900; --系統預估的執行時間
  1346.     V_LOGICAL_READS NUMBER := 5368709120; --bytes
  1347.     V_DISK_READS NUMBER := 5368709120; --bytes

  1348.   BEGIN

  1349.     SELECT SUM(CASE T.PARAM_NAME
  1350.                  WHEN 'V_UNDOSIZE' THEN
  1351.                   T.PARAM_VALUE
  1352.                END) V_UNDOSIZE,
  1353.            SUM(CASE T.PARAM_NAME
  1354.                  WHEN 'V_TMPSIZE' THEN
  1355.                   T.PARAM_VALUE
  1356.                END) V_TMPSIZE,
  1357.            SUM(CASE T.PARAM_NAME
  1358.                  WHEN 'V_PLAN_COST' THEN
  1359.                   T.PARAM_VALUE
  1360.                END) V_PLAN_COST,
  1361.            SUM(CASE T.PARAM_NAME
  1362.                  WHEN 'V_PLAN_CARDINALITY' THEN
  1363.                   T.PARAM_VALUE
  1364.                END) V_PLAN_CARDINALITY,
  1365.            SUM(CASE T.PARAM_NAME
  1366.                  WHEN 'V_ELAPSED_TIME' THEN
  1367.                   T.PARAM_VALUE
  1368.                END) V_ELAPSED_TIME,
  1369.            SUM(CASE T.PARAM_NAME
  1370.                  WHEN 'V_EXECUTIONS' THEN
  1371.                   T.PARAM_VALUE
  1372.                END) V_EXECUTIONS,
  1373.            SUM(CASE T.PARAM_NAME
  1374.                  WHEN 'V_PARALLEL' THEN
  1375.                   T.PARAM_VALUE
  1376.                END) V_PARALLEL,
  1377.            SUM(CASE T.PARAM_NAME
  1378.                  WHEN 'V_ESTIMATE_TIME' THEN
  1379.                   T.PARAM_VALUE
  1380.                END) V_ESTIMATE_TIME,
  1381.            SUM(CASE T.PARAM_NAME
  1382.                  WHEN 'V_LOGICAL_READS' THEN
  1383.                   T.PARAM_VALUE
  1384.                END) V_UNDOSIZE,
  1385.            SUM(CASE T.PARAM_NAME
  1386.                  WHEN 'V_DISK_READS' THEN
  1387.                   T.PARAM_VALUE
  1388.                END) V_DISK_READS
  1389.       INTO V_UNDOSIZE,
  1390.            V_TMPSIZE,
  1391.            V_PLAN_COST,
  1392.            V_PLAN_CARDINALITY,
  1393.            V_ELAPSED_TIME,
  1394.            V_EXECUTIONS,
  1395.            V_PARALLEL,
  1396.            V_ESTIMATE_TIME,
  1397.            V_LOGICAL_READS,
  1398.            V_DISK_READS
  1399.       FROM XB_SQL_PARAMETERS_LHR T
  1400.      WHERE T.PARAM_NAME IN ('V_LOGICAL_READS',
  1401.                             'V_DISK_READS',
  1402.                             'V_UNDOSIZE',
  1403.                             'V_TMPSIZE',
  1404.                             'V_PLAN_COST',
  1405.                             'V_PLAN_CARDINALITY',
  1406.                             'V_ELAPSED_TIME',
  1407.                             'V_EXECUTIONS',
  1408.                             'V_PARALLEL',
  1409.                             'V_ESTIMATE_TIME');

  1410.     INSERT INTO XB_SQL_MONITOR_PP_LHR
  1411.       (ID,
  1412.        INST_ID,
  1413.        SID,
  1414.        SERIAL#,
  1415.        USERNAME,
  1416.        SQL_TEXT,
  1417.        EXECUTIONS,
  1418.        SQL_ID,
  1419.        SQL_EXEC_START,
  1420.        LAST_LOAD_TIME,
  1421.        LAST_ACTIVE_TIME,
  1422.        ELAPSED_TIME,
  1423.        ELAPSED_TIME1,
  1424.        MONITOR_TYPES,
  1425.        MONITOR_TYPES1,
  1426.        MONITOR_VALUE,
  1427.        SESSION_INFO,
  1428.        SESSION_STATE,
  1429.        EVENT,
  1430.        CPU_TIME,
  1431.        BUFFER_GETS,
  1432.        PHYSICAL_READ_BYTES,
  1433.        PHYSICAL_WRITE_BYTES,
  1434.        BLOCKING_INSTANCE,
  1435.        BLOCKING_SESSION,
  1436.        ASH_COUNTS,
  1437.        IN_DATE)
  1438.       SELECT S_XB_SQL_MONITOR_PP_LHR.NEXTVAL,
  1439.              INST_ID,
  1440.              SID,
  1441.              SERIAL#,
  1442.              USERNAME,
  1443.              SQL_TEXT,
  1444.              EXECUTIONS,
  1445.              SQL_ID,
  1446.              SQL_EXEC_START,
  1447.              '' LAST_LOAD_TIME,
  1448.              '' LAST_ACTIVE_TIME,
  1449.              F_GET_TOTAL_TIME_LHR(ELAPSED_TIME_S),
  1450.              ELAPSED_TIME_S,
  1451.              MONITOR_TYPES,
  1452.              '' MONITOR_TYPES1,
  1453.              '' MONITOR_VALUE,
  1454.              SESSION_INFO,
  1455.              SESSION_STATE,
  1456.              EVENT,
  1457.              CPU_TIME,
  1458.              BUFFER_GETS,
  1459.              PHYSICAL_READ_BYTES,
  1460.              PHYSICAL_WRITE_BYTES,
  1461.              BLOCKING_INSTANCE,
  1462.              BLOCKING_SESSION,
  1463.              ASH_COUNTS,
  1464.              SYSDATE IN_DATE
  1465.         FROM (

  1466.              WITH TMPA AS (SELECT ASH.INST_ID,
  1467.                                   ASH.SESSION_ID SID,
  1468.                                   ASH.SESSION_SERIAL# SERIAL#,
  1469.                                   ASH.SESSION_TYPE,
  1470.                                   ASH.USER_ID,
  1471.                                   ASH.SQL_ID,
  1472.                                   ASH.SQL_CHILD_NUMBER,
  1473.                                   ASH.SQL_OPNAME,
  1474.                                   ASH.SQL_EXEC_ID,
  1475.                                   ASH.EVENT,
  1476.                                   ASH.SESSION_STATE,
  1477.                                   ASH.BLOCKING_SESSION,
  1478.                                   ASH.BLOCKING_SESSION_SERIAL#,
  1479.                                   ASH.BLOCKING_INST_ID BLOCKING_INSTANCE,
  1480.                                   ASH.CLIENT_ID,
  1481.                                   ASH.MACHINE,
  1482.                                   GVS.PARSING_SCHEMA_NAME USERNAME,
  1483.                                   ASH.SQL_EXEC_START,
  1484.                                   (ASH.MODULE || '--' || ASH.ACTION || '--' ||
  1485.                                   ASH.PROGRAM || '--' || ASH.MACHINE || '--' ||
  1486.                                   ASH.CLIENT_ID || '--' || ASH.SESSION_TYPE) SESSION_INFO,
  1487.                                   COUNT(*) ASH_COUNTS,
  1488.                                   (GVS.ELAPSED_TIME / 1000000) ELAPSED_TIME_S,
  1489.                                   (GVS.CPU_TIME / 1000000) CPU_TIME,
  1490.                                   GVS.EXECUTIONS,
  1491.                                   GVS.DISK_READS,
  1492.                                   GVS.BUFFER_GETS,
  1493.                                   GVS.LAST_ACTIVE_TIME,
  1494.                                   GVS.LAST_LOAD_TIME,
  1495.                                   GVS.PHYSICAL_READ_BYTES,
  1496.                                   GVS.PHYSICAL_WRITE_BYTES,
  1497.                                   GVS.SQL_TEXT
  1498.                              FROM GV$ACTIVE_SESSION_HISTORY ASH, GV$SQL GVS
  1499.                             WHERE ASH.INST_ID = GVS.INST_ID
  1500.                               AND GVS.SQL_ID = ASH.SQL_ID
  1501.                               AND ASH.SQL_ID IS NOT NULL
  1502.                               AND ASH.SAMPLE_TIME > SYSDATE - 30 / 1440
  1503.                             GROUP BY ASH.INST_ID,
  1504.                                      ASH.SESSION_ID,
  1505.                                      ASH.SESSION_SERIAL#,
  1506.                                      ASH.SESSION_TYPE,
  1507.                                      ASH.USER_ID,
  1508.                                      ASH.SQL_ID,
  1509.                                      ASH.SQL_CHILD_NUMBER,
  1510.                                      ASH.SQL_OPNAME,
  1511.                                      ASH.SQL_EXEC_ID,
  1512.                                      ASH.EVENT,
  1513.                                      ASH.SESSION_STATE,
  1514.                                      ASH.BLOCKING_SESSION,
  1515.                                      ASH.BLOCKING_SESSION_SERIAL#,
  1516.                                      ASH.BLOCKING_INST_ID,
  1517.                                      ASH.CLIENT_ID,
  1518.                                      ASH.MACHINE,
  1519.                                      GVS.PARSING_SCHEMA_NAME,
  1520.                                      ASH.SQL_EXEC_START,
  1521.                                      (ASH.MODULE || '--' || ASH.ACTION || '--' ||
  1522.                                      ASH.PROGRAM || '--' || ASH.MACHINE || '--' ||
  1523.                                      ASH.CLIENT_ID || '--' ||
  1524.                                      ASH.SESSION_TYPE),
  1525.                                      (GVS.ELAPSED_TIME / 1000000),
  1526.                                      (GVS.CPU_TIME / 1000000),
  1527.                                      GVS.EXECUTIONS,
  1528.                                      GVS.DISK_READS,
  1529.                                      GVS.BUFFER_GETS,
  1530.                                      GVS.LAST_ACTIVE_TIME,
  1531.                                      GVS.LAST_LOAD_TIME,
  1532.                                      GVS.PHYSICAL_READ_BYTES,
  1533.                                      GVS.PHYSICAL_WRITE_BYTES,
  1534.                                      GVS.SQL_TEXT
  1535.                            HAVING COUNT(*) > 6)

  1536.              ------------------------------------------ 物理讀過大
  1537.                SELECT INST_ID,
  1538.                       SID,
  1539.                       SERIAL#,
  1540.                       USERNAME,
  1541.                       SQL_TEXT,
  1542.                       EXECUTIONS,
  1543.                       SQL_ID,
  1544.                       SQL_EXEC_START,
  1545.                       LAST_LOAD_TIME,
  1546.                       LAST_ACTIVE_TIME,
  1547.                       ELAPSED_TIME_S,
  1548.                       SESSION_INFO,
  1549.                       SESSION_STATE,
  1550.                       EVENT,
  1551.                       CPU_TIME,
  1552.                       DISK_READS,
  1553.                       BUFFER_GETS,
  1554.                       PHYSICAL_READ_BYTES,
  1555.                       PHYSICAL_WRITE_BYTES,
  1556.                       BLOCKING_INSTANCE,
  1557.                       BLOCKING_SESSION,
  1558.                       ASH_COUNTS,
  1559.                       '物理讀過大' MONITOR_TYPES
  1560.                  FROM TMPA T
  1561.                 WHERE T.DISK_READS > V_DISK_READS
  1562.                      
  1563.                UNION ALL
  1564.                ------------------------------------------ 邏輯讀過大
  1565.                SELECT INST_ID,
  1566.                       SID,
  1567.                       SERIAL#,
  1568.                       USERNAME,
  1569.                       SQL_TEXT,
  1570.                       EXECUTIONS,
  1571.                       SQL_ID,
  1572.                       SQL_EXEC_START,
  1573.                       LAST_LOAD_TIME,
  1574.                       LAST_ACTIVE_TIME,
  1575.                       ELAPSED_TIME_S,
  1576.                       SESSION_INFO,
  1577.                       SESSION_STATE,
  1578.                       EVENT,
  1579.                       CPU_TIME,
  1580.                       DISK_READS,
  1581.                       BUFFER_GETS,
  1582.                       PHYSICAL_READ_BYTES,
  1583.                       PHYSICAL_WRITE_BYTES,
  1584.                       BLOCKING_INSTANCE,
  1585.                       BLOCKING_SESSION,
  1586.                       ASH_COUNTS,
  1587.                       '邏輯讀過大' MONITOR_TYPES
  1588.                  FROM TMPA T
  1589.                 WHERE T.BUFFER_GETS > V_LOGICAL_READS
  1590.                 
  1591.                UNION ALL
  1592.                ------------------------------------------ 執行次數過大
  1593.                SELECT INST_ID,
  1594.                       SID,
  1595.                       SERIAL#,
  1596.                       USERNAME,
  1597.                       SQL_TEXT,
  1598.                       EXECUTIONS,
  1599.                       SQL_ID,
  1600.                       SQL_EXEC_START,
  1601.                       LAST_LOAD_TIME,
  1602.                       LAST_ACTIVE_TIME,
  1603.                       ELAPSED_TIME_S,
  1604.                       SESSION_INFO,
  1605.                       SESSION_STATE,
  1606.                       EVENT,
  1607.                       CPU_TIME,
  1608.                       DISK_READS,
  1609.                       BUFFER_GETS,
  1610.                       PHYSICAL_READ_BYTES,
  1611.                       PHYSICAL_WRITE_BYTES,
  1612.                       BLOCKING_INSTANCE,
  1613.                       BLOCKING_SESSION,
  1614.                       ASH_COUNTS,
  1615.                       '執行次數過大' MONITOR_TYPES
  1616.                  FROM TMPA T
  1617.                 WHERE T.EXECUTIONS > V_EXECUTIONS
  1618.                      
  1619.                UNION ALL
  1620.                ------------------------------------------ SQL總執行時間過大
  1621.                SELECT INST_ID,
  1622.                       SID,
  1623.                       SERIAL#,
  1624.                       USERNAME,
  1625.                       SQL_TEXT,
  1626.                       EXECUTIONS,
  1627.                       SQL_ID,
  1628.                       SQL_EXEC_START,
  1629.                       LAST_LOAD_TIME,
  1630.                       LAST_ACTIVE_TIME,
  1631.                       ELAPSED_TIME_S,
  1632.                       SESSION_INFO,
  1633.                       SESSION_STATE,
  1634.                       EVENT,
  1635.                       CPU_TIME,
  1636.                       DISK_READS,
  1637.                       BUFFER_GETS,
  1638.                       PHYSICAL_READ_BYTES,
  1639.                       PHYSICAL_WRITE_BYTES,
  1640.                       BLOCKING_INSTANCE,
  1641.                       BLOCKING_SESSION,
  1642.                       ASH_COUNTS,
  1643.                       'SQL總執行時間過大' MONITOR_TYPES
  1644.                  FROM TMPA T
  1645.                 WHERE T.ELAPSED_TIME_S > V_ELAPSED_TIME);

  1646.     COMMIT;

  1647.   END P_SQL_EPP2_LHR;

  1648.   PROCEDURE P_TUNING_SQL AS
  1649.     MY_TASK_NAME VARCHAR2(255);

  1650.   BEGIN
  1651.     ---- 調優捕捉到的垃圾SQL
  1652.     FOR CUR IN (SELECT DISTINCT A.USERNAME, A.SQL_ID, A.SQL_TEXT
  1653.                   FROM XB_SQL_MONITOR_PP_LHR A
  1654.                  WHERE A.TUNING_RESULT IS NULL
  1655.                    AND A.IN_DATE >= SYSDATE - 1) LOOP

  1656.       MY_TASK_NAME := DBMS_SQLTUNE.CREATE_TUNING_TASK(SQL_TEXT => CUR.SQL_TEXT,
  1657.                                                       USER_NAME => CUR.USERNAME);
  1658.       DBMS_SQLTUNE.EXECUTE_TUNING_TASK(TASK_NAME => MY_TASK_NAME);

  1659.       UPDATE XB_SQL_MONITOR_PP_LHR T
  1660.          SET T.TUNING_RESULT = DBMS_SQLTUNE.REPORT_TUNING_TASK(MY_TASK_NAME),
  1661.              T.TUNING_TIME = SYSDATE
  1662.        WHERE T.SQL_ID = CUR.SQL_ID;
  1663.       COMMIT;
  1664.     END LOOP;
  1665.     /*
  1666.     EXCEPTION
  1667.       WHEN OTHERS THEN
  1668.         dbms_output.put_line('error:' || SQLCODE || ',' || SQLERRM);*/
  1669.   END P_TUNING_SQL;

  1670.   PROCEDURE P_GET_PPSQL_PARAMETER AS
  1671.     V_TMPSIZE NUMBER := 5368709120; --bytes
  1672.     V_UNDOSIZE NUMBER := 5368709120; --bytes
  1673.     V_PLAN_COST NUMBER := 3107523095; --cost 花費
  1674.     V_PLAN_CARDINALITY NUMBER := 30748908521460; --預估行數
  1675.     V_ELAPSED_TIME NUMBER := 10000000; ---執行時間,例執行時間設定5小時,則: 5h*60*60*1000000
  1676.     V_EXECUTIONS NUMBER := 1000; --執行次數
  1677.     V_LOGICAL_READS NUMBER := 1000; --邏輯讀
  1678.     V_DISK_READS NUMBER := 1000; --物理讀
  1679.   BEGIN

  1680.     SELECT ROUND(SUM(BYTES) / 2)
  1681.       INTO V_TMPSIZE
  1682.       FROM V$TEMPFILE D
  1683.      WHERE D.STATUS = 'ONLINE';

  1684.     SELECT ROUND(SUM(BYTES) / 2)
  1685.       INTO V_UNDOSIZE
  1686.       FROM DBA_DATA_FILES D, DBA_TABLESPACES DT
  1687.      WHERE DT.TABLESPACE_NAME = D.TABLESPACE_NAME
  1688.        AND DT.CONTENTS = 'UNDO'
  1689.        AND DT.STATUS = 'ONLINE'
  1690.      GROUP BY D.TABLESPACE_NAME;

  1691.     SELECT ROUND(MAX(A.COST) * 0.8), ROUND(MAX(A.CARDINALITY) * 0.8)
  1692.       INTO V_PLAN_COST, V_PLAN_CARDINALITY
  1693.       FROM GV$SQL_PLAN A
  1694.      WHERE A.OPERATION <> 'MERGE JOIN'
  1695.        AND A.OPTIONS <> 'CARTESIAN'
  1696.        AND NOT EXISTS
  1697.      (SELECT 1
  1698.               FROM GV$SQLAREA B
  1699.              WHERE B.SQL_ID = A.SQL_ID
  1700.                AND B.INST_ID = A.INST_ID
  1701.                AND B.PARSING_SCHEMA_NAME LIKE '%SYS%');

  1702.     SELECT ROUND(MAX(A.ELAPSED_TIME /
  1703.                      (DECODE(A.EXECUTIONS, 0, 1, A.EXECUTIONS)) / 1000000) * 0.8),
  1704.            ROUND(MAX(A.EXECUTIONS) * 0.8),
  1705.            ROUND(MAX(A.BUFFER_GETS /
  1706.                      (DECODE(A.EXECUTIONS, 0, 1, A.EXECUTIONS))) * 0.9),
  1707.            ROUND(MAX(A.DISK_READS /
  1708.                      (DECODE(A.EXECUTIONS, 0, 1, A.EXECUTIONS))) * 0.9)
  1709.       INTO V_ELAPSED_TIME, V_EXECUTIONS, V_LOGICAL_READS, V_DISK_READS
  1710.       FROM GV$SQLAREA A
  1711.      WHERE NOT EXISTS (SELECT 1
  1712.               FROM GV$SQL_PLAN AA
  1713.              WHERE A.SQL_ID = AA.SQL_ID
  1714.                AND AA.OPERATION = 'MERGE JOIN'
  1715.                AND AA.OPTIONS = 'CARTESIAN'
  1716.                AND AA.INST_ID = A.INST_ID)
  1717.        AND A.PARSING_SCHEMA_NAME NOT LIKE '%SYS%';

  1718.     UPDATE XB_SQL_PARAMETERS_LHR T
  1719.        SET T.PARAM_VALUE = V_TMPSIZE
  1720.      WHERE UPPER(T.PARAM_NAME) = UPPER('v_tmpsize');
  1721.     UPDATE XB_SQL_PARAMETERS_LHR T
  1722.        SET T.PARAM_VALUE = V_UNDOSIZE
  1723.      WHERE UPPER(T.PARAM_NAME) = UPPER('v_undosize');
  1724.     UPDATE XB_SQL_PARAMETERS_LHR T
  1725.        SET T.PARAM_VALUE = V_PLAN_COST
  1726.      WHERE UPPER(T.PARAM_NAME) = UPPER('v_plan_cost');
  1727.     UPDATE XB_SQL_PARAMETERS_LHR T
  1728.        SET T.PARAM_VALUE = V_PLAN_CARDINALITY
  1729.      WHERE UPPER(T.PARAM_NAME) = UPPER('v_PLAN_CARDINALITY');
  1730.     UPDATE XB_SQL_PARAMETERS_LHR T
  1731.        SET T.PARAM_VALUE = V_ELAPSED_TIME
  1732.      WHERE UPPER(T.PARAM_NAME) IN
  1733.            (UPPER('v_ELAPSED_TIME'), UPPER('V_ESTIMATE_TIME'));
  1734.     UPDATE XB_SQL_PARAMETERS_LHR T
  1735.        SET T.PARAM_VALUE = V_EXECUTIONS
  1736.      WHERE UPPER(T.PARAM_NAME) = UPPER('v_EXECUTIONS');
  1737.     UPDATE XB_SQL_PARAMETERS_LHR T
  1738.        SET T.PARAM_VALUE = V_EXECUTIONS
  1739.      WHERE UPPER(T.PARAM_NAME) = UPPER('V_LOGICAL_READS');
  1740.     UPDATE XB_SQL_PARAMETERS_LHR T
  1741.        SET T.PARAM_VALUE = V_EXECUTIONS
  1742.      WHERE UPPER(T.PARAM_NAME) = UPPER('V_DISK_READS');

  1743.     COMMIT;

  1744.   EXCEPTION
  1745.     WHEN OTHERS THEN
  1746.       DBMS_OUTPUT.PUT_LINE('error:' || SQLCODE || ',' || SQLERRM);
  1747.   END P_GET_PPSQL_PARAMETER;

  1748.   FUNCTION F_GET_RIGHT_SIZE_LHR(P_SIZE IN VARCHAR2) RETURN VARCHAR2 AS

  1749.     V_RESULTS VARCHAR2(255);

  1750.   BEGIN

  1751.     SELECT CASE
  1752.              WHEN P_SIZE = 0 THEN
  1753.               0 || ''
  1754.              WHEN P_SIZE < 1024 AND P_SIZE > 0 THEN
  1755.               P_SIZE || 'BYTES'
  1756.              WHEN P_SIZE >= 1024 AND P_SIZE < 1048576 THEN
  1757.               ROUND(P_SIZE / 1024, 3) || 'KB'
  1758.              WHEN P_SIZE >= 1048576 AND P_SIZE < 1073741824 THEN
  1759.               ROUND(P_SIZE / 1048576, 3) || 'M'
  1760.              WHEN P_SIZE >= 1073741824 AND P_SIZE < 1099511627776 THEN
  1761.               ROUND(P_SIZE / 1073741824, 3) || 'G'
  1762.              WHEN P_SIZE >= 1099511627776 AND P_SIZE < 1125899906842624 THEN
  1763.               ROUND(P_SIZE / 1099511627776, 3) || 'T'
  1764.            END AS SIZES
  1765.       INTO V_RESULTS
  1766.       FROM DUAL;

  1767.     RETURN V_RESULTS;
  1768.   EXCEPTION
  1769.     WHEN OTHERS THEN
  1770.       NULL;
  1771.       RETURN P_SIZE;
  1772.   END;

  1773.   FUNCTION F_GET_TOTAL_TIME_LHR(P_TOTAL_SECONDS NUMBER,
  1774.                                 P_FLAG VARCHAR2 DEFAULT 'S')
  1775.     RETURN VARCHAR2 DETERMINISTIC IS
  1776.     -----------------------------------------------------------------------------------
  1777.     -- Created on 2012/10/18 16:49:28 by lhr
  1778.     --Changed on 2015/9/3 16:49:28 by lhr
  1779.     -- function: 該函式 返回一個秒轉換為標準時間
  1780.     -----------------------------------------------------------------------------------
  1781.     V_TMP FLOAT(18);
  1782.     V_RETURNS VARCHAR2(200);
  1783.     V_TOTAL_SECONDS NUMBER := P_TOTAL_SECONDS;
  1784.   BEGIN

  1785.     IF P_TOTAL_SECONDS IS NULL OR P_TOTAL_SECONDS = 0 THEN

  1786.       RETURN 0 || '秒';

  1787.     END IF;

  1788.     ---傳入的為天轉換為秒
  1789.     IF UPPER(P_FLAG) = 'D' THEN
  1790.       V_TOTAL_SECONDS := P_TOTAL_SECONDS * 24 * 60 * 60;
  1791.     END IF;

  1792.     -- 首先處理日期

  1793.     --年
  1794.     V_TMP := TRUNC(V_TOTAL_SECONDS / (12 * 30 * 24 * 3600));

  1795.     IF V_TMP > 0 THEN
  1796.       V_RETURNS := V_TMP || '年';

  1797.     END IF;

  1798.     ---月
  1799.     V_TMP := TRUNC(MOD(V_TOTAL_SECONDS, (12 * 30 * 24 * 3600)) /
  1800.                    (3600 * 24 * 30));

  1801.     IF V_TMP > 0 THEN
  1802.       V_RETURNS := V_RETURNS || V_TMP || '月';

  1803.     END IF;

  1804.     --天
  1805.     V_TMP := TRUNC(MOD(V_TOTAL_SECONDS, (3600 * 24 * 30)) / (3600 * 24));

  1806.     IF V_TMP > 0 THEN
  1807.       V_RETURNS := V_RETURNS || V_TMP || '天';

  1808.     END IF;

  1809.     --處理小時

  1810.     V_TMP := TRUNC(MOD(V_TOTAL_SECONDS, 3600 * 24) / 3600);

  1811.     IF V_TMP > 0 THEN
  1812.       V_RETURNS := V_RETURNS || V_TMP || '時';
  1813.     END IF;

  1814.     --處理分鐘

  1815.     V_TMP := TRUNC(MOD(V_TOTAL_SECONDS, 3600) / 60);

  1816.     IF V_TMP > 0 THEN
  1817.       V_RETURNS := V_RETURNS || V_TMP || '分';
  1818.     END IF;

  1819.     --處理秒鐘
  1820.     V_TMP := TRUNC(MOD(V_TOTAL_SECONDS, 60), 3);

  1821.     IF V_TMP > 0 AND V_TMP < 1 THEN
  1822.       V_RETURNS := V_RETURNS || '0' || V_TMP || '秒';

  1823.     ELSIF V_TMP >= 1 THEN
  1824.       V_RETURNS := V_RETURNS || V_TMP || '秒';
  1825.     ELSIF V_TMP = 0 AND V_RETURNS IS NULL THEN
  1826.       V_RETURNS := '0' || TRUNC(V_TOTAL_SECONDS, 6) || '秒';
  1827.     END IF;

  1828.     RETURN V_RETURNS;

  1829.   END F_GET_TOTAL_TIME_LHR;
  1830.   ---------------------------------------------------------------------------------------------------------------------------------
  1831.   ---------------------------------------------------------------------------------------------------------------------------------

  1832.   FUNCTION F_GET_SQL_OBJECT_LHR(P_SQL VARCHAR2,
  1833.                                 P_COMMAND VARCHAR2 DEFAULT 'INTO',
  1834.                                 PFLAG NUMBER DEFAULT 1) RETURN VARCHAR2 IS
  1835.     -----------------------------------------------------------------------------------
  1836.     -- CREATED ON 2012/8/20 11:33:07 BY LHR
  1837.     --CHANGED ON 2012/8/20 11:33:07 BY LHR
  1838.     -- FUNCTION: 該函式返回
  1839.     -----------------------------------------------------------------------------------

  1840.     --- 1、 回車--》 空格 2、 取前1500個字母 3、多空格--》單空格
  1841.     V_SQL VARCHAR2(32767) := TRIM(REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(REPLACE(REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(UPPER(P_SQL),
  1842.                                                                                                                                       '/\*.*?\*/'),
  1843.                                                                                                                        '--.*+'),
  1844.                                                                                                         CHR(10),
  1845.                                                                                                         ' '),
  1846.                                                                                                 CHR(13),
  1847.                                                                                                 ' '),
  1848.                                                                                         '[ ]+',
  1849.                                                                                         ' '),
  1850.                                                                          '/\*.*?\*/'),
  1851.                                                           '[ ]+',
  1852.                                                           ' '));
  1853.     V_OBJECT_NAME VARCHAR2(4000);
  1854.     V_OBJECT_OWNER VARCHAR2(4000);
  1855.     V_L_INTO NUMBER;
  1856.     V_L_TABR NUMBER;
  1857.   BEGIN
  1858.     ---從第一個INSERT位置開始擷取50個字元
  1859.     V_SQL := REPLACE(SUBSTR(V_SQL, INSTR(V_SQL, P_COMMAND), 1000), '(', ' ');
  1860.     --INTO的位置 表名前的空格
  1861.     V_L_INTO := INSTR(V_SQL, P_COMMAND) + LENGTH(P_COMMAND) + 1;

  1862.     --I表名後的空格位置
  1863.     V_L_TABR := INSTR(V_SQL,
  1864.                       ' ',
  1865.                       INSTR(V_SQL, P_COMMAND) + LENGTH(P_COMMAND) + 1,
  1866.                       1);

  1867.     ----- 表名
  1868.     V_OBJECT_NAME := SUBSTR(V_SQL, V_L_INTO, V_L_TABR - V_L_INTO);

  1869.     IF V_OBJECT_NAME LIKE '%.%' THEN

  1870.       V_OBJECT_OWNER := SUBSTR(V_OBJECT_NAME,
  1871.                                1,
  1872.                                INSTR(V_OBJECT_NAME, '.') - 1); --表名前的OWNER

  1873.       V_OBJECT_NAME := SUBSTR(V_OBJECT_NAME, INSTR(V_OBJECT_NAME, '.') + 1); -- 去掉表名前的OWNER
  1874.     END IF;

  1875.     IF PFLAG = 1 THEN
  1876.       RETURN V_OBJECT_NAME;
  1877.     ELSE
  1878.       RETURN V_OBJECT_OWNER;
  1879.     END IF;
  1880.   EXCEPTION
  1881.     WHEN OTHERS THEN
  1882.       NULL;
  1883.   END F_GET_SQL_OBJECT_LHR;

  1884.   FUNCTION F_GET_SQL_COMMAND_LHR(P_SQL VARCHAR2) RETURN VARCHAR2 IS
  1885.     -----------------------------------------------------------------------------------
  1886.     -- CREATED ON 2012/8/20 11:33:07 BY LHR
  1887.     --CHANGED ON 2012/8/20 11:33:07 BY LHR
  1888.     -- FUNCTION: 該函式返回SQL語句的型別
  1889.     -----------------------------------------------------------------------------------

  1890.     --- 1、 回車--》 空格 2、 取前1500個字母 3、多空格--》單空格
  1891.     V_SQL VARCHAR2(32767) := TRIM(REPLACE(REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(REPLACE(REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(UPPER(P_SQL),
  1892.                                                                                                                                                  '/\*.*?\*/'),
  1893.                                                                                                                                   '--.*+'),
  1894.                                                                                                                    CHR(10),
  1895.                                                                                                                    ' '),
  1896.                                                                                                            CHR(13),
  1897.                                                                                                            ' '),
  1898.                                                                                                    '[ ]+',
  1899.                                                                                                    ' '),
  1900.                                                                                     '/\*.*?\*/'),
  1901.                                                                      '[ ]+',
  1902.                                                                      ' '),
  1903.                                                       'SELECT',
  1904.                                                       'SELECT '),
  1905.                                               '('));
  1906.     V_COMMAND VARCHAR2(255);
  1907.   BEGIN
  1908.     IF P_SQL IS NULL THEN
  1909.       RETURN '';
  1910.     ELSIF V_SQL IS NULL THEN

  1911.       SELECT CASE
  1912.                WHEN UPPER(P_SQL) LIKE '%BEGIN%' THEN
  1913.                 'PL/SQL EXECUTE'
  1914.                WHEN UPPER(P_SQL) LIKE '%MERGE%' THEN
  1915.                 'MERGE'
  1916.                WHEN (REPLACE(UPPER(P_SQL), ' ', '') LIKE '%CREATEINDEX%' OR
  1917.                     REPLACE(UPPER(P_SQL), ' ', '') LIKE
  1918.                     '%CREATEUNIQUEINDEX%' OR REPLACE(UPPER(P_SQL), ' ', '') LIKE
  1919.                     '%CREATEBITMAPINDEX%' OR
  1920.                     UPPER(P_SQL) LIKE '%CREATE%INDEX%') THEN
  1921.                 'CREATE INDEX'
  1922.                WHEN REPLACE(UPPER(P_SQL), ' ', '') LIKE '%CREATETABLE%' THEN
  1923.                 'CREATE TABLE'
  1924.                WHEN UPPER(P_SQL) LIKE '%INSERT%' THEN
  1925.                 'INSERT'
  1926.                WHEN UPPER(P_SQL) LIKE '%UPDATE%' THEN
  1927.                 'UPDATE'
  1928.                WHEN UPPER(P_SQL) LIKE '%DELETE%' THEN
  1929.                 'DELETE'
  1930.                WHEN UPPER(P_SQL) LIKE '%WITH%' THEN
  1931.                 'WITH'
  1932.                WHEN UPPER(P_SQL) LIKE '%SELECT%' THEN
  1933.                 'SELECT'
  1934.                WHEN REPLACE(UPPER(P_SQL), ' ', '') LIKE '%ALTERTABLE%' THEN
  1935.                 'ALTER TABLE'
  1936.                WHEN REPLACE(UPPER(P_SQL), ' ', '') LIKE '%ALTERINDEX%' THEN
  1937.                 'ALTER INDEX'
  1938.              END
  1939.         INTO V_COMMAND
  1940.         FROM DUAL;

  1941.     ELSE

  1942.       V_COMMAND := SUBSTR(V_SQL, 1, INSTR(V_SQL, ' ', 4) - 1);

  1943.       IF TRIM(V_COMMAND) IN ('ALTER', 'CREATE') THEN
  1944.         V_SQL := REGEXP_REPLACE(REPLACE(REPLACE(V_SQL, 'UNIQUE'), 'BITMAP'),
  1945.                                 '[ ]+',
  1946.                                 ' ');

  1947.         V_COMMAND := SUBSTR(V_SQL, 1, INSTR(V_SQL, ' ', 2, 2) - 1);

  1948.       ELSIF TRIM(V_COMMAND) IN ('DECLARE', 'BEGIN') THEN
  1949.         V_COMMAND := 'PL/SQL EXECUTE';

  1950.       ELSIF TRIM(V_COMMAND) = 'MERGE' THEN
  1951.         V_COMMAND := 'MERGE INTO';
  1952.       ELSIF TRIM(V_COMMAND) IS NULL THEN

  1953.         SELECT CASE
  1954.                  WHEN UPPER(P_SQL) LIKE '%BEGIN%' THEN
  1955.                   'PL/SQL EXECUTE'
  1956.                  WHEN UPPER(P_SQL) LIKE '%MERGE%' THEN
  1957.                   'MERGE'
  1958.                  WHEN (REPLACE(UPPER(P_SQL), ' ', '') LIKE '%CREATEINDEX%' OR
  1959.                       REPLACE(UPPER(P_SQL), ' ', '') LIKE
  1960.                       '%CREATEUNIQUEINDEX%' OR
  1961.                       REPLACE(UPPER(P_SQL), ' ', '') LIKE
  1962.                       '%CREATEBITMAPINDEX%' OR
  1963.                       UPPER(P_SQL) LIKE '%CREATE%INDEX%') THEN
  1964.                   'CREATE INDEX'
  1965.                  WHEN REPLACE(UPPER(P_SQL), ' ', '') LIKE '%CREATETABLE%' THEN
  1966.                   'CREATE TABLE'
  1967.                  WHEN UPPER(P_SQL) LIKE '%INSERT%' THEN
  1968.                   'INSERT'
  1969.                  WHEN UPPER(P_SQL) LIKE '%UPDATE%' THEN
  1970.                   'UPDATE'
  1971.                  WHEN UPPER(P_SQL) LIKE '%DELETE%' THEN
  1972.                   'DELETE'
  1973.                  WHEN UPPER(P_SQL) LIKE '%WITH%' THEN
  1974.                   'WITH'
  1975.                  WHEN UPPER(P_SQL) LIKE '%SELECT%' THEN
  1976.                   'SELECT'
  1977.                  WHEN REPLACE(UPPER(P_SQL), ' ', '') LIKE '%ALTERTABLE%' THEN
  1978.                   'ALTER TABLE'
  1979.                  WHEN REPLACE(UPPER(P_SQL), ' ', '') LIKE '%ALTERINDEX%' THEN
  1980.                   'ALTER INDEX'
  1981.                END
  1982.           INTO V_COMMAND
  1983.           FROM DUAL;

  1984.       END IF;

  1985.     END IF;

  1986.     V_COMMAND := REPLACE(REPLACE(REPLACE(TRIM(V_COMMAND), '(('), '('), '*');

  1987.     IF V_COMMAND NOT IN ('ALTER INDEX',
  1988.                          'ALTER TABLE',
  1989.                          'CREATE INDEX',
  1990.                          'CREATE TABLE',
  1991.                          'DELETE',
  1992.                          'INSERT',
  1993.                          'MERGE INTO',
  1994.                          'PL/SQL EXECUTE',
  1995.                          'SELECT',
  1996.                          'WITH',
  1997.                          'UPDATE') THEN
  1998.       RETURN '';
  1999.     END IF;

  2000.     RETURN V_COMMAND;

  2001.   EXCEPTION
  2002.     WHEN OTHERS THEN
  2003.       NULL;
  2004.   END;

  2005. END PKG_SQL_MONITOR_LHR;
  2006. /



  2007. --------------------------------------------------------------

  2008. EXEC PKG_SQL_MONITOR_LHR.P_GET_PPSQL_PARAMETER;



  2009. -------------------------
  2010. EXEC DBMS_SCHEDULER.DROP_PROGRAM('PROG_SQL_DONE_LHR',TRUE);
  2011. EXEC DBMS_SCHEDULER.DROP_JOB(JOB_NAME => 'JOB_SQL_DONE_LHR' );

  2012. BEGIN
  2013.     DBMS_SCHEDULER.CREATE_PROGRAM(PROGRAM_NAME => 'PROG_SQL_DONE_LHR',
  2014.                                   PROGRAM_ACTION => 'PKG_SQL_MONITOR_LHR.P_SQL_DONE_LHR',
  2015.                                   PROGRAM_TYPE => 'STORED_PROCEDURE',
  2016.                                   ENABLED => TRUE);
  2017. END;
  2018. /
  2019.  


  2020. BEGIN

  2021.     DBMS_SCHEDULER.CREATE_JOB(JOB_NAME => 'JOB_SQL_DONE_LHR',
  2022.                               PROGRAM_NAME => 'PROG_SQL_DONE_LHR',
  2023.                               REPEAT_INTERVAL=> 'FREQ=SECONDLY;INTERVAL=30',
  2024.                               JOB_STYLE => 'LIGHTWEIGHT',
  2025.                               ENABLED=>TRUE,
  2026.                               COMMENTS =>'監控已經執行完畢的SQL語句(V$SQL_MONITOR)');

  2027. END;
  2028. /


  2029. ------------------------
  2030. EXEC DBMS_SCHEDULER.DROP_PROGRAM('PROG_SQL_EPP_LHR',TRUE);
  2031. EXEC DBMS_SCHEDULER.DROP_JOB(JOB_NAME => 'JOB_SQL_EPP_LHR' );
  2032. BEGIN
  2033.     DBMS_SCHEDULER.create_program(program_name => 'PROG_SQL_EPP_LHR',
  2034.                                   program_action => 'PKG_SQL_MONITOR_LHR.P_SQL_EPP_LHR',
  2035.                                   program_type => 'STORED_PROCEDURE',
  2036.                                   enabled => TRUE);
  2037. END;
  2038. /
  2039.             
  2040. BEGIN

  2041.     DBMS_SCHEDULER.CREATE_JOB(JOB_NAME => 'JOB_SQL_EPP_LHR',
  2042.                               PROGRAM_NAME => 'PROG_SQL_EPP_LHR',
  2043.                               REPEAT_INTERVAL=> 'FREQ=SECONDLY;INTERVAL=30',
  2044.                               JOB_STYLE => 'LIGHTWEIGHT',
  2045.                               ENABLED=>TRUE,
  2046.                               COMMENTS =>'記錄效能低下的SQL語句');
  2047. END;
  2048. /

  2049. EXEC DBMS_SCHEDULER.DROP_JOB(JOB_NAME => 'JOB_AUTO_TUNING_SQL_LHR' );
  2050. BEGIN
  2051.   --DBMS_SCHEDULER.drop_job('JOB_AUTO_TUNING_SQL_LHR');
  2052.     DBMS_SCHEDULER.CREATE_JOB(JOB_NAME => 'JOB_AUTO_TUNING_SQL_LHR',
  2053.                               JOB_TYPE => 'STORED_PROCEDURE',
  2054.                               JOB_ACTION => 'PKG_SQL_MONITOR_LHR.P_TUNING_SQL',
  2055.                               repeat_interval => 'FREQ=MINUTELY;INTERVAL=50',
  2056.                               ENABLED => TRUE,
  2057.                               START_DATE => SYSDATE,
  2058.                               COMMENTS => '每隔50分鐘優化一次SQL');
  2059. END;
  2060. /
  2061.  
  2062. EXEC DBMS_SCHEDULER.DROP_JOB(JOB_NAME => 'JOB_SQL_EPP2_LHR' );
  2063. BEGIN
  2064.   --DBMS_SCHEDULER.DROP_JOB('JOB_SQL_EPP2_LHR');
  2065.     DBMS_SCHEDULER.CREATE_JOB(JOB_NAME => 'JOB_SQL_EPP2_LHR',
  2066.                               JOB_TYPE => 'STORED_PROCEDURE',
  2067.                               JOB_ACTION => 'PKG_SQL_MONITOR_LHR.P_SQL_EPP2_LHR',
  2068.                               REPEAT_INTERVAL => 'FREQ=MINUTELY;INTERVAL=28',
  2069.                               ENABLED => TRUE,
  2070.                               START_DATE => SYSDATE,
  2071.                               COMMENTS => 'GV$SQL檢查效能低下的SQL');
  2072. END;
  2073. /
  2074.  

  2075. ------------------------- 檢視 ------------------------------
  2076. CREATE OR REPLACE VIEW VW_SQL_PP_LHR AS
  2077. --記錄所有正在執行中的效能差的SQL語句
  2078. WITH TMPS AS
  2079.  (SELECT WB.INST_ID INST_ID,
  2080.        WB.SID SID,
  2081.        WB.SERIAL#,
  2082.        WB.SPID,
  2083.        WB.OSUSER,
  2084.        WB.USERNAME,
  2085.        WA.PLAN_DEPTH,
  2086.        WA.PLAN_OPERATION PLAN_OPERATION,
  2087.        WA.PLAN_OPTIONS,
  2088.        WA.PLAN_PARTITION_START,
  2089.        WA.PLAN_PARTITION_STOP,
  2090.        WA.STARTS,
  2091.        WA.PLAN_COST,
  2092.        WA.PLAN_CARDINALITY,
  2093.        NVL(WB.SQL_ID, WA.SQL_ID) SQL_ID,
  2094.        WB.SQL_EXEC_START,
  2095.        WA.PX_SERVERS_REQUESTED,
  2096.        WA.PX_SERVERS_ALLOCATED,
  2097.        WA.PX_MAXDOP,
  2098.        WA.ELAPSED_TIME_S ELAPSED_TIME_S,
  2099.        WA.CPU_TIME CPU_TIME,
  2100.        WA.BUFFER_GETS,
  2101.        WA.PHYSICAL_READ_BYTES,
  2102.        WA.PHYSICAL_WRITE_BYTES,
  2103.        WA.USER_IO_WAIT_TIME USER_IO_WAIT_TIME,
  2104.        NVL((SELECT NS.SQL_TEXT
  2105.           FROM GV$SQLAREA NS
  2106.          WHERE NS.SQL_ID = WB.SQL_ID
  2107.            AND NS.INST_ID = WB.INST_ID),WA.SQL_TEXT) SQL_TEXT,
  2108.        WB.LOGON_TIME,
  2109.        WB.SQL_EXEC_ID,
  2110.        WB.EVENT,
  2111.        WB.BLOCKING_INSTANCE BLOCKING_INSTANCE,
  2112.        WB.BLOCKING_SESSION BLOCKING_SESSION,
  2113.        WB.BLOCKING_SESSION_SERIAL# BLOCKING_SESSION_SERIAL#,
  2114.        WB.TADDR,
  2115.        WB.SADDR,
  2116.        WB.LAST_CALL_ET,
  2117.        (WB.SESSION_INFO || '--' || WB.SESSION_TYPE || '--' || WB.MACHINE) SESSION_INFO,
  2118.        (SELECT NS.EXECUTIONS
  2119.           FROM GV$SQLAREA NS
  2120.          WHERE NS.SQL_ID = WB.SQL_ID
  2121.            AND NS.INST_ID = WB.INST_ID) EXECUTIONS,
  2122.        'SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(''' || WB.SQL_ID ||
  2123.        ''',' || WB.SQL_CHILD_NUMBER || ',''advanced''));' SQL_PLAN,
  2124.        WB.ASH_COUNTS,
  2125.        WB.SESSION_STATE
  2126.   FROM (SELECT A.INST_ID,
  2127.                A.SID,
  2128.                A.PLAN_DEPTH,
  2129.                A.PLAN_OPERATION PLAN_OPERATION,
  2130.                A.PLAN_OPTIONS,
  2131.                A.PLAN_PARTITION_START,
  2132.                A.PLAN_PARTITION_STOP,
  2133.                A.STARTS,
  2134.                MAX(A.PLAN_COST) OVER(PARTITION BY A.INST_ID, A.SID, A.KEY, A.SQL_EXEC_ID, A.SQL_ID) AS PLAN_COST,
  2135.                MAX(A.PLAN_CARDINALITY) OVER(PARTITION BY A.INST_ID, A.SID, A.KEY, A.SQL_EXEC_ID, A.SQL_ID) AS PLAN_CARDINALITY,
  2136.                A.SQL_ID,
  2137.                A.SQL_EXEC_START,
  2138.                B.PX_SERVERS_REQUESTED,
  2139.                B.PX_SERVERS_ALLOCATED,
  2140.                B.PX_MAXDOP,
  2141.                (B.ELAPSED_TIME / 1000000) ELAPSED_TIME_S,
  2142.                (B.CPU_TIME / 1000000) CPU_TIME,
  2143.                B.BUFFER_GETS,
  2144.                B.PHYSICAL_READ_BYTES,
  2145.                B.PHYSICAL_WRITE_BYTES,
  2146.                (B.USER_IO_WAIT_TIME / 1000000) USER_IO_WAIT_TIME,
  2147.                B.SQL_TEXT SQL_TEXT,
  2148.                (B.MODULE || '--' || B.ACTION || '--' || B.PROGRAM || '--' ||
  2149.                B.PROCESS_NAME || '--' || B.CLIENT_IDENTIFIER || '--' ||
  2150.                B.CLIENT_INFO || '--' || B.SERVICE_NAME) SESSION_INFO,
  2151.                A.SQL_EXEC_ID
  2152.           FROM GV$SQL_PLAN_MONITOR A, GV$SQL_MONITOR B
  2153.          WHERE A.SID = B.SID
  2154.            AND A.KEY = B.KEY
  2155.            AND A.INST_ID = B.INST_ID
  2156.            AND A.SQL_EXEC_ID = B.SQL_EXEC_ID
  2157.            AND A.STATUS IN ('EXECUTING', 'DONE(ERROR)')
  2158.            AND B.STATUS IN ('EXECUTING', 'DONE(ERROR)')
  2159.            AND B.PROCESS_NAME NOT LIKE 'p%') WA
  2160.  RIGHT OUTER JOIN (SELECT ASH.INST_ID,
  2161.                           ASH.SESSION_ID SID,
  2162.                           ASH.SESSION_SERIAL# SERIAL#,
  2163.                           (SELECT PR.SPID
  2164.                              FROM GV$PROCESS PR
  2165.                             WHERE GVS.PADDR = PR.ADDR
  2166.                               AND PR.INST_ID = ASH.INST_ID) SPID,
  2167.                           ASH.SESSION_TYPE,
  2168.                           ASH.USER_ID,
  2169.                           ASH.SQL_ID,
  2170.                           ASH.SQL_CHILD_NUMBER,
  2171.                           ASH.SQL_OPNAME,
  2172.                           ASH.SQL_EXEC_ID,
  2173.                           NVL(ASH.EVENT, GVS.EVENT) EVENT,
  2174.                           ASH.SESSION_STATE,
  2175.                           ASH.BLOCKING_SESSION,
  2176.                           ASH.BLOCKING_SESSION_SERIAL#,
  2177.                           ASH.BLOCKING_INST_ID BLOCKING_INSTANCE,
  2178.                           ASH.CLIENT_ID,
  2179.                           ASH.MACHINE,
  2180.                           GVS.LAST_CALL_ET,
  2181.                           GVS.TADDR,
  2182.                           GVS.SADDR,
  2183.                           GVS.LOGON_TIME,
  2184.                           GVS.USERNAME,
  2185.                           GVS.OSUSER,
  2186.                           GVS.SQL_EXEC_START,
  2187.                                                     (GVS.MODULE || '--' || GVS.ACTION || '--' || GVS.PROGRAM || '--' ||
  2188.                GVS.PROCESS || '--' || GVS.CLIENT_IDENTIFIER || '--' ||
  2189.                GVS.CLIENT_INFO || '--' || GVS.SERVICE_NAME) SESSION_INFO,
  2190.                           COUNT(*) ASH_COUNTS
  2191.                      FROM GV$ACTIVE_SESSION_HISTORY ASH, GV$SESSION GVS
  2192.                     WHERE ASH.INST_ID = GVS.INST_ID
  2193.                       AND GVS.SQL_ID = ASH.SQL_ID
  2194.                       AND GVS.SQL_EXEC_ID = ASH.SQL_EXEC_ID
  2195.                       AND ASH.SESSION_ID = GVS.SID
  2196.                       AND ASH.SESSION_SERIAL# = GVS.SERIAL#
  2197.                       AND GVS.STATUS = 'ACTIVE'
  2198.                       AND ASH.SQL_ID IS NOT NULL
  2199.                     GROUP BY ASH.INST_ID,
  2200.                              ASH.SESSION_ID,
  2201.                              ASH.SESSION_SERIAL#,
  2202.                              ASH.SESSION_TYPE,
  2203.                              ASH.USER_ID,
  2204.                              ASH.SQL_ID,
  2205.                              ASH.SQL_CHILD_NUMBER,
  2206.                              ASH.SQL_OPNAME,
  2207.                              ASH.SQL_EXEC_ID,
  2208.                              NVL(ASH.EVENT, GVS.EVENT),
  2209.                              ASH.SESSION_STATE,
  2210.                              ASH.BLOCKING_SESSION,
  2211.                              ASH.BLOCKING_SESSION_SERIAL#,
  2212.                              ASH.BLOCKING_INST_ID,
  2213.                              ASH.CLIENT_ID,
  2214.                              ASH.MACHINE,
  2215.                              GVS.LAST_CALL_ET,
  2216.                              GVS.TADDR,
  2217.                              GVS.SADDR,
  2218.                              GVS.LOGON_TIME,
  2219.                              GVS.USERNAME,
  2220.                              GVS.OSUSER,
  2221.                              GVS.PADDR,
  2222.                           (GVS.MODULE || '--' || GVS.ACTION || '--' || GVS.PROGRAM || '--' ||
  2223.                GVS.PROCESS || '--' || GVS.CLIENT_IDENTIFIER || '--' ||
  2224.                GVS.CLIENT_INFO || '--' || GVS.SERVICE_NAME),
  2225.                              GVS.SQL_EXEC_START
  2226.                    HAVING COUNT(*) > 6) WB
  2227.     ON (WB.SID = WA.SID AND WB.INST_ID = WA.INST_ID AND
  2228.        WB.SQL_ID = WA.SQL_ID AND WB.SQL_EXEC_ID = WA.SQL_EXEC_ID)
  2229. )
  2230. ------------------------------------------ 笛卡爾積
  2231. SELECT DISTINCT T.INST_ID,
  2232.                 T.SID,
  2233.                 T.SERIAL#,
  2234.                 T.SPID,
  2235.                 T.OSUSER,
  2236.                 T.USERNAME,
  2237.                 T.EVENT,
  2238.                 T.SESSION_STATE,
  2239.                 T.SQL_TEXT,
  2240.                 T.EXECUTIONS,
  2241.                 T.ELAPSED_TIME_S,
  2242.                 T.CPU_TIME,
  2243.                 T.USER_IO_WAIT_TIME,
  2244.                 T.BUFFER_GETS,
  2245.                 T.PLAN_OPERATION,
  2246.                                 T.STARTS,
  2247.                 T.PLAN_PARTITION_START,
  2248.                 T.PLAN_PARTITION_STOP,
  2249.                 T.PHYSICAL_READ_BYTES,
  2250.                 T.PHYSICAL_WRITE_BYTES,
  2251.                 T.BLOCKING_INSTANCE,
  2252.                 T.BLOCKING_SESSION,
  2253.                                 T.BLOCKING_SESSION_SERIAL#,
  2254.                 T.LAST_CALL_ET,
  2255.                 T.SQL_ID,
  2256.                 T.SQL_EXEC_START,
  2257.                 T.SQL_PLAN,
  2258.                 T.LOGON_TIME,
  2259.                                 T.ASH_COUNTS,
  2260.                 T.SESSION_INFO,
  2261.                 '笛卡爾積【' || COUNT(*) OVER(PARTITION BY T.INST_ID, T.SID, T.SERIAL#, T.SQL_ID) || '】個' MONITOR_TYPES
  2262.   FROM TMPS T
  2263.  WHERE T.PLAN_OPERATION = 'MERGE JOIN'
  2264.    AND T.PLAN_OPTIONS = 'CARTESIAN'
  2265.    AND T.USERNAME NOT IN ('SYS')

  2266. UNION ALL

  2267. ------------------------------------------ SQL執行時間過大
  2268. SELECT T.INST_ID,
  2269.        T.SID,
  2270.        T.SERIAL#,
  2271.        T.SPID,
  2272.        T.OSUSER,
  2273.        T.USERNAME,
  2274.        T.EVENT,
  2275.        T.SESSION_STATE,
  2276.        T.SQL_TEXT,
  2277.        T.EXECUTIONS,
  2278.        T.ELAPSED_TIME_S,
  2279.        T.CPU_TIME,
  2280.        T.USER_IO_WAIT_TIME,
  2281.        T.BUFFER_GETS,
  2282.        T.PLAN_OPERATION,
  2283.        T.STARTS,
  2284.        T.PLAN_PARTITION_START,
  2285.        T.PLAN_PARTITION_STOP,
  2286.        T.PHYSICAL_READ_BYTES,
  2287.        T.PHYSICAL_WRITE_BYTES,
  2288.        T.BLOCKING_INSTANCE,
  2289.        T.BLOCKING_SESSION,
  2290.        T.BLOCKING_SESSION_SERIAL#,
  2291.        T.LAST_CALL_ET,
  2292.        T.SQL_ID,
  2293.        T.SQL_EXEC_START,
  2294.        T.SQL_PLAN,
  2295.        T.LOGON_TIME,
  2296.              T.ASH_COUNTS,
  2297.        T.SESSION_INFO,
  2298.        '執行時間過大' MONITOR_TYPES
  2299.   FROM TMPS T
  2300.  WHERE T.ELAPSED_TIME_S > 10 --5 * 60 * 60
  2301.    AND (nvl(PLAN_DEPTH,1)=1)

  2302. UNION ALL

  2303. ------------------------------------------ 分割槽表全分割槽掃描

  2304. SELECT T.INST_ID,
  2305.        T.SID,
  2306.        T.SERIAL#,
  2307.        T.SPID,
  2308.        T.OSUSER,
  2309.        T.USERNAME,
  2310.        T.EVENT,
  2311.        T.SESSION_STATE,
  2312.        T.SQL_TEXT,
  2313.        T.EXECUTIONS,
  2314.        T.ELAPSED_TIME_S,
  2315.        T.CPU_TIME,
  2316.        T.USER_IO_WAIT_TIME,
  2317.        T.BUFFER_GETS,
  2318.        T.PLAN_OPERATION,
  2319.        T.STARTS,
  2320.        T.PLAN_PARTITION_START,
  2321.        T.PLAN_PARTITION_STOP,
  2322.        T.PHYSICAL_READ_BYTES,
  2323.        T.PHYSICAL_WRITE_BYTES,
  2324.        T.BLOCKING_INSTANCE,
  2325.        T.BLOCKING_SESSION,
  2326.        T.BLOCKING_SESSION_SERIAL#,
  2327.        T.LAST_CALL_ET,
  2328.        T.SQL_ID,
  2329.        T.SQL_EXEC_START,
  2330.        T.SQL_PLAN,
  2331.        T.LOGON_TIME,
  2332.              T.ASH_COUNTS,
  2333.        T.SESSION_INFO,
  2334.        '分割槽表全分割槽掃描' MONITOR_TYPES
  2335.   FROM TMPS T
  2336.  WHERE T.PLAN_OPERATION LIKE 'PARTITION%'
  2337.    AND T.PLAN_OPTIONS = 'ALL'
  2338.   -- AND T.ELAPSED_TIME_S >= 0.5 * 60 * 60

  2339. UNION ALL

  2340. ------------------------------------------ 執行計劃中COST花費超大

  2341. SELECT T.INST_ID,
  2342.        T.SID,
  2343.        T.SERIAL#,
  2344.        T.SPID,
  2345.        T.OSUSER,
  2346.        T.USERNAME,
  2347.        T.EVENT,
  2348.        T.SESSION_STATE,
  2349.        T.SQL_TEXT,
  2350.        T.EXECUTIONS,
  2351.        T.ELAPSED_TIME_S,
  2352.        T.CPU_TIME,
  2353.        T.USER_IO_WAIT_TIME,
  2354.        T.BUFFER_GETS,
  2355.        T.PLAN_OPERATION,
  2356.        T.STARTS,
  2357.        T.PLAN_PARTITION_START,
  2358.        T.PLAN_PARTITION_STOP,
  2359.        T.PHYSICAL_READ_BYTES,
  2360.        T.PHYSICAL_WRITE_BYTES,
  2361.        T.BLOCKING_INSTANCE,
  2362.        T.BLOCKING_SESSION,
  2363.        T.BLOCKING_SESSION_SERIAL#,
  2364.        T.LAST_CALL_ET,
  2365.        T.SQL_ID,
  2366.        T.SQL_EXEC_START,
  2367.        T.SQL_PLAN,
  2368.        T.LOGON_TIME,
  2369.              T.ASH_COUNTS,
  2370.        T.SESSION_INFO,
  2371.        '執行計劃中COST花費超大【' || T.PLAN_COST || '】' MONITOR_TYPES
  2372.   FROM TMPS T
  2373.  WHERE T.PLAN_COST >= 3107523095
  2374.    AND (nvl(PLAN_DEPTH,1)=1)

  2375. UNION ALL
  2376. ------------------------------------------ 執行計劃中預估行數超大

  2377. SELECT T.INST_ID,
  2378.        T.SID,
  2379.        T.SERIAL#,
  2380.        T.SPID,
  2381.        T.OSUSER,
  2382.        T.USERNAME,
  2383.        T.EVENT,
  2384.        T.SESSION_STATE,
  2385.        T.SQL_TEXT,
  2386.        T.EXECUTIONS,
  2387.        T.ELAPSED_TIME_S,
  2388.        T.CPU_TIME,
  2389.        T.USER_IO_WAIT_TIME,
  2390.        T.BUFFER_GETS,
  2391.        T.PLAN_OPERATION,
  2392.        T.STARTS,
  2393.        T.PLAN_PARTITION_START,
  2394.        T.PLAN_PARTITION_STOP,
  2395.        T.PHYSICAL_READ_BYTES,
  2396.        T.PHYSICAL_WRITE_BYTES,
  2397.        T.BLOCKING_INSTANCE,
  2398.        T.BLOCKING_SESSION,
  2399.        T.BLOCKING_SESSION_SERIAL#,
  2400.        T.LAST_CALL_ET,
  2401.        T.SQL_ID,
  2402.        T.SQL_EXEC_START,
  2403.        T.SQL_PLAN,
  2404.        T.LOGON_TIME,
  2405.              T.ASH_COUNTS,
  2406.        T.SESSION_INFO,
  2407.        '執行計劃中預估行數超大【' || T.PLAN_CARDINALITY || '】' MONITOR_TYPES
  2408.   FROM TMPS T
  2409.  WHERE T.PLAN_CARDINALITY > 30748908521460
  2410.    AND (nvl(PLAN_DEPTH,1)=1)

  2411.  UNION ALL
  2412. ------------------------------------------ SQL請求並行數過多


  2413. SELECT T.INST_ID,
  2414.        T.SID,
  2415.        T.SERIAL#,
  2416.        T.SPID,
  2417.        T.OSUSER,
  2418.        T.USERNAME,
  2419.        T.EVENT,
  2420.        T.SESSION_STATE,
  2421.        T.SQL_TEXT,
  2422.        T.EXECUTIONS,
  2423.        T.ELAPSED_TIME_S,
  2424.        T.CPU_TIME,
  2425.        T.USER_IO_WAIT_TIME,
  2426.        T.BUFFER_GETS,
  2427.        T.PLAN_OPERATION,
  2428.        T.STARTS,
  2429.        T.PLAN_PARTITION_START,
  2430.        T.PLAN_PARTITION_STOP,
  2431.        T.PHYSICAL_READ_BYTES,
  2432.        T.PHYSICAL_WRITE_BYTES,
  2433.        T.BLOCKING_INSTANCE,
  2434.        T.BLOCKING_SESSION,
  2435.        T.BLOCKING_SESSION_SERIAL#,
  2436.        T.LAST_CALL_ET,
  2437.        T.SQL_ID,
  2438.        T.SQL_EXEC_START,
  2439.        T.SQL_PLAN,
  2440.        T.LOGON_TIME,
  2441.              T.ASH_COUNTS,
  2442.        T.SESSION_INFO,
  2443.        'SQL請求並行數過多【' || PX_MAXDOP || '】' MONITOR_TYPES
  2444.   FROM TMPS T
  2445.  WHERE T.PX_MAXDOP>=8
  2446.     AND (nvl(PLAN_DEPTH,1)=1)


  2447. UNION ALL
  2448. ------------------------------------------ 系統預估的剩餘執行時間過長

  2449. SELECT T.INST_ID,
  2450.        T.SID,
  2451.        T.SERIAL#,
  2452.        T.SPID,
  2453.        T.OSUSER,
  2454.        T.USERNAME,
  2455.        T.EVENT,
  2456.        T.SESSION_STATE,
  2457.        T.SQL_TEXT,
  2458.        T.EXECUTIONS,
  2459.        T.ELAPSED_TIME_S,
  2460.        T.CPU_TIME,
  2461.        T.USER_IO_WAIT_TIME,
  2462.        T.BUFFER_GETS,
  2463.        T.PLAN_OPERATION,
  2464.        T.STARTS,
  2465.        T.PLAN_PARTITION_START,
  2466.        T.PLAN_PARTITION_STOP,
  2467.        T.PHYSICAL_READ_BYTES,
  2468.        T.PHYSICAL_WRITE_BYTES,
  2469.        T.BLOCKING_INSTANCE,
  2470.        T.BLOCKING_SESSION,
  2471.        T.BLOCKING_SESSION_SERIAL#,
  2472.        T.LAST_CALL_ET,
  2473.        T.SQL_ID,
  2474.        T.SQL_EXEC_START,
  2475.        T.SQL_PLAN,
  2476.        T.LOGON_TIME,
  2477.              T.ASH_COUNTS,
  2478.        T.SESSION_INFO,
  2479.        '系統預估的剩餘執行時間過長【' || ROUND(D.TIME_REMAINING) || '】' MONITOR_TYPES
  2480.   FROM TMPS T, GV$SESSION_LONGOPS D
  2481.  WHERE T.SQL_EXEC_ID = D.SQL_EXEC_ID
  2482.    AND T.SID = D.SID
  2483.    AND T.SERIAL# = D.SERIAL#
  2484.    AND D.TIME_REMAINING > 10
  2485.    AND T.INST_ID = D.INST_ID
  2486.    AND D.TIME_REMAINING >0
  2487.       AND (nvl(PLAN_DEPTH,1)=1)

  2488.  UNION ALL
  2489.  ------------------------------------------ 等待事件異常

  2490. SELECT T.INST_ID,
  2491.        T.SID,
  2492.        T.SERIAL#,
  2493.        T.SPID,
  2494.        T.OSUSER,
  2495.        T.USERNAME,
  2496.        T.EVENT,
  2497.        T.SESSION_STATE,
  2498.        T.SQL_TEXT,
  2499.        T.EXECUTIONS,
  2500.        T.ELAPSED_TIME_S,
  2501.        T.CPU_TIME,
  2502.        T.USER_IO_WAIT_TIME,
  2503.        T.BUFFER_GETS,
  2504.        T.PLAN_OPERATION,
  2505.        T.STARTS,
  2506.        T.PLAN_PARTITION_START,
  2507.        T.PLAN_PARTITION_STOP,
  2508.        T.PHYSICAL_READ_BYTES,
  2509.        T.PHYSICAL_WRITE_BYTES,
  2510.        T.BLOCKING_INSTANCE,
  2511.        T.BLOCKING_SESSION,
  2512.        T.BLOCKING_SESSION_SERIAL#,
  2513.        T.LAST_CALL_ET,
  2514.        T.SQL_ID,
  2515.        T.SQL_EXEC_START,
  2516.        T.SQL_PLAN,
  2517.        T.LOGON_TIME,
  2518.              T.ASH_COUNTS,
  2519.        T.SESSION_INFO,
  2520.        '等待事件異常【' || T.EVENT || '】' MONITOR_TYPES
  2521.   FROM TMPS T
  2522.  WHERE T.EVENT NOT IN ('db file sequential read', 'db file scattered read','db file parallel write','db file parallel read')
  2523.    AND (nvl(PLAN_DEPTH,1)=1)


  2524.  UNION ALL
  2525. ------------------------------------------ TMP表空間佔用過大

  2526. SELECT T.INST_ID,
  2527.        T.SID,
  2528.        T.SERIAL#,
  2529.        T.SPID,
  2530.        T.OSUSER,
  2531.        T.USERNAME,
  2532.        T.EVENT,
  2533.        T.SESSION_STATE,
  2534.        T.SQL_TEXT,
  2535.        T.EXECUTIONS,
  2536.        T.ELAPSED_TIME_S,
  2537.        T.CPU_TIME,
  2538.        T.USER_IO_WAIT_TIME,
  2539.        T.BUFFER_GETS,
  2540.        T.PLAN_OPERATION,
  2541.        T.STARTS,
  2542.        T.PLAN_PARTITION_START,
  2543.        T.PLAN_PARTITION_STOP,
  2544.        T.PHYSICAL_READ_BYTES,
  2545.        T.PHYSICAL_WRITE_BYTES,
  2546.        T.BLOCKING_INSTANCE,
  2547.        T.BLOCKING_SESSION,
  2548.        T.BLOCKING_SESSION_SERIAL#,
  2549.        T.LAST_CALL_ET,
  2550.        T.SQL_ID,
  2551.        T.SQL_EXEC_START,
  2552.        T.SQL_PLAN,
  2553.        T.LOGON_TIME,
  2554.              T.ASH_COUNTS,
  2555.        T.SESSION_INFO,
  2556.        'SQL佔用TMP表空間過大【' || C.BYTES || '】Bytes' MONITOR_TYPES
  2557.   FROM TMPS T,
  2558.        (SELECT A.INST_ID, A.SESSION_ADDR, SUM(A.BLOCKS) * 8 * 1024 BYTES
  2559.           FROM GV$TEMPSEG_USAGE A
  2560.          GROUP BY A.INST_ID, A.SESSION_ADDR) C
  2561.  WHERE C.SESSION_ADDR = T.SADDR
  2562.    AND C.INST_ID = T.INST_ID
  2563.    AND C.BYTES > 10 --50 * 1024 * 1024 * 1024
  2564.    AND (nvl(PLAN_DEPTH,1)=1)

  2565. UNION ALL
  2566. ----------------------------------------- SQL佔用UNDO過大,INACTIVE的會話也可能佔用UNDO,但是這裡只記錄正在執行的SQL語句

  2567. SELECT T.INST_ID,
  2568.        T.SID,
  2569.        T.SERIAL#,
  2570.        T.SPID,
  2571.        T.OSUSER,
  2572.        T.USERNAME,
  2573.        T.EVENT,
  2574.        T.SESSION_STATE,
  2575.        T.SQL_TEXT,
  2576.        T.EXECUTIONS,
  2577.        T.ELAPSED_TIME_S,
  2578.        T.CPU_TIME,
  2579.        T.USER_IO_WAIT_TIME,
  2580.        T.BUFFER_GETS,
  2581.        T.PLAN_OPERATION,
  2582.        T.STARTS,
  2583.        T.PLAN_PARTITION_START,
  2584.        T.PLAN_PARTITION_STOP,
  2585.        T.PHYSICAL_READ_BYTES,
  2586.        T.PHYSICAL_WRITE_BYTES,
  2587.        T.BLOCKING_INSTANCE,
  2588.        T.BLOCKING_SESSION,
  2589.        T.BLOCKING_SESSION_SERIAL#,
  2590.        T.LAST_CALL_ET,
  2591.        T.SQL_ID,
  2592.        T.SQL_EXEC_START,
  2593.        T.SQL_PLAN,
  2594.        T.LOGON_TIME,
  2595.              T.ASH_COUNTS,
  2596.        T.SESSION_INFO,
  2597.        'SQL佔用UNDO過大【' || USED_SIZE_BYTES || '】Bytes' MONITOR_TYPES
  2598.   FROM TMPS T,
  2599.        (SELECT ST.ADDR,
  2600.                ST.INST_ID,
  2601.                (ST.USED_UBLK * 8 * 1024) USED_SIZE_BYTES
  2602.           FROM GV$TRANSACTION ST, V$ROLLNAME R, GV$ROLLSTAT G
  2603.          WHERE ST.XIDUSN = R.USN
  2604.            AND R.USN = G.USN
  2605.            AND G.INST_ID = ST.INST_ID) V1
  2606.  WHERE V1.ADDR = T.TADDR
  2607.    AND T.INST_ID = V1.INST_ID
  2608.    AND USED_SIZE_BYTES > 1024 -- 50 * 1024 * 1024 * 1024

  2609. UNION ALL
  2610. ----------------------------------------- 耗費效能SQL


  2611. SELECT T.INST_ID,
  2612.        T.SID,
  2613.        T.SERIAL#,
  2614.        T.SPID,
  2615.        T.OSUSER,
  2616.        T.USERNAME,
  2617.        T.EVENT,
  2618.        T.SESSION_STATE,
  2619.        T.SQL_TEXT,
  2620.        T.EXECUTIONS,
  2621.        T.ELAPSED_TIME_S,
  2622.        T.CPU_TIME,
  2623.        T.USER_IO_WAIT_TIME,
  2624.        T.BUFFER_GETS,
  2625.        T.PLAN_OPERATION,
  2626.        T.STARTS,
  2627.        T.PLAN_PARTITION_START,
  2628.        T.PLAN_PARTITION_STOP,
  2629.        T.PHYSICAL_READ_BYTES,
  2630.        T.PHYSICAL_WRITE_BYTES,
  2631.        T.BLOCKING_INSTANCE,
  2632.        T.BLOCKING_SESSION,
  2633.        T.BLOCKING_SESSION_SERIAL#,
  2634.        T.LAST_CALL_ET,
  2635.        T.SQL_ID,
  2636.        T.SQL_EXEC_START,
  2637.        T.SQL_PLAN,
  2638.        T.LOGON_TIME,
  2639.              T.ASH_COUNTS,
  2640.        T.SESSION_INFO,
  2641.        'ASH捕獲的次數【' || T.ASH_COUNTS || '】【'||SESSION_STATE||'】' MONITOR_TYPES
  2642.   FROM TMPS T
  2643. WHERE T.ASH_COUNTS>=4
  2644.    AND (nvl(PLAN_DEPTH,1)=1)
  2645.  ORDER BY SQL_EXEC_START DESC
  2646. ;






看看之前的一些結果:




修改後的結果:

該指令碼中有檢視也有表。若想直接查詢資料庫耗費效能的SQL語句,可以直接使用檢視VW_SQL_PP_LHR進行查詢。若想查詢歷史記錄,則可以通過表XB_SQL_MONITOR_PP_LHR來查詢。另外,對於監控中使用的參數列為XB_SQL_PARAMETERS_LHRJOB每次都會從該表中讀取到配置引數的值,該表的查詢結果如下圖所示:

該指令碼中有檢視也有表。若想直接查詢資料庫耗費效能的SQL語句,可以直接使用檢視VW_SQL_PP_LHR進行查詢。若想查詢歷史記錄,則可以通過表XB_SQL_MONITOR_PP_LHR來查詢。另外,對於監控中使用的參數列為XB_SQL_PARAMETERS_LHR。JOB每次都會從該表中讀取到配置引數的值,該表的查詢結果如下圖所示:


下面簡單測試一下上邊的監控指令碼的效果。首先構造一個笛卡爾積連線的SQL,並開啟並行。再構造一個鎖等待的SQL。如下所示的3條SQL語句
① SELECT /*+ monitor parallel(20)*/
 COUNT(*)
  FROM DBA_OBJECTS A,
       DBA_OBJECTS B,
       DBA_OBJECTS C,
       DBA_OBJECTS D;
② UPDATE  /*+ MONITOR */ XB_SQL_MONITOR_LHR T SET T.SQL_TEXT='XXXXXXXXXXXX';
③ UPDATE  /*+ MONITOR */ XB_SQL_MONITOR_LHR T SET T.SQL_TEXT='XXXXXXXXXXXX';
讓以上3條SQL在不同的會話執行,等待大約5分鐘後然後檢視監控效果。
首先查詢檢視VW_SQL_PP_LHR:



從結果可以很明顯的看出,會話(28,583)在等待鎖,而會話(133,437)阻塞了會話(28,583)。對於會話(29,1207)可以看出,由於開了20個並行,所以導致系統CPU不足,所有的會話均在等待CPU資源,而且該會話的SQL語句產生了笛卡爾積、並行數過多、COST花費過大等問題。
對於該JOB的效能,由於作者從多個方面做了優化,所以基本不影響資料庫的執行。下面是該JOB的執行日誌:
SELECT JRD.LOG_ID,
       JRD.JOB_NAME,
       N.JOB_CLASS,
      TO_CHAR(JRD.ACTUAL_START_DATE, 'YYYY-MM-DD HH24:MI:SS') ACTUAL_START_DATE,
       TO_CHAR(JRD.LOG_DATE, 'YYYY-MM-DD HH24:MI:SS') LOG_DATE,
       JRD.STATUS,
       JRD.ERROR#,
       JRD.RUN_DURATION 執行時長,
       JRD.ADDITIONAL_INFO
  FROM DBA_SCHEDULER_JOB_LOG N, DBA_SCHEDULER_JOB_RUN_DETAILS JRD
 WHERE N.LOG_ID = JRD.LOG_ID 
   AND N.JOB_NAME LIKE 'JOB_SQL_%' 
 ORDER BY JRD.LOG_ID DESC;
JOB執行日誌如下圖所示:


查詢監控表XB_SQL_MONITOR_PP_LHR也可獲取相應的監控資訊,這裡不再演示。





About Me

...............................................................................................................................

本文作者:小麥苗,只專注於資料庫的技術,更注重技術的運用

本文在itpubhttp://blog.itpub.net/26736162)、部落格園http://www.cnblogs.com/lhrbest和個人微信公眾號(xiaomaimiaolhr)上有同步更新

本文itpub地址:http://blog.itpub.net/26736162/viewspace-1262559/

本文部落格園地址:http://www.cnblogs.com/lhrbest

本文pdf小麥苗雲盤地址:http://blog.itpub.net/26736162/viewspace-1624453/

● QQ群:230161599     微信群:私聊

聯絡我請加QQ好友(646634621),註明新增緣由

2017-04-01 09:00 ~ 2017-04-03 22:00魔都完成

文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

版權所有,歡迎分享本文,轉載請保留出處

...............................................................................................................................

拿起手機使用微信客戶端掃描下邊的左邊圖片來關注小麥苗的微信公眾號:xiaomaimiaolhr,掃描右邊的二維碼加入小麥苗的QQ群,學習最實用的資料庫技術。

【SQL監控】SQL完全監控的指令碼
DBA筆試面試講解
歡迎與我聯絡

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-1262559/,如需轉載,請註明出處,否則將追究法律責任。

相關文章