1、前言
v$session和v$process两个视图作为数据库管理了解系统性能、分析系统原因最常用的视图,因此作为一名非菜鸟的DBA管理员,就需要对这两个视图有充分的了解;
2、概念
首先必须了解什么是session:通俗来讲,session 是通信双方从开始通信到通信结束期间的一个上下文(context)。这个上下文是一段位于服务器端的内存:记录了本次连接的客户端机器、通过哪个应用程序、哪个用户在登录等信息。session 是和connection同时建立的,两者是对同一件事情不同层次的描述。简单讲,connection是物理上的客户机同服务器段的通信链路,session是逻辑上的用户同服务器的通信交互。
oracle中一个用户登录oracle服务器的前提,就是该用户具有oracle的 “create session”权限。oracle允许同一个用户在同一个客户机上建立多个同服务器的连接,这一点从oracle的视图V$session中可以看到[select * from v$session;]。每个session都代表了用户与服务器的一个交互。就像两个国家之间可以同时开展很多谈判,经济的,环境的等等。关闭了有关经济的谈判,不会影响到环境谈判的进行。后台进程PMON会每隔一段时间,就会测试用户连接状况,如果连接已断开,PMON会清理现场,释放相关的资源。
1. sqlplus 登录 oracle
这种场景比较容易理解,一个连接对应一个session。
2. 其他客户端工具登录oracle
比如:pl/sql developer 登录oracle。pl/sql developer 可以设置是否每个窗口共用同一个session. 如果想在调试窗口调试存储过程或函数,则必须设置为共享session。如果设置为非共享,则每次打开一个操作窗口,pl/sql developer 会利用最初输入的帐户和口令建立新的connection 和 session.
3. IIS 用程序登录oracle
这种情况下,其实是IIS在登录oracle。connection 和 session 的建立情况和iis机制相关。
“对于Oracle来说,安全的Sessions数应该为Sessions = (IIS process number) * (min pool size)。”
IIS进程:在IIS6.0中,采用了新的进程隔离模式来响应用户的请求,在IIS管理器中,可以设置应用程序池的最大进程数。对于新的WEB应用请求,IIS进程管理器会启动多个W3wp.exe进行响应。
4、java配置连接池
通过在应用配置java的脚本的时候,可以配置相应的session的数量,这样应用连接过来就默认分配了相应的session,而且这些session不管有没有连接都不会被PMON清除掉,减少了数据库的开销。
3、v$session视图的说明
数据库的所有session提供了相应的视图v$session,为所有的dba提供了一个查询和管理的窗口,那么就有必要了解下v$session的相关信息;
Column |
Datatype |
Description |
说明 |
SADDR |
RAW(4 | 8) |
Session address |
|
SID |
NUMBER |
Session identifier |
通过这两个值来确定唯一的一个session |
SERIAL# |
NUMBER |
Session serial number. Used to uniquely identify a session's objects. Guarantees that session-level commands are applied to the correct session objects if the session ends and another session begins with the same session ID. |
|
AUDSID |
NUMBER |
Auditing session ID |
If AUDSID=0, then it is an internally generated SYS session. |
PADDR |
RAW(4 | 8) |
Address of the process that owns the session |
process address,关联v$process的addr字段 |
USER# |
NUMBER |
Oracle user identifier |
同dba_users关联 |
USERNAME |
VARCHAR2(30) |
Oracle username |
同dba_users |
COMMAND |
NUMBER |
Command in progress (last statement parsed). |
session正在执行的sql id,1代表create table,3代表select |
You can find the command name for any value n returned in thisCOMMAND column by running this SQL query: |
|||
|
|||
SELECT command_name |
|||
FROM v$sqlcommand |
|||
WHERE command_type = n; |
|||
|
|||
A value of 0 in this COMMAND column means the command is not recorded in V$SESSION. |
|||
OWNERID |
NUMBER |
Identifier of the user who owns the migratable session; the column contents are invalid if the value is 2147483644 |
|
For operations using Parallel Slaves, interpret this value as a 4-byte value. The low-order 2 bytes represent the session number and the high-order bytes represent the instance ID of the query coordinator. |
|
||
TADDR |
VARCHAR2(8) |
Address of the transaction state object |
当前的transaction address。可以用来关联v$transaction中的addr字段 |
LOCKWAIT |
VARCHAR2(8) |
Address of the lock the session is waiting for; NULL if none |
可以通过这个字段查询出当前正在等待的锁的相关信息。sid + lockwait与v$loc中的sid + kaddr相对应。 |
STATUS |
VARCHAR2(8) |
Status of the session: |
用来判断session状态。Active:正执行SQL语句。inactive:等待操作。killed:被标注为杀死 |
|
|||
ACTIVE - Session currently executing SQL |
|||
INACTIVE - Session which is inactive and either has no configured limits or has not yet exceeded the configured limits |
|||
KILLED - Session marked to be killed |
|||
CACHED - Session temporarily cached for use by Oracle*XA |
|||
SNIPED - An inactive session that has exceeded some configured limits (for example, resource limits specified for the resource manager consumer group or idle_time specified in the user's profile). Such sessions will not be allowed to become active again. |
|||
|
|||
SERVER |
VARCHAR2(9) |
Server type: |
服务类型(一般专用类型) |
|
|||
DEDICATED |
|||
SHARED |
|||
PSEUDO |
|||
POOLED |
|||
NONE |
|||
|
|||
SCHEMA# |
NUMBER |
Schema user identifier |
跟USER#一致 |
SCHEMANAME |
VARCHAR2(30) |
Schema user name |
跟USERNAME一致 |
OSUSER |
VARCHAR2(30) |
Operating system client user name |
客户端操作系统用户名 |
PROCESS |
VARCHAR2(24) |
Operating system client process ID |
客户端process id |
MACHINE |
VARCHAR2(64) |
Operating system machine name |
客户端machine name |
PORT |
NUMBER |
Client port number |
客户端的端口号 |
TERMINAL |
VARCHAR2(30) |
Operating system terminal name |
客户端执行的terminal name |
PROGRAM |
VARCHAR2(48) |
Operating system program name |
客户端应用程序 |
TYPE |
VARCHAR2(10) |
Session type |
用户进程还是后台进程,后台进程一般不能KILL |
SQL_ADDRESS |
RAW(4 | 8) |
Used with SQL_HASH_VALUE to identify the SQL statement that is currently being executed |
|
SQL_HASH_VALUE |
NUMBER |
Used with SQL_ADDRESS to identify the SQL statement that is currently being executed |
|
SQL_ID |
VARCHAR2(13) |
SQL identifier of the SQL statement that is currently being executed |
当前被执行的SQL语句,跟v$sql关联 |
SQL_CHILD_NUMBER |
NUMBER |
Child number of the SQL statement that is currently being executed |
|
SQL_EXEC_START |
DATE |
Time when the execution of the SQL currently executed by this session started; NULL if SQL_ID is NULL |
当前sql的开始运行时间 |
SQL_EXEC_ID |
NUMBER |
SQL execution identifier; NULL if SQL_ID is NULL or if the execution of that SQL has not yet started (see V$SQL_MONITOR) |
|
PREV_SQL_ADDR |
RAW(4 | 8) |
Used with PREV_HASH_VALUE to identify the last SQL statement executed |
|
PREV_HASH_VALUE |
NUMBER |
Used with SQL_HASH_VALUE to identify the last SQL statement executed |
|
PREV_SQL_ID |
VARCHAR2(13) |
SQL identifier of the last SQL statement executed |
刚被执行的SQL语句,从v$sql查询相应的SQL语句 |
PREV_CHILD_NUMBER |
NUMBER |
Child number of the last SQL statement executed |
|
PREV_EXEC_START |
DATE |
SQL execution start of the last executed SQL statement |
|
PREV_EXEC_ID |
NUMBER |
SQL execution identifier of the last executed SQL statement |
|
PLSQL_ENTRY_OBJECT_ID |
NUMBER |
Object ID of the top-most PL/SQL subprogram on the stack; NULL if there is no PL/SQL subprogram on the stack |
|
PLSQL_ENTRY_SUBPROGRAM_ID |
NUMBER |
Subprogram ID of the top-most PL/SQL subprogram on the stack; NULL if there is no PL/SQL subprogram on the stack |
|
PLSQL_OBJECT_ID |
NUMBER |
Object ID of the currently executing PL/SQL subprogram; NULL if executing SQL |
|
PLSQL_SUBPROGRAM_ID |
NUMBER |
Subprogram ID of the currently executing PL/SQL object; NULL if executing SQL |
|
VARCHAR2(48) |
Name of the currently executing module as set by calling theDBMS_APPLICATION_INFO.SET_MODULE procedure |
|
|
MODULE_HASH |
NUMBER |
Hash value of the MODULE column |
|
VARCHAR2(32) |
Name of the currently executing action as set by calling theDBMS_APPLICATION_INFO.SET_ACTION procedure |
|
|
ACTION_HASH |
NUMBER |
Hash value of the ACTION column |
|
CLIENT_INFO |
VARCHAR2(64) |
Information set by the DBMS_APPLICATION_INFO.SET_CLIENT_INFOprocedure |
|
FIXED_TABLE_SEQUENCE |
NUMBER |
This contains a number that increases every time the session completes a call to the database and there has been an intervening select from a dynamic performance table. This column can be used by performance monitors to monitor statistics in the database. Each time the performance monitor looks at the database, it only needs to look at sessions that are currently active or have a higher value in this column than the highest value that the performance monitor saw the last time. All the other sessions have been idle since the last time the performance monitor looked at the database. |
|
ROW_WAIT_OBJ# |
NUMBER |
Object ID for the table containing the row specified in ROW_WAIT_ROW# |
|
ROW_WAIT_FILE# |
NUMBER |
Identifier for the datafile containing the row specified inROW_WAIT_ROW#. This column is valid only if the session is currently waiting for another transaction to commit and the value ofROW_WAIT_OBJ# is not -1. |
|
ROW_WAIT_BLOCK# |
NUMBER |
Identifier for the block containing the row specified in ROW_WAIT_ROW#. This column is valid only if the session is currently waiting for another transaction to commit and the value of ROW_WAIT_OBJ# is not -1. |
|
ROW_WAIT_ROW# |
NUMBER |
Current row being locked. This column is valid only if the session is currently waiting for another transaction to commit and the value ofROW_WAIT_OBJ# is not -1. |
|
TOP_LEVEL_CALL# |
NUMBER |
Oracle top level call number |
|
LOGON_TIME |
DATE |
Time of logon |
|
LAST_CALL_ET |
NUMBER |
If the session STATUS is currently ACTIVE, then the value represents the elapsed time (in seconds) since the session has become active. |
|
If the session STATUS is currently INACTIVE, then the value represents the elapsed time (in seconds) since the session has become inactive. |
|
||
PDML_ENABLED |
VARCHAR2(3) |
This column has been replaced by the PDML_STATUS column |
|
FAILOVER_TYPE |
VARCHAR2(13) |
Indicates whether and to what extent transparent application failover (TAF) is enabled for the session: |
|
|
|
||
NONE - Failover is disabled for this session |
|
||
SESSION - Client is able to fail over its session following a disconnect |
|
||
SELECT - Client is able to fail over queries in progress as well |
|
||
|
|
||
See Also: |
|
||
|
|
||
|
|||
Oracle Database Net Services Administrator's Guide for information on configuring TAF |
|
||
|
|
||
FAILOVER_METHOD |
VARCHAR2(10) |
Indicates the transparent application failover method for the session: |
|
|
|
||
NONE - Failover is disabled for this session |
|
||
BASIC - Client itself reconnects following a disconnect |
|
||
PRECONNECT - Backup instance can support all connections from every instance for which it is backed up |
|
||
|
|
||
FAILED_OVER |
VARCHAR2(3) |
Indicates whether the session is running in failover mode and failover has occurred (YES) or not (NO) |
|
RESOURCE_CONSUMER_GROUP |
VARCHAR2(32) |
Name of the session's current resource consumer group |
|
PDML_STATUS |
VARCHAR2(8) |
If ENABLED, the session is in a PARALLEL DML enabled mode. If DISABLED,PARALLEL DML enabled mode is not supported for the session. If FORCED, the session has been altered to force PARALLEL DML. |
|
PDDL_STATUS |
VARCHAR2(8) |
If ENABLED, the session is in a PARALLEL DDL enabled mode. If DISABLED,PARALLEL DDL enabled mode is not supported for the session. If FORCED, the session has been altered to force PARALLEL DDL. |
|
PQ_STATUS |
VARCHAR2(8) |
If ENABLED, the session is in a PARALLEL QUERY enabled mode. IfDISABLED, PARALLEL QUERY enabled mode is not supported for the session. If FORCED, the session has been altered to force PARALLELQUERY. |
|
CURRENT_QUEUE_DURATION |
NUMBER |
If queued (1), the current amount of time the session has been queued. If not currently queued, the value is 0. |
|
CLIENT_IDENTIFIER |
VARCHAR2(64) |
Client identifier of the session |
|
BLOCKING_SESSION_STATUS |
VARCHAR2(11) |
This column provides details on whether there is a blocking session: |
|
|
|
||
VALID - there is a blocking session, and it is identified in theBLOCKING_INSTANCE and BLOCKING_SESSION columns |
|
||
NO HOLDER - there is no session blocking this session |
|
||
NOT IN WAIT - this session is not in a wait |
|
||
UNKNOWN - the blocking session is unknown |
|
||
|
|
||
BLOCKING_INSTANCE |
NUMBER |
Instance identifier of the blocking session. This column is valid only ifBLOCKING_SESSION_STATUS has the value VALID. |
|
BLOCKING_SESSION |
NUMBER |
Session identifier of the blocking session. This column is valid only ifBLOCKING_SESSION_STATUS has the value VALID. |
|
FINAL_BLOCKING_SESSION_STATUS |
VARCHAR2(11) |
The final blocking session is the final element in the wait chain constructed by following the sessions that are blocked by one another starting with this session. In the case of a cyclical wait chain, one of the sessions in the wait chain will be chosen as the final blocker. |
|
This column provides details on whether there is a final blocking session: |
|
||
|
|
||
VALID - there is a final blocking session and it is identified in theFINAL_BLOCKING_INSTANCE and FINAL_BLOCKING_SESSION columns |
|
||
NO HOLDER - there is no session blocking this session |
|
||
NOT IN WAIT - this session is not in a wait |
|
||
UNKNOWN - the final blocking session is unknown |
|
||
|
|
||
FINAL_BLOCKING_INSTANCE |
NUMBER |
Instance identifier of the final blocking session. This column is valid only ifFINAL_BLOCKING_SESSION_STATUS has the value VALID. |
|
FINAL_BLOCKING_SESSION |
NUMBER |
Session identifier of the blocking session. This column is valid only ifFINAL_BLOCKING_SESSION_STATUS has the value VALID. |
|
SEQ# |
NUMBER |
A number that uniquely identifies the current or last wait (incremented for each wait) |
|
EVENT# |
NUMBER |
Event number |
等待事件的编号跟v$session_wait关联 |
EVENT |
VARCHAR2(64) |
Resource or event for which the session is waiting |
等待事件的解释跟v$session_wait关联 |
P1TEXT |
VARCHAR2(64) |
Description of the first wait event parameter |
对应DBA_EXTENTS的FILE_ID,BLOCK_ID,BLOCKS |
P1 |
NUMBER |
First wait event parameter (in decimal) |
|
P1RAW |
RAW(8) |
||
P2TEXT |
VARCHAR2(64) |
Description of the second wait event parameter |
|
P2 |
NUMBER |
Second wait event parameter (in decimal) |
|
P2RAW |
RAW(8) |
||
P3TEXT |
VARCHAR2(64) |
Description of the third wait event parameter |
|
P3 |
NUMBER |
Third wait event parameter (in decimal) |
|
P3RAW |
RAW(8) |
|
|
WAIT_CLASS_ID |
NUMBER |
Identifier of the class of the wait event |
|
WAIT_CLASS# |
NUMBER |
Number of the class of the wait event |
|
WAIT_CLASS |
VARCHAR2(64) |
Name of the class of the wait event |
|
WAIT_TIME |
NUMBER |
If the session is currently waiting, then the value is 0. If the session is not in a wait, then the value is as follows: |
|
|
|
||
> 0 - Value is the duration of the last wait in hundredths of a second |
|
||
-1 - Duration of the last wait was less than a hundredth of a second |
|
||
-2 - Parameter TIMED_STATISTICS was set to false |
|
||
|
|
||
This column has been deprecated in favor of the columnsWAIT_TIME_MICRO and STATE. |
|
||
SECONDS_IN_WAIT |
NUMBER |
If the session is currently waiting, then the value is the amount of time waited for the current wait. If the session is not in a wait, then the value is the amount of time since the start of the last wait. |
|
This column has been deprecated in favor of the columnsWAIT_TIME_MICRO and TIME_SINCE_LAST_WAIT_MICRO. |
|
||
STATE |
VARCHAR2(19) |
Wait state: |
|
|
|
||
WAITING - Session is currently waiting |
当前session处于等待 |
||
WAITED UNKNOWN TIME - Duration of the last wait is unknown; this is the value when the parameter TIMED_STATISTICS is set to false |
|
||
WAITED SHORT TIME - Last wait was less than a hundredth of a second |
|
||
WAITED KNOWN TIME - Duration of the last wait is specified in theWAIT_TIME column |
|
||
|
|
||
WAIT_TIME_MICRO |
NUMBER |
Amount of time waited (in microseconds). If the session is currently waiting, then the value is the time spent in the current wait. If the session is currently not in a wait, then the value is the amount of time waited in the last wait. |
|
TIME_REMAINING_MICRO |
NUMBER |
Value is interpreted as follows: |
|
|
|
||
> 0 - Amount of time remaining for the current wait (in microseconds) |
|
||
0 - Current wait has timed out |
|
||
-1 - Session can indefinitely wait in the current wait |
|
||
NULL - Session is not currently waiting |
|
||
|
|
||
TIME_SINCE_LAST_WAIT_MICRO |
NUMBER |
Time elapsed since the end of the last wait (in microseconds). If the session is currently in a wait, then the value is 0. |
|
SERVICE_NAME |
VARCHAR2(64) |
Service name of the session |
|
SQL_TRACE |
VARCHAR2(8) |
Indicates whether SQL tracing is enabled (ENABLED) or disabled (DISABLED) |
是否打开跟踪 |
SQL_TRACE_WAITS |
VARCHAR2(5) |
Indicates whether wait tracing is enabled (TRUE) or not (FALSE) |
|
SQL_TRACE_BINDS |
VARCHAR2(5) |
Indicates whether bind tracing is enabled (TRUE) or not (FALSE) |
|
SQL_TRACE_PLAN_STATS |
VARCHAR2(10) |
Frequency at which row source statistics are dumped in the trace files for each cursor: |
|
|
|
||
never |
|
||
first_execution |
|
||
all_executions |
|
||
|
|
||
SESSION_EDITION_ID |
NUMBER |
Shows the value that, in the session, would be reported bysys_context('USERENV', 'SESSION_EDITION_ID') |
|
CREATOR_ADDR |
RAW(4 | 8) |
Address of the creating process or circuit |
|
CREATOR_SERIAL# |
NUMBER |
Serial number of the creating process or circuit |
|
ECID |
VARCHAR2(64) |
Execution context identifier (sent by Application Server) |
|
以上只是标注了常用的字段的注释,如果漏掉的也请各位高手补充,后续随着工作的深入也会相应的补充;
4、v$session在实际工作中的作用
4.1查询锁住对象的会话信息:
SELECT OBJECT_NAME, MACHINE, S.SID, S.SERIAL#
FROM GV$LOCKED_OBJECT L, DBA_OBJECTS O, GV$SESSION S
WHERE L.OBJECT_ID = O.OBJECT_ID AND L.SESSION_ID = S.SID;
4.2查看当前会话所执行的语句以及会话相关信息:
SELECT A.SID, A.SERIAL#, A.USERNAME, A.TERMINAL, A.PROGRAM, S.SQL_TEXT
FROM V$SESSION A, V$SQLAREA S WHERE A.SQL_ADDRESS = S.ADDRESS(+)
AND A.SQL_HASH_VALUE = S.HASH_VALUE(+) ORDER BY A.USERNAME, A.SID;
4.3杀掉会话:
ALTER SYSTEM KILL SESSION 'SID, #SERIAL'; (需要注意当前的session tpye不能是background)