共計 4007 個字符,預計需要花費 11 分鐘才能閱讀完成。
本篇文章為大家展示了如何理解 Oracle 邏輯讀和物理讀,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
1. 物理讀(physical read)
物理讀即是把數據從磁盤讀入到 buffer catch 的過程。通常情況下是,如果需要數據的時候發現不存在于 buffer catch 當中,即 oracle 就會執行物理讀。
當數據塊第一次讀取到, 就會緩存到 buffer cache 中, 而第二次讀取和修改該數據塊時就在內存 buffer cache 了 以下是例子:
1.1 *** 次讀取:
C:\Documents and Settings\Paul Yi sqlplus /as sysdba
SQL*Plus: Release 9.2.0.4.0 – Production on Thu Feb 28 09:32:04 2008
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 – Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.4.0 – Production
SQL set autotrace traceonly
SQL select * from test;
Execution Plan
———————————————————-
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=4 Bytes=8)
1 0 TABLE ACCESS (FULL) OF TEST (Cost=2 Card=4 Bytes=8)
Statistics
———————————————————-
175 recursive calls
0 db block gets
24 consistent gets
9 physical reads –9 個物理讀
0 redo size
373 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
1 rows processed
1.2 第二次讀取
SQL select * from test;
Execution Plan
———————————————————-
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=4 Bytes=8)
1 0 TABLE ACCESS (FULL) OF TEST (Cost=2 Card=4 Bytes=8)
Statistics
———————————————————-
0 recursive calls
0 db block gets
7 consistent gets
0 physical reads – 沒有發生物理讀了, 直接從 buffer cache 中讀取了
0 redo size
373 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
1.3 數據塊被重新讀入 buffer cache , 這種發生在
如果有新的數據需要被讀入 Buffer Cache 中,而 Buffer Cache 又沒有足夠的空閑空間,Oracle 就根據 LRU 算法將 LRU 鏈表中 LRU 端的數據置換出去。當這些數據被再次訪問到時,需要重新從磁盤讀入。
SQL alter session set events immediate trace name flush_cache – 清空數據緩沖區
Session altered.
SQL select * from test;
Execution Plan
———————————————————-
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=4 Bytes=8)
1 0 TABLE ACCESS (FULL) OF TEST (Cost=2 Card=4 Bytes=8)
Statistics
———————————————————-
0 recursive calls
0 db block gets
7 consistent gets
6 physical reads – 又重新發生了物理讀
0 redo size
373 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
2. 邏輯讀(buffer read)
邏輯讀指的就是從 (或者視圖從)Buffer Cache 中讀取數據塊。按照訪問數據塊的模式不同,可以分為即時讀(Current Read) 和一致性讀(Consistent Read)。注意:邏輯 IO 只有邏輯讀,沒有邏輯寫。
即時讀
即時讀即讀取數據塊當前的 *** 數據。任何時候在 Buffer Cache 中都只有一份當前數據塊。即時讀通常發生在對數據進行修改、刪除操作時。這時,進程會給數據加上行級鎖,并且標識數據為“臟”數據。
SQL select * from test for update;
Execution Plan
———————————————————-
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=4 Bytes=8)
1 0 FOR UPDATE
2 1 TABLE ACCESS (FULL) OF TEST (Cost=2 Card=4 Bytes=8)
Statistics
———————————————————-
0 recursive calls
1 db block gets
14 consistent gets
0 physical reads
252 redo size
386 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL
一致性讀
Oracle 是一個多用戶系統。當一個會話開始讀取數據還未結束讀取之前,可能會有其他會話修改它將要讀取的數據。如果會話讀取到修改后的數據,就會造成數據的不一致。一致性讀就是為了保證數據的一致性。在 Buffer Cache 中的數據塊上都會有 *** 一次修改數據塊時的 SCN。如果一個事務需要修改數據塊中數據,會先在回滾段中保存一份修改前數據和 SCN 的數據塊,然后再更新 Buffer Cache 中的數據塊的數據及其 SCN,并標識其為“臟”數據。當其他進程讀取數據塊時,會先比較數據塊上的 SCN 和自己的 SCN。如果數據塊上的 SCN 小于等于進程本身的 SCN,則直接讀取數據塊上的數據; 如果數據塊上的 SCN 大于進程本身的 SCN,則會從回滾段中找出修改前的數據塊讀取數據。通常,普通查詢都是一致性讀。
下面這個例子幫助大家理解一下一致性讀:
會話 1 中:
SQL select * from test;
ID
———-
1000
SQL update test set id=2000;
1 row updated.
會話 2 中:
SQL set autotrace on
SQL select * from test;
ID
———-
1000
Execution Plan
———————————————————-
0 SELECT STATEMENT ptimizer=CHOOSE (Cost=2 Card=4 Bytes=8)
1 0 TABLE ACCESS (FULL) OF TEST (Cost=2 Card=4 Bytes=8)
Statistics
———————————————————-
0 recursive calls
0 db block gets
9 consistent gets 沒有事物做 update 時 是 7 consistent gets 說明多了 2 個 consistent gets 這 2 個是要從回滾段中獲取的
0 physical reads
52 redo size
373 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL
上述內容就是如何理解 Oracle 邏輯讀和物理讀,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注丸趣 TV 行業資訊頻道。