在ORACLE数据库中,如果一个比较大的索引在重建过程中耗费时间比较长,那么怎么查看索引重建耗费的时间,以及完成了多少(比例)了呢,我们可以通过V$SESSION_LONGOPS视图来查看索引重建的时间和进度。
官方文档关于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 the TIMED_STATISTICS or SQL_TRACE parameter to true
Gather statistics for your objects with the ANALYZE statement or the DBMS_STATS package
这个视图显示运行时间超过6秒的各类数据库操作的状态,这些操作包括备份、恢复功能,统计信息收集,以及查询操作等。
要监控查询执行进展情况,你必须是CBO优化器模式,并且满足下面条件:
- TIMED_STATISTICS或SQL_TRACE参数为true。
- 使用DBMS_STATS包或ANLYZE语句收集、分析过对象的统计信息。
| Column | DateType | Description | Description(中文) |
| SID | NUMBER | Session identifier | Session标识 |
| SERIAL# | NUMBER | Session serial number | Session串号 |
| 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 the SQL_HASH_VALUEcolumn to identify the SQL statement associated with the operation | |
| SQL_HASH_VALUE | NUMBER | Used with the value of the SQL_ADDRESS column 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 |
下面我们来演示一下,首先构造了一个大表TEST_INDX,表TEST_INDX上建有一个索引IDX_TEST_INDX。我们开启两个会话窗口:
会话窗口1,执行下面SQL语句:
SQL> SELECT SID, SERIAL#, STATUS FROM V$SESSION WHERE AUDSID=USERENV('SESSIONID');
SID SERIAL# STATUS
---------- ---------- --------
827 746 ACTIVE
SQL> ALTER INDEX IDX_TEST_INDX REBUILD;
Index altered.
SQL>