[20230210]建立完善swcnm.sql指令碼.txt

lfree發表於2023-02-15

[20230210]建立完善swcnm.sql指令碼.txt

--//遇到asm例子出現阻塞的情況.正常使用tpt的swc指令碼無法查詢.因為asm例項在nomount狀態.with subquery報錯.
--//自己建立一個新的查詢指令碼.使用v$instance,v$thread作為子查詢的子表.

SYS@test> startup nomount
ORACLE instance started.

Total System Global Area  805306368 bytes
Fixed Size                  8924064 bytes
Variable Size             297796704 bytes
Database Buffers          490733568 bytes
Redo Buffers                7852032 bytes

SYS@test> set verify off
SYS@test> @ swc sid||','||SERIAL#||',@'||inst_id||','||event 1=1
                              THEN ' ['||NVL((SELECT class FROM bclass WHERE r = s.p3),'undo @bclass '||s.p3)||']' ELSE null END,'ON CPU')
                                                                *
ERROR at line 22:
ORA-01219: database or pluggable database not open: queries allowed on fixed tables or views only
--//主要裡面使用with子查詢,使用別名不對.
--//改寫如下:

$ cat swcnm.sql
prompt  @ swcnm sid||','||SERIAL#||',@'||inst_id||','||event 1=1
col path format a180
WITH
    v$instance AS (SELECT /*+ INLINE */ class, ROWNUM r from v$waitstat)
  , v$thread AS (
    SELECT
        REPLACE(SYS_CONNECT_BY_PATH(&1, '->'), '->', ' -> ')
               ||CASE WHEN CONNECT_BY_ISLEAF = 1 AND ses.blocking_session IS NOT NULL
                      THEN ' -> [idle blocker '||ses.blocking_instance||','||ses.blocking_session||' ('||ses.program||')]' ELSE NULL   
                 END path
    FROM (
        SELECT
            s.*
          , CASE WHEN s.type = 'BACKGROUND' AND s.program LIKE '%(DBW%)' THEN
              '(DBWn)'
            WHEN s.type = 'BACKGROUND' OR REGEXP_LIKE(s.program, '.*\([PJ]\d+\)') THEN
              REGEXP_REPLACE(SUBSTR(s.program,INSTR(s.program,'(')), '\d', 'n')
            ELSE
                '('||REGEXP_REPLACE(REGEXP_REPLACE(s.program, '(.*)@(.*)(\(.*\))', '\1'), '\d', 'n')||')'
            END || ' ' program2
          , NVL(s.event||CASE WHEN event like 'enq%' AND state = 'WAITING'
                              THEN ' [mode='||BITAND(p1, POWER(2,14)-1)||']'
                              WHEN s.event IN (SELECT name FROM v$event_name WHERE parameter3 = 'class#')
                              THEN ' ['||NVL((SELECT class FROM v$instance WHERE r = s.p3),'undo @bclass '||s.p3)||']' ELSE null END,'ON CPU')
                       || ' ' event2
          , TO_CHAR(CASE WHEN state = 'WAITING' THEN p1 ELSE null END, '0XXXXXXXXXXXXXXX') p1hex
          , TO_CHAR(CASE WHEN state = 'WAITING' THEN p2 ELSE null END, '0XXXXXXXXXXXXXXX') p2hex
          , TO_CHAR(CASE WHEN state = 'WAITING' THEN p3 ELSE null END, '0XXXXXXXXXXXXXXX') p3hex
        FROM
            gv$session s
    ) ses
    CONNECT BY NOCYCLE
        (    PRIOR ses.blocking_session  = ses.sid
         AND PRIOR ses.blocking_instance = ses.inst_id
        )
    START WITH (ses.state='WAITING' AND ses.wait_class!='Idle') AND &2
)
SELECT
    COUNT(*) sessions
  , path
FROM
    v$thread
GROUP BY
    path
ORDER BY
    sessions DESC
/

--//例子:
@ swcnm sid||','||SERIAL#||',@'||inst_id||','||event 1=1

--//簡單測試:
--//先在nomunt下測試:
SYS@test> @ swcnm sid||','||SERIAL#||',@'||inst_id||','||event 1=1
@ swcnm sid||','||SERIAL#||',@'||inst_id||','||event 1=1
no rows selected

--//ok沒有問題,進入open模式,繼續測試:
SCOTT@test01p> @ ver1
PORT_STRING                    VERSION        BANNER                                                                               CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0           12.2.0.1.0     Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production              0

--//session 1:
SCOTT@test01p> @ spid
       SID    SERIAL# PROCESS                  SERVER    SPID                     PID  P_SERIAL# C50
---------- ---------- ------------------------ --------- -------------------- ------- ---------- --------------------------------------------------
       169      61475 9544:1400                DEDICATED 12952                     26          4 alter system kill session '169,61475' immediate;

SCOTT@test01p> create table t ( a number);
Table created.

SCOTT@test01p> insert into t values (1);
1 row created.

SCOTT@test01p> commit ;
Commit complete.

SCOTT@test01p> update t set a=1;
1 row updated.

--//session 2:
SCOTT@test01p> @ spid
       SID    SERIAL# PROCESS                  SERVER    SPID                     PID  P_SERIAL# C50
---------- ---------- ------------------------ --------- -------------------- ------- ---------- --------------------------------------------------
       178      25684 8384:7108                DEDICATED 3004                      42          7 alter system kill session '178,25684' immediate;

SCOTT@test01p> update t set a=2;

--//掛起!!
--//session 1:
SCOTT@test01p> @ swcnm sid||','||SERIAL#||',@'||inst_id||','||event 1=1
@ swcnm sid||','||SERIAL#||',@'||inst_id||','||event 1=1
  SESSIONS PATH
---------- -----------------------------------------------------------------------------------------------
         1  -> 178,25684,@1,enq: TX - row lock contention -> 169,61475,@1,SQL*Net message to client
         1  -> 178,25684,@1,enq: TX - row lock contention



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

相關文章