1. 程式人生 > >Oracle v$SQLTEXT

Oracle v$SQLTEXT

SQL> desc v$sqltext;

 Name    Null?    Type

 ADDRESS     RAW(8)

 HASH_VALUE     NUMBER

 SQL_ID      VARCHAR2(13)

 COMMAND_TYPE     NUMBER

 PIECE     NUMBER

 SQL_TEXT     VARCHAR2(64)

但v$sqltext中儲存的比較簡單,沒有該語句的統計資訊,比如執行次數等。其中piece表示SQL語句分片之後的順序編號,比如三行的值為0,1,2, 按照順序連線起來就是一個完成的SQL語句。sql_text 表示分片後的sql語句的一部分,注意它的長度只有64 bytes . 欄位HASH_VALUE 和 address 一起唯一標誌一條sql 。

下面的sql,利用v$session視圖裡的 PREV_SQL_ADDR, PREV_HASH_VALUE列,確定某個SID或者spid對應的完整SQL

(1)根據sid獲得sql語句

select /*+ ORDERED */ sql_text

from v$sqltext a

where (a.hash_value,a.ADDRESS) in

( select decode(sql_hash_value,0,PREV_HASH_VALUE,sql_hash_value),

decode(sql_hash_value,0,PREV_SQL_ADDR,SQL_ADDRESS)

from v$session

 b

where b.sid=&SID)

order by piece asc

SQL> select sid,username from v$session;

  SID     USERNAME

-------   --------

36

37 SYS  --要查詢的當前SYS使用者在執行什麼語句

38

40

26 rows selected.

SQL> select /*+ ORDERED */ sql_text

from v$sqltext a

where (a.hash_value,a.ADDRESS) in

( select decode(sql_hash_value,0,PREV_HASH_VALUE,sql_hash_value),

decode(sql_hash_value,0,PREV_SQL_ADDR,SQL_ADDRESS)

from v$session b

where b.sid=&SID)

order by piece asc;

Enter value for sid: 37

old   7: where b.sid=&SID)

new   7: where b.sid=37)

SQL_TEXT

---------

select /*+ ORDERED */ sql_text from v$sqltext a where (a.hash_va

lue,a.ADDRESS) in ( select decode(sql_hash_value,0,PREV_HASH_VAL

UE,sql_hash_value), decode(sql_hash_value,0,PREV_SQL_ADDR,SQL_AD

DRESS) from v$session b where b.sid=37) order by piece asc

(2)根據作業系統pid獲得sql語句,這個程序是oracle程序並且是LOCAL=NO

select /*+ ORDERED */ sql_text

from v$sqltext a

where (a.hash_value,a.ADDRESS) in

( select decode(sql_hash_value,0,PREV_HASH_VALUE,sql_hash_value),

decode(sql_hash_value,0,PREV_SQL_ADDR,SQL_ADDRESS)

from v$session b

where b.paddr = (select addr

from v$process c

where c.spid = '&spid'))

order by piece asc

以作業系統的process ID為引數。(這個可以通過top命令找出LOCAL=NO的oracle消耗CPU較高的程序PID)

下面是實際生產環境的例子,業務反饋CPU使用率過高,登入到主機使用TOP檢視,可以看到使用top命令檢視到PID為3193,31433,31690這幾個oracle程序使用CPU達到了百分之百了,這裡可以使用上面的v$sqltext檢視結合v$session和v$process來定位正在執行的佔用cpu較高的SQL語句。

[email protected]:~> top
top - 15:50:18 up 1041 days, 23:28,  6 users,  load average: 11.93, 8.62, 14.23
Tasks: 509 total,  18 running, 490 sleeping,   0 stopped,   1 zombie
Cpu(s): 89.2%us,  1.2%sy,  0.2%ni,  9.1%id,  0.4%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:     23641M total,    22667M used,      973M free,     1534M buffers
Swap:    25583M total,       80M used,    25503M free,    17946M cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                  
31493 oracle    20   0 16.3g 245m 241m R  100  1.0   3:18.24 oracle                                    
31433 oracle    20   0 16.3g 246m 242m R  100  1.0   3:24.92 oracle                                    
31690 oracle    20   0 16.3g 250m 246m R  100  1.1   3:11.16 oracle        
                           
31609 oracle    20   0 16.3g 245m 241m R   99  1.0   3:15.02 oracle                                    
 9645 oracle    20   0 16.3g 320m 316m R   99  1.4   3:33.72 oracle                                    
11860 oracle    20   0 16.3g 374m 370m R   98  1.6   3:44.13 oracle                                    
31553 oracle    20   0 16.3g 243m 239m R   98  1.0   3:22.38 oracle                                    
31222 oracle    20   0 16.3g 247m 243m R   97  1.0   3:25.97 oracle                                    
  456 oracle    20   0 16.3g 321m 316m R   97  1.4   3:36.43 oracle                                    
31127 oracle    20   0 16.3g 247m 243m R   95  1.0   3:29.34 oracle                                    
 5205 oracle    20   0 2958m  79m  18m R   75  0.3   0:02.25 java                                      
 5173 oracle    20   0 98684  21m 7896 S    6  0.1   0:00.18 perl                                      
11357 root      20   0  672m 9556 6232 S    3  0.0  27:11.31 InforGuardMa                              
12016 root      30  10  470m 8256 3212 S    3  0.0   4909:32 hpi_program                               
26379 root      20   0  3876  468  372 S    1  0.0   4711:34 guard-userspace                           
 5162 oracle    20   0  9072 1532  872 R    1  0.0   0:00.03 top                                       
11425 root      20   0  223m  60m 6440 S    1  0.3   1299:53 java                                      
19249 oracle    -2   0 16.3g  15m  14m S    1  0.1   1473:10 oracle                                    
30529 oracle    20   0  9072 1544  876 S    1  0.0   0:01.42 top                                       

[email protected]:~> sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Aug 28 15:50:34 2018

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, Oracle Label Security, OLAP,
Data Mining, Oracle Database Vault and Real Application Testing options

SQL> select /*+ ORDERED */ sql_text 
from v$sqltext a
where (a.hash_value,a.ADDRESS) in
( select decode(sql_hash_value,0,PREV_HASH_VALUE,sql_hash_value),
decode(sql_hash_value,0,PREV_SQL_ADDR,SQL_ADDRESS)
from v$session b
where b.paddr = (select addr 
from v$process c
where c.spid = '&spid'))
order by piece asc  2    3    4    5    6    7    8    9   10  
 11  ;
Enter value for spid: 31493   ---輸入pid找出對應oracle程序執行sql語句
old   9: where c.spid = '&spid'))
new   9: where c.spid = '31493'))

SQL_TEXT    ---這裡定位出消耗CPU較高的SQL語句了,就是這條SQL語句導致CPU使用率達到百分之百,後面就要對這條SQL語句進行分析優化了。
----------------------------------------------------------------
select *  from (select row_.*, rownum rownum_  from (
        SELECT        a.*, b.GROUPCUSTOMERNAME,b.GROUP
CUSTOMERID,m.MEMBERGROUPID           FROM AAA_WSOPGROUPSUBSREG
ISTERLOG a           JOIN AAA_WSOPGROUPCUSTOMER b    ON a.GRO
UPCUSTOMERKEY=b.GROUPCUSTOMERKEY    LEFT JOIN AAA_WSOPMEMBERGROU
P m    ON a.MEMBERGROUPKEY=m.MEMBERGROUPKEY    WHERE EXISTS (
       SELECT :"SYS_B_0" FROM (          SELECT c.GROUP
CUSTOMERKEY          FROM AAA_WSOPCUSTOMERORGREL c
      WHERE EXISTS (               SELECT :"SYS_B_1" FROM (
              SELECT ORGID, PARENTID
   FROM T_BME_ORGANIZATION             START WITH ORGI

SQL_TEXT
----------------------------------------------------------------
D = :1                 CONNECT BY PRIOR ORGID = PARENTID
          ) d            WHERE c.ORGID = d.ORGID
         )          ) e      WHERE a.REGISTERTYPE != :"SYS_
B_2" AND a.STATUS != :"SYS_B_3"     AND a.GROUPCUSTOMERKEY   = e
.GROUPCUSTOMERKEY

               )        order by a.GROUPCUSTOMER
KEY,a.USERNAME          )row_ where rownum <= :"SYS_B_4" ) whe
re rownum_ >= :"SYS_B_5"

20 rows selected.

上面例子還結合v$process這張檢視,請參考我v$process這篇部落格:Oracle v$PROCESS