用v$session_longops监视大型操作
作者:Lunar
我们可以使用v$session_longops视图用于监视任何运行时间很长的操作(DDL和DML)。,使用它之前必须满足两个条件:
1. 设置TIMED_STATISTICS 或者SQL_TRACE,
2. 因为这个功能只能应用于基于成本的优化器,所以必须存在统计数据
与之相同的功能也可以使用Oracle 9i提供的DBMS_APPLICATION_INFO.SET_SESSION_LONGOPS来完成。
下面的脚本将显示一个状态信息,说明了运行时间很长的DDL操作已经使用的时间。
Select sid, message
From v$session_longops
Where sid = &sid
order by start_time;
实例1——监视创建大索引(DDL)
| Microsoft Windows 2000 [Version 5.00.2195] (C) 版权所有 1985-2000 Microsoft Corp.
C:/>sqlplus lunar/lunar@lunar
SQL*Plus: Release 9.2.0.1.0 - Production on 星期二 6月 15 16:32:25 2004
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
连接到: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production With the OLAP and Oracle Data Mining options JServer Release 9.2.0.1.0 - Production
SQL> select distinct sid from v$mystat;
SID ---------- 9
SQL> set time on 17:16:51 SQL> create index idx_t on t(OBJECT_ID);
索引已创建。
已用时间: 00: 03: 08.00 17:20:19 SQL> |
在另一个session中:
| SQL> conn lunar/lunar@lunar 已连接。 SQL> analyze table t ESTIMATE STATISTICS sample 1 PERCENT;
表已分析。
SQL> set linesize 1000 SQL> column message format a70 SQL> Select sid, message 2 From v$session_longops 3 Where sid = 9 4 order by start_time 5 /
SID MESSAGE ---------- -------------------------------------------------------- 9 Table Scan: LUNAR.T: 19537 out of 19537 Blocks done 9 SQL Execution: : 1881 out of 1881 units done 9 Table Scan: LUNAR.T: 39173 out of 39173 Blocks done 9 Sort/Merge: : 9976 out of 9976 Blocks done 9 Sort Output: : 9482 out of 9482 Blocks done
已用时间: 00: 00: 00.00 SQL> |
实例2——监视全表扫描
SELECT sid, serial#, opname,
TO_CHAR(start_time,'HH24:MI:SS') AS start_time,
(sofar/totalwork)*100 AS PERCENT_COMPLETE,
ELAPSED_SECONDS
FROM v$session_longops;
| SQL> SHOW USER USER 为"LUNAR" SQL> SQL> select avg_space from user_tables 2 where table_name='T';
AVG_SPACE ---------- 867 说明有统计数据
SQL> show parameter timed_stat
NAME TYPE VALUE ------------------------------------ ----------- -------------- timed_statistics boolean TRUE SQL> select * from t;
已选择3209728行。
已用时间: 00: 05: 38.06
Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1881 Card=1605276 By tes=120395700)
1 0 TABLE ACCESS (FULL) OF 'T' (Cost=1881 Card=1605276 Bytes=1 20395700)
Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 250492 consistent gets 39165 physical reads 0 redo size 187470467 bytes sent via SQL*Net to client 2354294 bytes received via SQL*Net from client 213983 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 3209728 rows processed
SQL> |
| Microsoft Windows 2000 [Version 5.00.2195] (C) 版权所有 1985-2000 Microsoft Corp.
C:/>sqlplus "/@lunar as sysdba"
SQL*Plus: Release 9.2.0.1.0 - Production on 星期二 6月 15 17:57:04 2004
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
连接到: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production With the OLAP and Oracle Data Mining options JServer Release 9.2.0.1.0 - Production
SQL> set linesize 1000 SQL> column message format a70 SQL> Select sid, message 2 From v$session_longops 3 Where sid = 9 4 order by start_time 5 /
SID MESSAGE ---------- ---------------------------------------------------------------------- 9 Table Scan: LUNAR.T: 19537 out of 19537 Blocks done 9 SQL Execution: : 1881 out of 1881 units done 9 Table Scan: LUNAR.T: 39173 out of 39173 Blocks done 9 Sort/Merge: : 9976 out of 9976 Blocks done 9 Sort Output: : 9482 out of 9482 Blocks done 9 Table Scan: LUNAR.T: 39173 out of 39173 Blocks done 9 SQL Execution: : 1881 out of 1881 units done 9 Table Scan: LUNAR.T: 39173 out of 39173 Blocks done 9 SQL Execution: : 1881 out of 1881 units done
已选择9行。
SQL> /
SID SERIAL# OPNAME START_TIME PERCENT_COMPLETE ---------- ---------- -------------------------- ---------- ---------------- 10 86 Import Schema Statistics 17:03:44 100 10 86 Delete Schema Statistics 17:04:02 100 9 167 Table Scan 17:05:21 100 9 167 SQL Execution 17:05:21 100 9 167 Table Scan 17:17:11 100 9 167 Sort/Merge 17:18:10 100 9 167 Sort Output 17:18:56 100 9 167 Table Scan 17:28:19 100 9 167 SQL Execution 17:28:19 100 9 167 Table Scan 17:28:52 100 9 167 SQL Execution 17:28:52 100 9 182 Table Scan 17:59:14 100 9 182 Table Scan 18:22:02 100 9 182 Table Scan 18:24:17 2.70339264289179
14 rows selected
SQL> /
SID SERIAL# OPNAME START_TIME PERCENT_COMPLETE ---------- ---------- -------------------------- ---------- ---------------- 10 86 Import Schema Statistics 17:03:44 100 10 86 Delete Schema Statistics 17:04:02 100 9 167 Table Scan 17:05:21 100 9 167 SQL Execution 17:05:21 100 9 167 Table Scan 17:17:11 100 9 167 Sort/Merge 17:18:10 100 9 167 Sort Output 17:18:56 100 9 167 Table Scan 17:28:19 100 9 167 SQL Execution 17:28:19 100 9 167 Table Scan 17:28:52 100 9 167 SQL Execution 17:28:52 100 9 182 Table Scan 17:59:14 100 9 182 Table Scan 18:22:02 100 9 182 Table Scan 18:24:17 44.0354325683506
14 rows selected
SQL> /
SID SERIAL# OPNAME START_TIME PERCENT_COMPLETE ---------- ---------- -------------------------- ---------- ---------------- 10 86 Import Schema Statistics 17:03:44 100 10 86 Delete Schema Statistics 17:04:02 100 9 167 Table Scan 17:05:21 100 9 167 SQL Execution 17:05:21 100 9 167 Table Scan 17:17:11 100 9 167 Sort/Merge 17:18:10 100 9 167 Sort Output 17:18:56 100 9 167 Table Scan 17:28:19 100 9 167 SQL Execution 17:28:19 100 9 167 Table Scan 17:28:52 100 9 167 SQL Execution 17:28:52 100 9 182 Table Scan 17:59:14 100 9 182 Table Scan 18:22:02 100 9 182 Table Scan 18:24:17 70.1860975672019
14 rows selected
SQL> SELECT sid, serial#, opname, 2 TO_CHAR(start_time,'HH24:MI:SS') AS start_time, 3 (sofar/totalwork)*100 AS PERCENT_COMPLETE, 4 ELAPSED_SECONDS 5 FROM v$session_longops;
SID SERIAL# OPNAME START_TIME PERCENT_COMPLETE ELAPSED_SECONDS ---- ---------- ---------------------------- ---------- ---------------- --------------- 10 86 Import Schema Statistics 17:03:44 100 0 10 86 Delete Schema Statistics 17:04:02 100 1 9 167 Table Scan 17:05:21 100 266 9 167 SQL Execution 17:05:21 100 266 9 167 Table Scan 17:17:11 100 59 9 167 Sort/Merge 17:18:10 100 46 9 167 Sort Output 17:18:56 100 82 9 167 Table Scan 17:28:19 100 15 9 167 SQL Execution 17:28:19 100 15 9 167 Table Scan 17:28:52 100 19 9 167 SQL Execution 17:28:52 100 19 9 182 Table Scan 17:59:14 100 336 9 182 Table Scan 18:22:02 100 18 9 182 Table Scan 18:24:17 100 357
14 rows selected
SQL> |
博客介绍了使用v$session_longops视图监视运行时间长的操作(DDL和DML),使用前需满足设置条件且存在统计数据。还给出了显示长时间DDL操作已用时间的脚本,并列举了监视创建大索引和全表扫描两个实例。
1160





