您好,登錄后才能下訂單哦!
一、Cursor_sharing簡介:
這個參數是用來告訴Oracle在什么情況下可以共享游標,即SQL重用。
Cursor_sharing參數有3個值可以設置:
1)、EXACT:通常來說,exact值是Oracle推薦的,也是默認的,它要求SQL語句在完全相同時才會重用,否則會被重新執行硬解析操作。
2)、SIMILAR:similar是在Oracle認為某條SQL語句的謂詞條件可能會影響到它的執行計劃時,才會被重新分析,否則將重用SQL。
3)、FORCE:force是在任何情況下,無條件重用SQL。
4)、在Oracle12c版本以后,不建議設置成SIMILAR。手冊已經把該參數廢棄。
備注:上面所說的SQL重用,僅僅是指謂詞條件不同的SQL語句,實際上這樣的SQL基本上都在執行同樣的業務操作。
二、在Cursor_sharing參數值不同的時對SQL的影響:
2.1 創建實驗環境:
11G:創建基表,錄入數據。
SYS@orcl> create table test (id number,name varchar2(10));
SYS@orcl> insert into test values (1,'aa');
SYS@orcl> insert into test values (2,'bb');
SYS@orcl> insert into test values (3,'cc');
SYS@orcl> commit;
創建三張實驗用表:
SYS@orcl> create table test_exact as select from test;
SYS@orcl> create table test_similar as select from test;
SYS@orcl> create table test_force as select * from test;
設置跟蹤trace
SYS@orcl> oradebug setmypid
SYS@orcl> oradebug tracefile_name
測試exact:
SYS@orcl> alter session set cursor_sharing=exact;
SYS@orcl> alter session set sql_trace=true;
SYS@orcl> select * from test_exact where id=1;
ID NAME
1 aa
SYS@orcl> select * from test_exact where id=2;
ID NAME
2 bb
SYS@orcl> select * from test_exact where id=3;
ID NAME
3 cc
SYS@orcl> alter session set sql_trace=false;
select from test_exact where id=2
select from test_exact where id=3
select * from test_exact where id=1
[oracle@orcl ~]$ tkprof /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2849.trc out.txt aggregate=no
SQL ID: 22cdhbrvt2nmw
Plan Hash: 3210958934
select *
from
test_exact where id=1
call count cpu elapsed disk query current rows
Parse 1 0.00 0.01 2 2 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 4 0 1
total 4 0.00 0.01 2 6 0 1
Misses in library cache during parse: 1 --硬解析1次
Optimizer mode: ALL_ROWS
Parsing user id: SYS
SQL ID: f9kq2n9utcww7
Plan Hash: 3210958934
select *
from
test_exact where id=2
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 1 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 4 0 1
total 4 0.00 0.00 0 5 0 1
Misses in library cache during parse: 1 --硬解析1次
Optimizer mode: ALL_ROWS
Parsing user id: SYS
SQL ID: 22cdhbrvt2nmw
Plan Hash: 3210958934
select *
from
test_exact where id=1
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 4 0 1
total 4 0.00 0.00 0 4 0 1
Misses in library cache during parse: 0 --軟解析
Optimizer mode: ALL_ROWS
Parsing user id: SYS
總結:當cursor_sharing=exact時,只有當SQL語句是完全一樣的情況下才能被重用。
測試SIMILAR:
SYS@orcl> oradebug setmypid
SYS@orcl> oradebug tracefile_name
SYS@orcl> alter session set cursor_sharing=similar;
SYS@orcl> alter session set sql_trace=true;
SYS@orcl> select * from test_similar where id=1;
ID NAME
1 aa
SYS@orcl> select * from test_similar where id=2;
ID NAME
2 bb
select from test_similar where id=:"SYS_B_0"
select from test_similar where id=:"SYS_B_0"
select * from test_similar where id=:"SYS_B_0"
SYS@orcl> alter session set sql_trace=false;
分析trace文件:
SQL ID: 6wvc0ymwz50uq
Plan Hash: 3269012161
select *
from
test_similar where id=:"SYS_B_0"
call count cpu elapsed disk query current rows
Parse 3 0.00 0.00 1 1 0 0
Execute 3 0.00 0.00 1 3 0 0
Fetch 5 0.00 0.00 0 11 0 2
total 11 0.00 0.00 2 15 0 2
Misses in library cache during parse: 3 --執行三次硬解析
Optimizer mode: ALL_ROWS
Parsing user id: SYS
對于SIMILAR的情況,如果CBO發現被綁定變量的謂詞還有其他的執行計劃可以選擇時,如果謂詞條件的值有變化,就將會產生一個新的子游標,而不是重用之前的SQL;如果謂詞沒有其他的執行計劃可選擇,則忽略謂詞的值,重用之前的SQL。
進一步測試:
SYS@orcl> alter system flush shared_pool;
SYS@orcl> alter system flush buffer_cache;
SYS@orcl> insert into test_similar values (1,'abc');
SYS@orcl> commit;
SYS@orcl> create index ind_test_similar on test_similar(id);
SYS@orcl> exec dbms_stats.gather_table_stats(user,'test_similar',cascade=>true);
SYS@orcl> alter session set cursor_sharing=similar;
SYS@orcl> alter session set sql_trace=true;
SYS@orcl> select * from test_similar where id=1 and name='aa';
ID NAME
1 aa
SYS@orcl> select * from test_similar where id=1 and name='abc';
ID NAME
1 abc
select * from test_similar where id=:"SYS_B_0" and name=:"SYS_B_1
查看trace:
SQL ID: 23gux1agm4fnt
Plan Hash: 3269012161
select *
from
test_similar where id=:"SYS_B_0" and name=:"SYS_B_1"
call count cpu elapsed disk query current rows
Parse 2 0.00 0.00 0 0 0 0
Execute 2 0.00 0.00 0 0 0 0
Fetch 4 0.00 0.00 0 9 0 2
total 8 0.00 0.00 0 9 0 2
Misses in library cache during parse: 1 --硬解析1次
Optimizer mode: ALL_ROWS
Parsing user id: SYS
測試FORCE:
SYS@orcl> oradebug setmypid
SYS@orcl> oradebug tracefile_name
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_4104.trc
SYS@orcl> alter session set cursor_sharing=force;
SYS@orcl> alter session set sql_trace=true;
SYS@orcl> select * from test_force where id=1;
ID NAME
1 aa
SYS@orcl> select * from test_force where id=2;
ID NAME
2 bb
SYS@orcl> select * from test_force where id=1;
ID NAME
1 aa
select * from test_force where id=:"SYS_B_0"
查看trace:
SQL ID: 5my70999m011j
Plan Hash: 1419416768
select *
from
test_force where id=:"SYS_B_0"
call count cpu elapsed disk query current rows
Parse 3 0.00 0.00 1 1 0 0
Execute 3 0.00 0.00 1 1 0 0
Fetch 6 0.00 0.00 0 12 0 3
total 12 0.00 0.00 2 14 0 3
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
總結:force是在任何情況下,無條件重用SQL。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。