您的位置:首页 > 其它

进程、会话、连接之间的差异(开启autotrace功能)

2013-04-22 10:59 369 查看
--========================

-- 进程、会话、连接之间的差异

--========================

在使用Oracle database的时候,连接与会话是我们经常碰到的词语之一。咋一看貌似一回事,事实则不然。一个连接上可以建立零个、

一个、甚至多个会话。啊,咋这样呢?是的,没错。这也是我们经常误解的原因。

各个会话之间是单独的,独立于其他会话,即便是同一个连接的多个会话也是如此。

一、几个术语之间的定义(参照Oracle 9i &10g 编程艺术)

连接(connection):连接是从客户到Oracle 实例的一条物理路径。连接可以在网络上建立,或者通过IPC 机制建立。通常会在

客户进程与一个专用服务器或一个调度器之间建立连接。

会话(session):会话是实例中存在的一个逻辑实体。这就是你的会话状态(session state),也就是表示特定会话的一组内存

中的数据结构.提到"数据库连接"时,大多数人首先想到的就是“会话”。你要在服务器中的会话上执行SQL、提交事务和运行存储过程。

二、通过例子演示来查看之间的关系

1. 无连接,无会话,无进程的情形

[sql]
view plaincopyprint?

-->没有建立建立连接时,没有任何会话服务器进程

[oracle@odbp ~]$ ps -ef | grep oracleorcl
oracle 5685 5446 0 19:30 pts/1 00:00:00 grep oracleorcl

[oracle@odbp ~]$ sqlplus /nolog

SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jun 27 19:30:49 2011
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.

idle> ho ps -ef | grep oracleorcl -->使用nolog登录是同样也看不到任何会话服务器进程

oracle 5691 5686 0 19:31 pts/0 00:00:00 /bin/bash -c ps -ef | grep oracleorcl

-->没有建立建立连接时,没有任何会话服务器进程
[oracle@odbp ~]$ ps -ef | grep oracleorcl
oracle    5685  5446  0 19:30 pts/1    00:00:00 grep oracleorcl

[oracle@odbp ~]$ sqlplus /nolog

SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jun 27 19:30:49 2011
Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.

idle> ho ps -ef | grep oracleorcl    -->使用nolog登录是同样也看不到任何会话服务器进程
oracle    5691  5686  0 19:31 pts/0    00:00:00 /bin/bash -c ps -ef | grep oracleorcl

2. 单个连接,单个会话,单个进程

[sql]
view plaincopyprint?

-->使用scott身份登录,有一个对应的服务器进程被产生

idle> conn scott/tiger
Connected.
scott@ORCL> select sid,serial#,username from v$session where username is not null;

SID SERIAL# USERNAME
---------- ---------- -------------------------

159 5 SCOTT

scott@SQL> ho ps -ef | grep oracleorcl
oracle 5696 5686 0 19:32 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle 5699 5686 0 19:32 pts/0 00:00:00 /bin/bash -c ps -ef | grep oracleorcl

-->使用scott身份登录,有一个对应的服务器进程被产生
idle> conn scott/tiger
Connected.
scott@ORCL> select sid,serial#,username from v$session where username is not null;

SID    SERIAL# USERNAME
---------- ---------- -------------------------
159          5 SCOTT

scott@SQL> ho ps -ef | grep oracleorcl
oracle    5696  5686  0 19:32 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle    5699  5686  0 19:32 pts/0    00:00:00 /bin/bash -c ps -ef | grep oracleorcl

3. 无连接,无会话,单个进程

[sql]
view plaincopyprint?

-->使用disconnect断开会话,但对应的服务器进程并没有撤销,直到使用exit则对应的服务器进程被释放

scott@SQL> disconnect
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

-->此时开启另外一个会话session2来查看scott的会话是否还存在,从下面的查询中已经不存在scott用户的会话

sys@ORCL> select sid,serial#,username from v$session where username='SCOTT';

no rows selected

scott@SQL> ho ps -ef | grep 5696 -->对应的后台进程依然存在

oracle 5696 5686 0 19:32 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle 5702 5686 0 19:32 pts/0 00:00:00 /bin/bash -c ps -ef | grep 5696

scott@ORCL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@odbp admin]$ ps -ef | grep 5696 -->exit命令退出后则相应的进程5696被释放

oracle 4082 16943 0 19:45 pts/0 00:00:00 grep 5696

-->使用disconnect断开会话,但对应的服务器进程并没有撤销,直到使用exit则对应的服务器进程被释放
scott@SQL> disconnect
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

-->此时开启另外一个会话session2来查看scott的会话是否还存在,从下面的查询中已经不存在scott用户的会话
sys@ORCL> select sid,serial#,username from v$session where username='SCOTT';

no rows selected

scott@SQL> ho ps -ef | grep 5696  -->对应的后台进程依然存在
oracle    5696  5686  0 19:32 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle    5702  5686  0 19:32 pts/0    00:00:00 /bin/bash -c ps -ef | grep 5696

scott@ORCL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@odbp admin]$ ps -ef | grep 5696  -->exit命令退出后则相应的进程5696被释放
oracle    4082 16943  0 19:45 pts/0    00:00:00 grep 5696

4. 单个连接,多个会话,单个进程

[sql]
view plaincopyprint?

-->从视图中观察对应的session与后台进程

-->在session1中使用scott登录

idle> conn scott/tiger;
Connected.

-->在session2 中使用sys帐户登录

sys@ORCL> select sid,serial#,username from v$session where username is not null;

SID SERIAL# USERNAME
---------- ---------- ------------------------------

141 4 SYS
159 5 SCOTT

-->在session1中开启autotrace功能

scott@ORCL> set autotrace on

-->可以看到在session2的v$session视图查询时多出了一个账户为scott,但SID与SERIAL#与之前不同的记录

sys@ORCL> set linesize 160
sys@ORCL> SELECT spid, s.sid, s.serial#,s.status,s.username, p.program
2 FROM v$process p, v$session s
3 WHERE p.addr = s.paddr
4 and s.username='SCOTT';

SPID SID SERIAL# STATUS USERNAME PROGRAM
------------ ---------- ---------- -------- ------------------------- --------------------------------------

4602 159 5 INACTIVE SCOTT oracle@oradb.robinson.com (TNS V1-V3)
4602 139 25 INACTIVE SCOTT oracle@oradb.robinson.com (TNS V1-V3)

sys@ORCL> ho ps -ef | grep 4602
oracle 4602 4499 0 18:36 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle 4856 4655 0 18:47 pts/3 00:00:00 /bin/bash -c ps -ef | grep 4602

sys@ORCL> ho ps -ef | grep oracleorcl
oracle 4602 4499 0 18:36 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle 4656 4655 0 18:36 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle 4859 4655 0 18:47 pts/3 00:00:00 /bin/bash -c ps -ef | grep oracleorcl

-->从上面的查询结果可以看出,SCOTT用户对应的后台进程仅有一个,其spid为4062

-->从视图中观察对应的session与后台进程
-->在session1中使用scott登录
idle> conn scott/tiger;
Connected.

-->在session2 中使用sys帐户登录
sys@ORCL> select sid,serial#,username from v$session where username is not null;

SID    SERIAL# USERNAME
---------- ---------- ------------------------------
141          4 SYS
159          5 SCOTT

-->在session1中开启autotrace功能
scott@ORCL> set autotrace on

-->可以看到在session2的v$session视图查询时多出了一个账户为scott,但SID与SERIAL#与之前不同的记录
sys@ORCL> set linesize 160
sys@ORCL> SELECT spid, s.sid, s.serial#,s.status,s.username, p.program
2  FROM v$process p, v$session s
3  WHERE p.addr = s.paddr
4  and s.username='SCOTT';

SPID                SID    SERIAL# STATUS   USERNAME                  PROGRAM
------------ ---------- ---------- -------- ------------------------- --------------------------------------
4602                159          5 INACTIVE SCOTT                     oracle@oradb.robinson.com (TNS V1-V3)
4602                139         25 INACTIVE SCOTT                     oracle@oradb.robinson.com (TNS V1-V3)

sys@ORCL> ho ps -ef | grep 4602
oracle    4602  4499  0 18:36 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle    4856  4655  0 18:47 pts/3    00:00:00 /bin/bash -c ps -ef | grep 4602

sys@ORCL> ho ps -ef | grep oracleorcl
oracle    4602  4499  0 18:36 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle    4656  4655  0 18:36 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
oracle    4859  4655  0 18:47 pts/3    00:00:00 /bin/bash -c ps -ef | grep oracleorcl

-->从上面的查询结果可以看出,SCOTT用户对应的后台进程仅有一个,其spid为4062

set autotrace 完成的动作

[sql]
view plaincopyprint?

当启用set autotrace功能后,通常会创建一个新的会话用于监控当前的操作并返回统计信息,下面描述其过程
a.在session1执行一个查询,则此时原来创建的会话(159,5)执行DML或DQL操作
b.新创建的会话(139,25)会话则开始查询V$SESSTAT 视图来记住实际会话(即运行DML 的会话)的初始统计值
c.原会话(139,25)中得DML或DQL操作执行
d.新会话(139,25)将再次查询V$SESSTAT 视图,根据与上次的差值计算统计信息并生成执行时的执行计划以及统计信息予以返回

当启用set autotrace功能后,通常会创建一个新的会话用于监控当前的操作并返回统计信息,下面描述其过程
a.在session1执行一个查询,则此时原来创建的会话(159,5)执行DML或DQL操作
b.新创建的会话(139,25)会话则开始查询V$SESSTAT 视图来记住实际会话(即运行DML 的会话)的初始统计值
c.原会话(139,25)中得DML或DQL操作执行
d.新会话(139,25)将再次查询V$SESSTAT 视图,根据与上次的差值计算统计信息并生成执行时的执行计划以及统计信息予以返回

有关启用set autotrace 请参考:启用 AUTOTRACE 功能

[sql]
view plaincopyprint?

-->下面演示在session1中的查询

scott@ORCL> select count(1) from emp;

COUNT(1)
----------

14

Execution Plan
----------------------------------------------------------

Plan hash value: 2937609675

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

| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FULL SCAN| PK_EMP | 14 | 1 (0)| 00:00:01 |
-------------------------------------------------------------------

Statistics
----------------------------------------------------------

296 recursive calls
0 db block gets
54 consistent gets
1 physical reads
0 redo size
411 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
1 rows processed

scott@ORCL> set autotrace off;
-->在session2中再次执行查询,可以看到会话139,25已经被释放

sys@ORCL> /

SPID SID SERIAL# STATUS USERNAME PROGRAM
------------ ---------- ---------- -------- ------------------------- --------------------------------------

4602 159 5 INACTIVE SCOTT oracle@oradb.robinson.com (TNS V1-V3)

-->下面演示在session1中的查询
scott@ORCL> select count(1) from emp;

COUNT(1)
----------
14

Execution Plan
----------------------------------------------------------
Plan hash value: 2937609675

-------------------------------------------------------------------
| Id  | Operation        | Name   | Rows  | Cost (%CPU)| Time     |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT |        |     1 |     1   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE  |        |     1 |            |          |
|   2 |   INDEX FULL SCAN| PK_EMP |    14 |     1   (0)| 00:00:01 |
-------------------------------------------------------------------

Statistics
----------------------------------------------------------
296  recursive calls
0  db block gets
54  consistent gets
1  physical reads
0  redo size
411  bytes sent via SQL*Net to client
385  bytes received via SQL*Net from client
2  SQL*Net roundtrips to/from client
6  sorts (memory)
0  sorts (disk)
1  rows processed

scott@ORCL> set autotrace off;
-->在session2中再次执行查询,可以看到会话139,25已经被释放
sys@ORCL> /

SPID                SID    SERIAL# STATUS   USERNAME                  PROGRAM
------------ ---------- ---------- -------- ------------------------- --------------------------------------
4602                159          5 INACTIVE SCOTT                     oracle@oradb.robinson.com (TNS V1-V3)

5.SID不变,serial#变化的情形

[sql]
view plaincopyprint?

-->将所有的会话全部退出,下面来查看SID不变而serial#变化的情形

[oracle@oradb ~]$ ps -ef | grep oracleorcl -->此时Oracle数据库无任何服务器进程

oracle 26767 16943 0 19:49 pts/0 00:00:00 grep oracleorcl
[oracle@oradb ~]$ sqlplus scott/tiger@orcl

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

scott@ORCL> select sid,serial#,username from v$session where username='SCOTT';

SID SERIAL# USERNAME
---------- ---------- ------------------------------

134 39 SCOTT

scott@ORCL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options
[uniread] Saved history (652 lines)
[oracle@oradb ~]$ sqlplus scott/tiger@orcl

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

scott@ORCL> select sid,serial#,username from v$session where username='SCOTT';

SID SERIAL# USERNAME
---------- ---------- ------------------------------

134 41 SCOTT
-->从上面的情形可以看出尽管scott用户退出后重新登录,依然使用了相同的SID,因此在执行kill session时,一定要注意SID,serial#

-->两者的值,以免kill掉不该kill的session

-->将所有的会话全部退出,下面来查看SID不变而serial#变化的情形
[oracle@oradb ~]$ ps -ef | grep oracleorcl    -->此时Oracle数据库无任何服务器进程
oracle   26767 16943  0 19:49 pts/0    00:00:00 grep oracleorcl
[oracle@oradb ~]$ sqlplus scott/tiger@orcl

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

scott@ORCL> select sid,serial#,username from v$session where username='SCOTT';

SID    SERIAL# USERNAME
---------- ---------- ------------------------------
134         39 SCOTT

scott@ORCL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options
[uniread] Saved history (652 lines)
[oracle@oradb ~]$ sqlplus scott/tiger@orcl

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

scott@ORCL> select sid,serial#,username from v$session where username='SCOTT';

SID    SERIAL# USERNAME
---------- ---------- ------------------------------
134         41 SCOTT
-->从上面的情形可以看出尽管scott用户退出后重新登录,依然使用了相同的SID,因此在执行kill session时,一定要注意SID,serial#
-->两者的值,以免kill掉不该kill的session

有关kill session的说明,请参考:Oracle 彻底 kill session

三、session与process的设置关系

session:指定了一个实例中允许的会话数,即能同时登录到数据库的并发用户数。

process: 指定了一个实例在操作系统级别能同时运行的进程数,包括后台进程与服务器进程。

由上面的分析可知,一个后台进程可能同时对应对个会话,因此通常sessions的值是大于processes的值

通常的设置公式

sessions = 1.1 * processes + 5

[sql]
view plaincopyprint?

-->如在下面的系统的设置中processes得值为150,session的值设定为170,

scott@ORCL> select name,value from v$parameter where name='processes';

NAME VALUE
-------------------- --------------------

processes 150

scott@ORCL> select name,value from v$parameter where name='sessions';

NAME VALUE
-------------------- --------------------

sessions 170

scott@ORCL> select 150*1.1+5 from dual;

150*1.1+5
----------

170
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: