Oracle體系結構之SQL語句的執行過程

一般來說,數據庫處理SQL都會經過三個過程:解析(parse)、執行(exec)、返回結果(fetch)

1.解析

當用戶發起一個SQL語句時,Oracle通過Server Process 接收SQL語句到達oracle實例,並在Shared pool 中的 Library Cache 查找是否存在該語句對應執行計劃的緩存。

如果不存在則將該SQL進行硬解析(Hard parse),生成最優化的執行計劃(plan),並將該執行計劃等信息載入Library Cache。

如果存在則不經過硬解析,而是直接進行軟解析(Soft parse),從而減少數據庫的分析時間。

2.執行

server process首先在buffer cache中查找是否存在該執行計劃所對應的數據塊,如果存在,就直接進行DML操作(邏輯IO),否則應從數據文件中將數據塊讀取到buffer cache中,再進行DML操作(物理IO)。

3.返回結果

對於SELECT語句需要返回結果,首先看是否需要排序,如果需要則排序後返回給用戶。

對於其他DML語句(insert/delete/update),則無需返回結果。當buffer cache中的數據塊被修改時,server process將自動記錄buffer的改變過程到SGA中的redo log buffer,最終分別由DBWR和LGWR進程負責將buffer cache中的髒數據塊和redo log buffer中的日誌寫到磁盤中的data file和redo log file。


解析類型:硬解析、軟解析、軟軟解析

對SQL的解析都需要頻繁地訪問數據字典

硬解析

判斷SQL語句是否存在語法、語義的問題

判斷SQL語句所涉及的對象(表、視圖)是否存在

判斷執行SQL語句的用戶對涉及的對象是否有權限

選擇最優的執行方案,生成執行計劃

其中生成執行計劃最消耗系統資源(CPU、I/O、Memory),尤其是CPU和I/O資源

軟解析:只判斷SQL語句的語法、語義、對象權限,而不生成執行計劃

軟軟解析:不解析

SQL> select name,value from v$sysstat where name like 'parse%' ;
NAME                                VALUE
------------------------------ ----------
parse time cpu                       1339
parse time elapsed                  17374
parse count (total)                 23639
parse count (hard)                   3060
parse count (failures)                149
parse count (describe)                  9


命中率:

命中率在Oracle數據庫的多個地方都會被提起

例如當一個進程需要訪問數據時,首先確定數據是否存在Buffer Cache中,如果存在(稱爲高速緩存命中),則直接讀取數據(邏輯IO/內存讀);如果不存在(稱爲高速緩存未命中),則需要在Buffer Cache中尋找足夠的空間將磁盤上需要的數據塊複製到Buffer Cache中(物理IO/硬盤讀)。

命中率=邏輯IO/(邏輯IO+物理IO)*100%

注:如果命中率低肯定有問題,但命中率高不一定沒問題(邏輯IO遠高於物理IO,但物理IO仍舊很高的情況下)

可以通過操作系統命令vmstat、iostat查看當前系統的IO情況

[oracle@ora11g ~]$ vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 374572  25260 788168    0    0   262    34  239  253  1  4 80 16  0
 2  0      0 374556  25268 788172    0    0     0    36  909 1341  0  1 99  0  0
 0  0      0 374556  25268 788172    0    0     0     0  876 1248  0  1 99  0  0
 0  0      0 374556  25268 788172    0    0    16    80  974 1495  0  1 99  0  0
 0  0      0 374556  25276 788164    0    0     0    48  966 1428  0  1 99  0  0
[oracle@ora11g ~]$ iostat 1 5
Linux 2.6.18-308.el5 (ora11g.example.com)       07/17/2013
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.00    0.00    3.85   15.19    0.00   79.96
Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda              33.72      1551.26       202.49    1052263     137352
sda1              0.10         2.58         0.01       1749          4
sda2             22.27      1256.66       141.92     852432      96271
sda3              0.07         2.37         0.00       1610          0
sda4              0.01         0.02         0.00         11          0
sda5              2.17        58.06         4.54      39382       3080
sda6              2.40        60.32        15.53      40920      10532
sda7              2.85        77.55        16.47      52605      11173
sda8              3.01        76.16         8.77      51663       5949
sda9              0.80        17.03        15.25      11554      10343
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.55    0.00    0.00   99.45
Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               5.00        32.00       128.00         32        128
sda1              0.00         0.00         0.00          0          0
sda2              0.00         0.00         0.00          0          0
sda3              0.00         0.00         0.00          0          0
sda4              0.00         0.00         0.00          0          0
sda5              0.00         0.00         0.00          0          0
sda6              1.00         0.00        32.00          0         32
sda7              2.00        32.00        32.00         32         32
sda8              1.00         0.00        32.00          0         32
sda9              1.00         0.00        32.00          0         32
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.26    0.00    0.77    0.26    0.00   98.71
Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               2.00         0.00       104.00          0        104
sda1              0.00         0.00         0.00          0          0
sda2              2.00         0.00       104.00          0        104
sda3              0.00         0.00         0.00          0          0
sda4              0.00         0.00         0.00          0          0
sda5              0.00         0.00         0.00          0          0
sda6              0.00         0.00         0.00          0          0
sda7              0.00         0.00         0.00          0          0
sda8              0.00         0.00         0.00          0          0
sda9              0.00         0.00         0.00          0          0
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.26    0.00    0.26    0.00    0.00   99.48
Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               4.00         0.00        32.00          0         32
sda1              0.00         0.00         0.00          0          0
sda2              0.00         0.00         0.00          0          0
sda3              0.00         0.00         0.00          0          0
sda4              0.00         0.00         0.00          0          0
sda5              1.00         0.00         8.00          0          8
sda6              1.00         0.00         8.00          0          8
sda7              1.00         0.00         8.00          0          8
sda8              0.00         0.00         0.00          0          0
sda9              1.00         0.00         8.00          0          8
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.78    0.00    0.00   99.22
Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda              13.00       160.00       208.00        160        208
sda1              0.00         0.00         0.00          0          0
sda2              4.00         0.00        80.00          0         80
sda3              0.00         0.00         0.00          0          0
sda4              0.00         0.00         0.00          0          0
sda5              0.00         0.00         0.00          0          0
sda6              1.00         0.00        32.00          0         32
sda7              3.00        64.00        32.00         64         32
sda8              4.00        96.00        32.00         96         32
sda9              1.00         0.00        32.00          0         32

也可以通過v$buffer_pool_statistics視圖查看命中率的情況

SQL> SELECT NAME, PHYSICAL_READS, DB_BLOCK_GETS, CONSISTENT_GETS,(1-(PHYSICAL_READS/(DB_BLOCK_GETS+CONSISTENT_GETS)))*100 "Hit Ratio"      FROM V$BUFFER_POOL_STATISTICS WHERE NAME='DEFAULT';
NAME                 PHYSICAL_READS DB_BLOCK_GETS CONSISTENT_GETS  Hit Ratio
-------------------- -------------- ------------- --------------- ----------
DEFAULT                       12786         14986          158305 92.6216595


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章