[20220129]完善tpt ash ash_index_helperx指令碼.txt

lfree發表於2022-02-18

[20220129]完善tpt ash ash_index_helperx指令碼.txt

--//tpt scripts包的ash目錄下有兩個指令碼ash_index_helperx.sql,ash_index_helper.sql指令碼用來定位表來提供索引建立幫助。
--//不過我下載執行包如下錯誤。

SYS@127.0.0.1:9105/dbcn> @ash/ash_index_helper % portal_his.EMR_WJZ  &day

-- Santa's Little (Index) Helper BETA v0.5 - by Tanel Poder ( )
AND sq.sql_plan_hash_value(+) = plan.plan_hash_value
                              *
ERROR at line 88:
ORA-01417: a table may be outer joined to at most one other table

--//我僅僅修改刪除外連線的加號+在118行處。修改如下:
--//ash_index_helperx指令碼修改類似。
--AND sq.sql_id(+) = plan.sql_id
--AND sq.sql_child_number(+) = plan.child_number
--AND sq.sql_plan_line_id(+) = plan.id
--AND sq.sql_plan_hash_value(+) = plan.plan_hash_value
AND sq.sql_id = plan.sql_id
AND sq.sql_child_number = plan.child_number
AND sq.sql_plan_line_id = plan.id
AND sq.sql_plan_hash_value = plan.plan_hash_value
--//使用中還發現一個小問題aindex_predicates顯示寬度僅僅60,有點窄,如果加寬也不合適,而且後面使用truncate,導致顯示資訊被
--//截斷,無法清楚的知道在那個欄位上建立索引,更改一下,在加入引數&5, &6 ,增加一些靈活性.增加如下程式碼:

COL v_filter    new_value v_filter
COL v_truncate  new_value v_truncate
set term off
select decode('&&5',NULL,'1=1','0','1=1',q'aindex_operation not like '%INDEX%'') v_filter , decode('&&6',NULL,'truncate','0','truncate','wrap') v_truncate from dual;
set term on

--//說明:輸入'',0 使用原來模式,輸入1對於引數5,不查詢掉索引存取模式,對於引數6顯示PREDICATES使用wrap.

--//例子如下:
@ash/ash_index_helper % owner.table_name &day '' '' --//原來的模式.
@ash/ash_index_helper % owner.table_name &day 0  0 --//原來的模式.
@ash/ash_index_helper % owner.table_name &day 1  1  --//過濾掉索引存取模式以及顯示PREDICATES使用truncate
--//注意一點過濾索引存取模式,也過濾INDEX RANGE SCAN 過濾,可能建立的索引不合適,也要引起注意.

--//另外可以這樣輸入:
@ash/ash_index_helper % owner.% &day 0 0
--//這樣可以定位該schema模式下最有問題的sql語句.

--//注意指令碼中的^G字元要使用ctrl+v ctrl+g輸入.
$ cat -vs ash_index_helper.sql
-- Copyright 2018 Tanel Poder. All rights reserved. More info at
-- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.

SET LINESIZE 999 PAGESIZE 5000 TRIMOUT ON TRIMSPOOL ON

--define 5 = decode('&&5',NULL,q'^Gaindex_operation like '%INDEX%'^G','1=1')
--define 6 = decode('&&6',NULL,'','truncate')

COL v_filter    new_value v_filter
COL v_truncate  new_value v_truncate
set term off
select decode('&&5',NULL,'1=1','0','1=1',q'^Gaindex_operation not like '%INDEX%'^G') v_filter , decode('&&6',NULL,'truncate','0','truncate','wrap') v_truncate from dual;
set term on

COL accessed_table     HEAD Accessed_Table FOR a20
COL aindex_operation  HEAD Plan_Operation FOR a60

COL aindex_predicates HEAD PREDICATES     FOR a60  &&v_truncate
--COL aindex_predicates HEAD PREDICATES     FOR a60 truncate
COL obj_alias_qbc_name FOR a40
COL options   FOR a30

COL AAS                 FOR 9999.9
COL cpu_pct  HEAD CPU  FOR A5
COL wait_pct HEAD WAIT FOR A5

COL ela_sec_exec HEAD "ELA_SEC/EXEC" FOR 9999990.999

COL aindex_plan_hash_value HEAD PLAN_HASH_VALUE   PRINT
COL aindex_sql_id          HEAD SQL_ID          NOPRINT
COL aindex_sql_child       HEAD "CHILD"          PRINT
COL aindex_sample_time     HEAD SAMPLE_HOUR
COL projection FOR A520

COL pct_child HEAD "Activity %" FOR A8
COL pct_child_vis HEAD "Visual" FOR A12

COL aindex_id        HEAD "ID" FOR 9999
COL aindex_parent_id HEAD "PID"  FOR 9999
col module1 format a20

PROMPT
PROMPT -- Santa's Little (Index) Helper BETA v0.5 - by Tanel Poder ( )

WITH
tab AS (SELECT /*+ NO_MERGE */ owner, table_name, num_rows
        FROM dba_tables
        WHERE UPPER(table_name) LIKE
                UPPER(CASE
                  WHEN INSTR('&2','.') > 0 THEN
                      SUBSTR('&2',INSTR('&2','.')+1)
                  ELSE
                      '&2'
                  END
                     ) ESCAPE '\'
        AND owner LIKE
            CASE WHEN INSTR('&2','.') > 0 THEN
              UPPER(SUBSTR('&2',1,INSTR('&2','.')-1))
            ELSE
              user
            END ESCAPE '\'
),
ind AS (SELECT /*+ NO_MERGE */ owner, index_name, table_owner, table_name
        FROM dba_indexes
        WHERE (table_owner, table_name) IN (SELECT owner, table_name FROM tab)),
sample_times AS (
    select * from dual
),
sq AS (
SELECT
    count(*) samples
  , ash.sql_id
  , ash.sql_child_number
  , ash.sql_plan_hash_value
  , NVL(ash.sql_plan_line_id,1) sql_plan_line_id -- this is because simple "planless" operations like single-row insert
  , ash.sql_plan_operation
  , ash.sql_plan_options
  , ash.session_state
  , ash.wait_class
  , ash.event
  , lower(ash.module) module1
FROM
    v$active_session_history ash
WHERE
    1=1
AND ash.sql_plan_operation IN ('TABLE ACCESS', 'INDEX')
AND ash.sql_id LIKE '&1'
AND ash.sample_time BETWEEN &3 AND &4
GROUP BY
    ash.sql_id
  , ash.sql_child_number
  , ash.sql_plan_hash_value
  , NVL(ash.sql_plan_line_id,1)
  , ash.sql_plan_operation
  , ash.sql_plan_options
  , ash.session_state
  , ash.wait_class
  , ash.event
  , lower(ash.module)
),
ash_and_plan AS (
SELECT
    plan.sql_id
  , plan.child_number
  , plan.plan_hash_value
  , sq.samples seconds
  , LPAD(TO_CHAR(ROUND(RATIO_TO_REPORT(sq.samples) OVER (PARTITION BY sq.sql_id, sq.sql_plan_hash_value) * 100, 1), 999.9)||' %',8) pct_child
  , '|'||RPAD( NVL( LPAD('#', ROUND(RATIO_TO_REPORT(sq.samples) OVER (PARTITION BY sq.sql_id, sq.sql_plan_hash_value) * 10), '#'), ' '), 10,' ')||'|' pct_child_vis
--, LPAD(plan.id,4)||CASE WHEN parent_id IS NULL THEN '    ' ELSE ' <- ' END||LPAD(plan.parent_id,4) aindex_plan_id
  , plan.id aindex_id
  , plan.parent_id aindex_parent_id
  , plan.operation ||' '|| plan.options || NVL2(plan.object_name, ' ['||plan.object_owner||'.'||plan.object_name ||']', null) aindex_operation
  , plan.object_owner
  , plan.object_name
  , plan.object_type
  , plan.cardinality
  , stat.executions
  , stat.elapsed_time
  , sq.session_state
  , sq.wait_class
  , sq.event
  , sq.module1
  , plan.object_alias || CASE WHEN plan.qblock_name IS NOT NULL THEN ' ['|| plan.qblock_name || ']' END obj_alias_qbc_name
  , CASE WHEN plan.access_predicates IS NOT NULL THEN '[A:] '|| SUBSTR(plan.access_predicates,1,1994) END || CASE WHEN plan.filter_predicates IS NOT NULL THEN ' [F:] ' || SUBSTR(plan.filter_predicates,1,1994) END aindex_predicates
--  , plan.projection
FROM
    v$sql_plan plan
  , v$sqlstats_plan_hash stat
  , sq
WHERE
    1=1
--AND sq.sql_id(+) = plan.sql_id
--AND sq.sql_child_number(+) = plan.child_number
--AND sq.sql_plan_line_id(+) = plan.id
--AND sq.sql_plan_hash_value(+) = plan.plan_hash_value
AND sq.sql_id = plan.sql_id
AND sq.sql_child_number = plan.child_number
AND sq.sql_plan_line_id = plan.id
AND sq.sql_plan_hash_value = plan.plan_hash_value
--
AND sq.sql_id(+) = stat.sql_id
AND sq.sql_plan_hash_value(+) = stat.plan_hash_value
--AND sq.sql_plan_hash_value = stat.plan_hash_value
AND stat.sql_id = plan.sql_id
AND stat.plan_hash_value = plan.plan_hash_value
--
AND plan.sql_id LIKE '&1'
----
)
SELECT * FROM (
    SELECT
        SUM(ap.seconds) seconds
      , ROUND(SUM(ap.seconds) / ((CAST(&4 AS DATE) - CAST(&3 AS DATE)) * 86400), 1) AAS
      , LPAD(TO_CHAR(ROUND(SUM(CASE WHEN ap.session_state = 'ON CPU'  THEN ap.seconds ELSE 0 END) / SUM(ap.seconds) * 100))||'%',4) cpu_pct
      , LPAD(TO_CHAR(ROUND(SUM(CASE WHEN ap.session_state = 'WAITING' THEN ap.seconds ELSE 0 END) / SUM(ap.seconds) * 100))||'%',4) wait_pct
      --, SUM(CASE WHEN ap.wait_class = 'User I/O'   THEN ap.seconds ELSE 0 END) iowait_sec
      --, t.owner||'.'||table_name accessed_table
      , table_name accessed_table
      , ap.aindex_operation
      , ap.cardinality plan_card
      , t.num_rows table_rows
      , ap.cardinality / NULLIF(t.num_rows,0) * 100 filter_pct
      , ap.executions sql_execs
      , ROUND(ap.elapsed_time / NULLIF(ap.executions,0) / 1000000,3) ela_sec_exec
      , ap.aindex_predicates
--    , COUNT(DISTINCT ap.sql_id) dist_sqlids
--    , COUNT(DISTINCT ap.plan_hash_value) dist_plans
      , MIN(ap.sql_id) sql_id
      , ap.module1
--    , MAX(ap.sql_id)
--      , ap.projection
    FROM
        ash_and_plan ap
      , (SELECT tab.*, 'TABLE' object_type, tab.owner object_owner, tab.table_name object_name FROM tab
         UNION ALL
         SELECT tab.*, 'INDEX', ind.owner object_owner, ind.index_name object_name
         FROM tab, ind
         WHERE tab.owner = ind.table_owner AND tab.table_name = ind.table_name
        ) t
    WHERE
       ap.object_owner = t.object_owner AND ap.object_name = t.object_name AND SUBSTR(ap.object_type,1,5) = t.object_type
        AND &&v_filter
    AND ap.seconds > 0
    GROUP BY
        t.owner
      , t.table_name
      , ap.aindex_operation
      , t.num_rows
      , ap.cardinality
      , ap.executions
      , ap.elapsed_time
      , ap.aindex_predicates
      , ap.module1
--      , ap.projection
    ORDER BY
        seconds DESC
)
WHERE rownum <= 50
/

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

相關文章