程式、會話、連線之間的差異
程式、會話、連線之間的差異
--========================
-- 程式、會話、連線之間的差異
--========================
在使用Oracle database的時候,連線與會話是我們經常碰到的詞語之一。咋一看貌似一回事,事實則不然。一個連線上可以建立零個、
一個、甚至多個會話。啊,咋這樣呢?是的,沒錯。這也是我們經常誤解的原因。
各個會話之間是單獨的,獨立於其他會話,即便是同一個連線的多個會話也是如此。
一、幾個術語之間的定義(參照Oracle 9i &10g 程式設計藝術)
連線(connection):連線是從客戶到Oracle 例項的一條物理路徑。連線可以在網路上建立,或者透過IPC 機制建立。通常會在
客戶程式與一個專用伺服器或一個排程器之間建立連線。
會話(session):會話是例項中存在的一個邏輯實體。這就是你的會話狀態(session state),也就是表示特定會話的一組記憶體
中的資料結構.提到"資料庫連線"時,大多數人首先想到的就是“會話”。你要在伺服器中的會話上執行SQL、提交事務和執行儲存過程。
二、透過例子演示來檢視之間的關係
1. 無連線,無會話,無程式的情形
三、session與process的設定關係
session:指定了一個例項中允許的會話數,即能同時登入到資料庫的併發使用者數。
process: 指定了一個例項在作業系統級別能同時執行的程式數,包括後臺程式與伺服器程式。
由上面的分析可知,一個後臺程式可能同時對應對個會話,因此通常sessions的值是大於processes的值
通常的設定公式
sessions = 1.1 * processes + 5
-- 程式、會話、連線之間的差異
--========================
在使用Oracle database的時候,連線與會話是我們經常碰到的詞語之一。咋一看貌似一回事,事實則不然。一個連線上可以建立零個、
一個、甚至多個會話。啊,咋這樣呢?是的,沒錯。這也是我們經常誤解的原因。
各個會話之間是單獨的,獨立於其他會話,即便是同一個連線的多個會話也是如此。
一、幾個術語之間的定義(參照Oracle 9i &10g 程式設計藝術)
連線(connection):連線是從客戶到Oracle 例項的一條物理路徑。連線可以在網路上建立,或者透過IPC 機制建立。通常會在
客戶程式與一個專用伺服器或一個排程器之間建立連線。
會話(session):會話是例項中存在的一個邏輯實體。這就是你的會話狀態(session state),也就是表示特定會話的一組記憶體
中的資料結構.提到"資料庫連線"時,大多數人首先想到的就是“會話”。你要在伺服器中的會話上執行SQL、提交事務和執行儲存過程。
二、透過例子演示來檢視之間的關係
1. 無連線,無會話,無程式的情形
2. 單個連線,單個會話,單個程式-->沒有建立建立連線時,沒有任何會話伺服器程式 [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
3. 無連線,無會話,單個程式-->使用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
4. 單個連線,多個會話,單個程式-->使用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
set autotrace 完成的動作-->從檢視中觀察對應的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 請參考:啟用 AUTOTRACE 功能當啟用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 檢視,根據與上次的差值計算統計資訊並生成執行時的執行計劃以及統計資訊予以返回
5.SID不變,serial#變化的情形-->下面演示在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)
有關kill session的說明,請參考:Oracle 徹底 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
- -->將所有的會話全部退出,下面來檢視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
三、session與process的設定關係
session:指定了一個例項中允許的會話數,即能同時登入到資料庫的併發使用者數。
process: 指定了一個例項在作業系統級別能同時執行的程式數,包括後臺程式與伺服器程式。
由上面的分析可知,一個後臺程式可能同時對應對個會話,因此通常sessions的值是大於processes的值
通常的設定公式
sessions = 1.1 * processes + 5
- -->如在下面的系統的設定中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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22578826/viewspace-703462/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 系統之間差異
- 保持會話連線會話
- oracle 會話,連線Oracle會話
- Bootstrap和Tailwind CSS之間的差異?bootAICSS
- 工作流和BPM之間的差異
- Git比對檔案之間的差異Git
- ORACLE會話連線程式三者總結Oracle會話線程
- js中split,splice,slice方法之間的差異。JS
- 碼農和程式設計師之間的5個關鍵差異程式設計師
- SQL Server 2017 各版本之間的差異SQLServer
- For Update操作分析——不同Oracle版本之間的差異研究Oracle
- MySQL和PostgreSQL在多表連線演算法上的差異MySql演算法
- 網路虛擬化、NFV和SDN之間的差異
- 處理方塊之間的連線線
- 譯:原生iOS應用程式和原生Android應用程式設計之間的差異iOSAndroid程式設計
- 對於 JavaScript 中迴圈之間的技術差異概述JavaScript
- 使用多重 SOA 來消除企業系統之間的差異
- 檢視當前pg會話連線數會話
- Weblogic Server連線池建立會話慢WebServer會話
- 《卸甲筆記》-PostgreSQL和Oracle的SQL差異分析之一:外連線筆記SQLOracle
- 怎樣解決程式語言之間的差異性問題?
- Spark和Hadoop之間的主要技術差異和選擇SparkHadoop
- [AlwaysOn] AlwaysOn可用性組的可用性模式之間的差異模式
- 查詢oracle歷史最大連線[會話]數Oracle會話
- 遠端桌面會話連線限制操作步驟會話
- 異常連線之未授權連線,行為風險分析的有效實踐
- 無程式碼平臺之間的區別,和你想象中的無程式碼開發有沒差異?
- Cisco 交換機之間的連線方法(轉)
- linux之間連線—使用SSHLinux
- 示例解讀 Python 2 和 Python 3 之間的主要差異Python
- db支援共享模式連線的話shutdown通常會很慢(dispatcher shutdown)模式
- 《卸甲筆記》-PostgreSQL和Oracle的SQL差異分析之五:函式的差異(五)筆記SQLOracle函式
- 《卸甲筆記》-PostgreSQL和Oracle的SQL差異分析之五:函式的差異(三)筆記SQLOracle函式
- 《卸甲筆記》-PostgreSQL和Oracle的SQL差異分析之五:函式的差異(一)筆記SQLOracle函式
- 發表話題的連線
- 表與表之間連線式on,using以及自然連線的區別
- [提問]實體bean和會話bean之間的cacheBean會話
- 技術分享|SQL和 NoSQL資料庫之間的差異:MySQL(VS)MongoDB資料庫MySqlMongoDB