[转]Oracle 初始化参数之cursor_sharing
本文转自:http://www.cnblogs.com/Richardzhu/archive/2013/01/21/2869837.html
一、Cursor_sharing简介:
这个参数是用来告诉Oracle在什么情况下可以共享游标,即SQL重用。
Cursor_sharing参数有3个值可以设置:
1)、EXACT:通常来说,exact值是Oracle推荐的,也是默认的,它要求SQL语句在完全相同时才会重用,否则会被重新执行硬解析操作。
2)、SIMILAR:similar是在Oracle认为某条SQL语句的谓词条件可能会影响到它的执行计划时,才会被重新分析,否则将重用SQL。
3)、FORCE:force是在任何情况下,无条件重用SQL。
备注:上面所说的SQL重用,仅仅是指谓词条件不同的SQL语句,实际上这样的SQL基本上都在执行同样的业务操作。
二、在Cursor_sharing参数值不同的时对SQL的影响:
2.1 创建实验环境:
 ----首先创建一张jack表----  1 SQL> create table jack (id int,name varchar2(10));
 2 
 3 Table created.
 4 
 ----产生一些数据----  5 SQL> insert into jack values(1,'aa');
 6 
 7 1 row created.
 8 
 9 SQL> insert into jack values(2,'bb');
10 
11 1 row created.
12 
13 SQL> insert into jack values(3,'cc');
14 
15 1 row created.
16 
17 SQL> insert into jack values(4,'dd');
18 
19 1 row created.
20 
21 SQL> commit;
22 
23 Commit complete.
24 
25 SQL> select * from jack;
26 
27     ID NAME
28 ---------- ----------
29      1 aa
30      2 bb
31      3 cc
32      4 dd
33 
 ----创建下面实验将要用到的三张表---- 34 SQL> create table jack_exact as select * from jack;
35 
36 Table created.
37 
38 SQL> create table jack_similar as select * from jack;
39 
40 Table created.
41 
42 SQL> create table jack_force as select * from jack;
43 
44 Table created.
45 
 ----查看该session的trace文件的路径---- 46 SQL> @/u01/scripts/showtrace
47 
48 trace_file_name
49 --------------------------------------------------------------------------------
50 /u01/app/oracle/diag/rdbms/yft/yft/trace/yft_ora_5329.trc
2.2 cursor_sharing=exact的情况:
  ----将cursor_sharing设置为exact----   1 SQL> alter session set cursor_sharing=exact;
  2 
  3 Session altered.
  4 
  5 SQL> alter session set sql_trace=true;
  6 
  7 Session altered.
  8 
  9 SQL> select * from jack_exact where id=1;
 10 
 11     ID NAME
 12 ---------- ----------
 13      1 aa
 14 
 15 SQL> select * from jack_exact where id=3;
 16 
 17     ID NAME
 18 ---------- ----------
 19      3 cc
 20   21 SQL> select * from jack_exact where id=1;
 22 
 23     ID NAME
 24 ---------- ----------
 25      1 aa
 26 
 27 SQL> alter session set sql_trace=false;
 28 
 29 Session altered.
 30 
  ----从下面的查询可以看出执行了两次硬解析----  31 SQL> select sql_text from v$sql where sql_text like 'select * from jack_exact where%';
 32 
 33 SQL_TEXT
 34 --------------------------------------------------------------------------------
 35 select * from jack_exact where id=1
 36 select * from jack_exact where id=3
 37 
  ----查看trace文件,通过tkprof工具       [oracle@yft ~]$ tkprof /u01/app/oracle/diag/rdbms/yft/yft/trace/yft_ora_5329.trc out.txt aggregate=no sys=no----  38 SQL ID: fnggytkynxz04
 39 Plan Hash: 4127630146
 40 select * 
 41 from
 42  jack_exact where id=1
 43 
 44 
 45 call     count       cpu    elapsed       disk      query    current        rows
 46 ------- ------  -------- ---------- ---------- ---------- ----------  ----------
 47 Parse        1      0.00       0.00          0          1          0           0
 48 Execute      1      0.00       0.00          0          0          0           0
 49 Fetch        2      0.00       0.00          0          4          0           1
 50 ------- ------  -------- ---------- ---------- ---------- ----------  ----------
 51 total        4      0.00       0.00          0          5          0           1
 52 
 53 Misses in library cache during parse: 1    ---id=1,执行一次硬解析
 54 Optimizer mode: ALL_ROWS
 55 Parsing user id: 105  
 56 
 57 Rows     Row Source Operation
 58 -------  ---------------------------------------------------
 59       1  TABLE ACCESS FULL JACK_EXACT (cr=4 pr=0 pw=0 time=0 us cost=3 size=20 card=1)
 60 
 61 ********************************************************************************
 62 
 63 SQL ID: 1n0paamkf7sup
 64 Plan Hash: 4127630146
 65 select * 
 66 from
 67  jack_exact where id=3
 68 
 69 
 70 call     count       cpu    elapsed       disk      query    current        rows
 71 ------- ------  -------- ---------- ---------- ---------- ----------  ----------
 72 Parse        1      0.00        
                    
                     
                    
                 
                    
                