oracle硬解析、軟解析、軟軟解析

硬解析和軟解析有相同的一步,而軟軟解析與硬解析、軟解析徹底不同。先來講下理論上的東西,而後來作個實驗。sql

硬解析過程:緩存

1.語法、語義及權限檢查;oop

2.查詢轉換(經過應用各類不一樣的轉換技巧,會生成語義上等同的新的SQL語句,如count(1)會轉爲count(*));orm

3.根據統計信息生成執行計劃(找出成本最低的路徑,這一步比較耗時);blog

4.將遊標信息(執行計劃)保存到庫緩存。hash

軟解析過程:io

1.語法、語義及權限檢查;table

2.將整條SQL hash後從庫緩存中執行計劃。form

軟解析對比硬解析省了三個步驟。test

軟軟解析過程:

要徹底理解軟軟解析先要理解遊標的概念,當執行SQL時,首先要打開遊標,執行完成後,要關閉遊標,遊標能夠理解爲SQL語句的一個句柄。

在執行軟軟解析以前,首先要進行軟解析,MOS上說執行3次的SQL語句會把遊標緩存到PGA,這個遊標一直開着,當再有相同的SQL執行時,則跳過解析的全部過程直接去取執行計劃。

SQL> drop table test purge;

SQL> alter system flush shared_pool;

SQL> create table test as select * from dba_objects where 1<>1;

SQL> exec dbms_stats.gather_table_stats(user,'test');

硬解析:

SQL> select * from test where object_id=20;

未選定行

SQL> select * from test where object_id=30;

未選定行

SQL> select * from test where object_id=40;

未選定行

SQL> select * from test where object_id=50;

未選定行

軟解析:

SQL> var oid number;

SQL> exec :oid:=20;

SQL> select * from test where object_id=:oid;

未選定行

SQL> exec :oid:=30;

SQL> select * from test where object_id=:oid;

未選定行

SQL> exec :oid:=40;

SQL> select * from test where object_id=:oid;

未選定行

SQL> exec :oid:=50;

SQL> select * from test where object_id=:oid;

未選定行

軟軟解析:

SQL> begin

for i in 1..4 loop

execute immediate 'select * from test where object_id=:i' using i;

end loop;

end;

/

SQL> col sql_text format a40

SQL> select sql_text,s.PARSE_CALLS,loads,executions from v$sql s

where sql_text like 'select * from test where object_id%'

order by 1,2,3,4;

SQL_TEXT PARSE_CALLS LOADS EXECUTIONS

 

 

 

能夠看到軟解析與軟軟解析相比,軟軟解析只是解析一次。

字段解釋:

PARSE_CALLS 解析的次數

LOADS 硬解析的次數

EXECUTIONS 執行的次數

相關文章
相關標籤/搜索