1. 程式人生 > 實用技巧 >檢視sql執行進度

檢視sql執行進度

動態效能檢視v$session_longops

v$session_longops

This view displays the status of various operations that run for longer than 6 seconds (in absolute time). These operations currently include many backup and recovery functions, statistics gathering, and query execution, and more operations are added for every Oracle release.

To monitor query execution progress, you must be using the cost-based optimizer and you must:

  • Set theTIMED_STATISTICSorSQL_TRACEparameter totrue

  • Gather statistics for your objects with theANALYZEstatement or theDBMS_STATSpackage

該檢視記錄了執行時間長於6秒的某個操作(這些操作可能是備份,恢復,收集統計資訊,Hash Join,Sort ,Nested loop,Table Scan, Index Scan 等等),這個檢視通常用來分析SQL執行緩慢的原因,配合V$SESSION檢視。

1.必須將初始化引數 timed_statistics設定為true或者開啟sql_trace

2.必須用ANALYZE或者DBMS_STATS對物件收集過統計資訊


ColumnDatatypeDescription
SID NUMBER Session identifier
SERIAL# NUMBER Session serial number
OPNAME VARCHAR2(64) Brief description of the operation
TARGET VARCHAR2(64) The object on which the operation is carried out
TARGET_DESC VARCHAR2(32) Description of the target
SOFAR NUMBER The units of work done so far
TOTALWORK NUMBER The total units of work
UNITS VARCHAR2(32) The units of measurement
START_TIME DATE The starting time of operation
LAST_UPDATE_TIME DATE Time when statistics last updated
TIMESTAMP DATE Timestamp
TIME_REMAINING NUMBER Estimate (in seconds) of time remaining for the operation to complete
ELAPSED_SECONDS NUMBER The number of elapsed seconds from the start of operations
CONTEXT NUMBER Context
MESSAGE VARCHAR2(512) Statistics summary message
USERNAME VARCHAR2(30) User ID of the user performing the operation
SQL_ADDRESS RAW(4 | 8) Used with the value of theSQL_HASH_VALUEcolumn to identify the SQL statement associated with the operation
SQL_HASH_VALUE NUMBER Used with the value of theSQL_ADDRESScolumn to identify the SQL statement associated with the operation
SQL_ID VARCHAR2(13) SQL identifier of the SQL statement associated with the operation
QCSID NUMBER Session identifier of the parallel coordinator      

 SID                    Session標識              
 SERIAL#                Session串號              
 OPNAME                 操作簡要說明              
 TARGET                 操作執行所在的物件          
 TARGET_DESC            目標物件說明            
 SOFAR                  至今為止完成的工作量       
 TOTALWORK              總工作量             
 UNITS                  工作量單位               
 START_TIME             操作開始時間            
 LAST_UPDATE_TIME       統計項最後更新時間       
 TIMESTAMP              操作的時間戳              
 TIME_REMAINING         預計完成操作的剩餘時間(秒)   
 ELAPSED_SECONDS        從操作開始總花費時間(秒)   
 CONTEXT                前後關係             
 MESSAGE                統計項的完整描述         
 USERNAME               執行操作的使用者ID          
 SQL_ADDRESS            關聯v$sql                
 SQL_HASH_VALUE         關聯v$sql             
 SQL_ID                 關聯v$sql       
 QCSID           主要是並行查詢一起使用 

要理解的就是:比如某個SQL語句執行時間比較長,但是每個操作都沒有超過6秒鐘,那麼你在V$SESSION_LONGOPS這個檢視中就無法查詢到該資訊。

  • 相關操作語句:

    

SELECT USERNAME,
       SID,
       OPNAME,
       ROUND(SOFAR * 100 / TOTALWORK, 0) || '%' AS PROGRESS,
       TIME_REMAINING,
       SQL_TEXT
  FROM V$SESSION_LONGOPS, V$SQL
 WHERE TIME_REMAINING <> 0
   AND SQL_ADDRESS = ADDRESS
   AND SQL_HASH_VALUE = HASH_VALUE;

select * from v$session a,v$session_longops b where a.SID = b.SID and a.SERIAL# = b.SERIAL# ;  
select * from v$sql a,v$session_longops b where a.SQL_ID = b.SQL_ID ;  
select * from v$sqlarea a,v$session_longops b where a.HASH_VALUE = b.SQL_HASH_VALUE ;  

如果是在RAC,改成GV$SESSION_LONGOPS這個檢視

常用:

select a.sofar,a.totalwork ,t.*from v$session_longops a where sofar<>totalwork --越接近說明馬上執行結束

原文地址:https://www.cnblogs.com/willspring/p/5670902.html