欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

对于自适应游标共享的一点补充

程序员文章站 2022-03-20 09:53:18
...

关于自适应游标共享请参加:http://blog.csdn.net/yidian815/article/details/17959907 对自适应游标共享的理解,本人认为难点在于对BIND_SENSITIVE 和BIND_AWARE的认识。再来复习一下: bind_sensitive:oracle认为该语句可能会因为绑定变量的不同而需要采取

关于自适应游标共享请参加:http://blog.csdn.net/yidian815/article/details/17959907

对自适应游标共享的理解,本人认为难点在于对BIND_SENSITIVE 和BIND_AWARE的认识。再来复习一下:

bind_sensitive:oracle认为该语句可能会因为绑定变量的不同而需要采取不同的执行计划,因此需要监控该语句的执行并依据执行结果做出判断。

bind_aware:oracle通过监视已经认定该语句需要针对不同的绑定变量取值采取不同的执行计划。

现在就好出现如下的问题:

oracle认定什么样式的sql语句是bind_sensitve?

对于bind_aware的语句,oracle是如何根据不同的变量取值来选择执行计划的?

在回答这些问题之前,先来看一下测试环境

SQL> desc t2;
 名称						       是否为空? 类型
 ----------------------------------------------------- -------- ------------------------------------
 ID								NUMBER
 RTYPE								VARCHAR2(20)
 SEL								NUMBER

SQL> select column_name,histogram from dba_tab_cols where table_name='T2' AND OWNER='EASY1';

COLUMN_NAME		       HISTOGRAM
------------------------------ ---------------
ID			       NONE
RTYPE			       FREQUENCY
SEL			       NONE


SQL> select rtype,count(1),min(sel),max(sel) from t2 group by rtype order by 3;

RTYPE		       COUNT(1)   MIN(SEL)   MAX(SEL)
-------------------- ---------- ---------- ----------
1			      1 7.6295E-06 7.6295E-06
2			      2 .000015259 .000015259
3			      4 .000030518 .000030518
4			      8 .000061036 .000061036
5			     16 .000122072 .000122072
6			     32 .000244144 .000244144
7			     64 .000488289 .000488289
8			    128 .000976577 .000976577
9			    256 .001953155 .001953155
10			    512  .00390631  .00390631
11			   1024 .007812619 .007812619

RTYPE		       COUNT(1)   MIN(SEL)   MAX(SEL)
-------------------- ---------- ---------- ----------
12			   2048 .015625238 .015625238
13			   4096 .031250477 .031250477
14			   8192 .062500954 .062500954
15			  16384 .125001907 .125001907
16			  32768 .250003815 .250003815
17			  65536  .50000763  .50000763
sel代表rype在整张表中的选择性。

首先我们猜测第一个问题的答案。

因为bind_sensitive是针对绑定变量的不同取值而论的,因此我们认为只有具有绑定变量的语句才可能是bind_sensitive的。

SQL> select sum(id) from t2 where rtype=16;

   SUM(ID)
----------
1610596352

SQL> select sum(id) from t2 where rtype=1;

   SUM(ID)
----------
	 1
SQL> l
  1* select sql_text,is_bind_sensitive,is_bind_aware from v$sql where sql_text like 'select sum(id) from t2%'
SQL> /

SQL_TEXT						     I I
------------------------------------------------------------ - -
select sum(id) from t2 where rtype=1			     N N
select sum(id) from t2 where rtype=16			     N N
是不是具有绑定变量就一定会是bind_sensitive?
SQL> var v varchar2(100)
SQL> select sum(id) from t2 where rtype=:v;

   SUM(ID)
----------

SQL> select sum(id) from t2 where id=:v;

   SUM(ID)
----------
SQL> l
  1* select sql_text,is_bind_sensitive,is_bind_aware from v$sql where sql_text like 'select sum(id) from t2%'
SQL> /

SQL_TEXT						     I I
------------------------------------------------------------ - -
select sum(id) from t2 where rtype=1			     N N
select sum(id) from t2 where id=:v			     N N
select sum(id) from t2 where rtype=:v			     Y N
select sum(id) from t2 where rtype=16			     N N
看来不光需要具有绑定变量,还需要在绑定变量所在列上具有直方图统计信息才可以。当然这是在等值操作的情况下,在其他情况下那?在这里我们不做分析,至少在目前的情况下,oracle对bind_sensitive具有如下限制(官方文档):

The optimizer has peeked at the bind values to generate selectivity estimates.

A histogram exists on the column containing the bind value.

  • 下面一段文字来自网络,尽快参考

    Q: What triggers a cursor to be marked "bind sensitive"?

    A: Our goal is to consider many types of predicates where the selectivity can change when the bind value changes. In this first version of the feature, we only handle equality predicates where a histogram exists on the column and range predicates (with or without histogram). We do not currently consider LIKE predicates, but it is on the top of our list for future work.

    下面我们来看看bind_aware的语句是如何工作的,为了简化操作,我们直接使用BIND_AWARE hint。关于该hint的使用,有如下解释:

    From 11.1.0.7 onward it is possible to skip the monitoring that is required to detect bind-sensitive queries by using the BIND_AWARE hint. In the following example, the presence of the hint tells the optimizer that we believe the query is bind-sensitive, so it should use bind-aware cursor sharing from the first execution.

    SELECT /*+ BIND_AWARE */ MAX(id) FROM acs_test_tab WHERE record_type = :l_record_type;

    The hint will only work if the query uses bind variables in WHERE clause predicates referencing columns with histograms.

    There is also a NO_BIND_AWARE hint that tells the optimizer to ignore bind-sensitive queries, effectively hiding the query from the adaptive cursor sharing functionality.

    Bind-aware cursor sharing has a small overhead associated with it, which is why Oracle use the "adaptive" approach to identifying queries that would benefit from bind-aware cursor sharing. Adding the hint to queries that will not benefit from it is a waste.

    在进一步实验之前,创建表t1

    SQL> create table t1 as select * from t2 where 1 =2;
    
    表已创建。
    
    SQL> alter table t1 modify rtype number;
    
    表已更改。
    
    SQL> insert into t1 select * from t2;
    
    已创建 131071 行。
    
    SQL> commit;
    
    提交完成。
    
    SQL> create index i1 on t1(rtype);
    
    索引已创建。
    SQL> exec dbms_stats.gather_table_stats(user,'t1',cascade=>true,estimate_percent=>null,method_opt=>'for all columns size auto,for columns rtype size 40');
    
    PL/SQL 过程已成功完成。
    
    SQL> select table_name,column_name,endpoint_number,to_char(endpoint_value),endpoint_actual_value from user_histograms where table_name='T1';
    
    TABLE_NAME COLUMN_NAME		ENDPOINT_NUMBER TO_CHAR(ENDPOINT_VALUE) 		 ENDPOINT_ACTUAL_VALUE
    ---------- -------------------- --------------- ---------------------------------------- ------------------------------
    T1	   RTYPE			      1 1
    T1	   RTYPE			      3 2
    T1	   RTYPE			      7 3
    T1	   RTYPE			     15 4
    T1	   RTYPE			     31 5
    T1	   RTYPE			     63 6
    T1	   RTYPE			    127 7
    T1	   RTYPE			    255 8
    T1	   RTYPE			    511 9
    T1	   RTYPE			   1023 10
    T1	   RTYPE			   2047 11
    
    TABLE_NAME COLUMN_NAME		ENDPOINT_NUMBER TO_CHAR(ENDPOINT_VALUE) 		 ENDPOINT_ACTUAL_VALUE
    ---------- -------------------- --------------- ---------------------------------------- ------------------------------
    T1	   RTYPE			   4095 12
    T1	   RTYPE			   8191 13
    T1	   RTYPE			  16383 14
    T1	   RTYPE			 131071 17
    T1	   ID				      0 1
    T1	   SEL				      0 .00000762951094834821
    T1	   ID				      1 131071
    T1	   SEL				      1 .87501335164416
    
    已选择19行。
    SQL> select rtype,count(1),min(sel),max(sel) from t1 group by rtype order by 3;
    
         RTYPE   COUNT(1)	MIN(SEL)   MAX(SEL)
    ---------- ---------- ---------- ----------
    	 1	    1 7.6295E-06 7.6295E-06
    	 2	    2 .000015259 .000015259
    	 3	    4 .000030518 .000030518
    	 4	    8 .000061036 .000061036
    	 5	   16 .000122072 .000122072
    	 6	   32 .000244144 .000244144
    	 7	   64 .000488289 .000488289
    	 8	  128 .000976577 .000976577
    	 9	  256 .001953155 .001953155
    	10	  512  .00390631  .00390631
    	11	 1024 .007812619 .007812619
    
         RTYPE   COUNT(1)	MIN(SEL)   MAX(SEL)
    ---------- ---------- ---------- ----------
    	12	 2048 .015625238 .015625238
    	13	 4096 .031250477 .031250477
    	14	 8192 .062500954 .062500954
    	17     114688 .875013352 .875013352
    对bind_aware的实验过程如下:
    SQL> alter system flush shared_pool;
    
    系统已更改。
    
    SQL> var vr number;
    SQL> exec :vr := 1
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
    	 1
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y Y    --由于使用了bind_aware HINT 
    
    SQL> @show_sel
    
    ADDRESS 	 HASH_VALUE SQL_ID	  CHILD_NUMBER PREDICATE				  RANGE_ID LOW	      HIGH
    ---------------- ---------- ------------- ------------ ---------------------------------------- ---------- ---------- ----------
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     0 =VR						 0 0.000007   0.000008 --根据直方图计算出rtype=1的选择性
    
    SQL> exec :vr := 2
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
    	 5
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N  --逐步淘汰出内存
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y Y
    
    SQL> @show_sel
    
    ADDRESS 	 HASH_VALUE SQL_ID	  CHILD_NUMBER PREDICATE				  RANGE_ID LOW	      HIGH
    ---------------- ---------- ------------- ------------ ---------------------------------------- ---------- ---------- ----------
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     1 =VR						 0 0.000007   0.000017    --由于rtype=2的选择性不再0.00007~0.00008之间,所以生成新的子游标,由于新游标和旧游标的执行计划相同,所以进行合并,子游标0被设置为非共享,逐步淘汰出内存
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     0 =VR						 0 0.000007   0.000008
    
    SQL> exec :vr := 1
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
    	 1
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y Y   --使用新的子游标,不再使用0号子游标
    
    SQL> @sho_sel
    SP2-0310: 无法打开文件 "sho_sel.sql"
    SQL> @show_sel
    
    ADDRESS 	 HASH_VALUE SQL_ID	  CHILD_NUMBER PREDICATE				  RANGE_ID LOW	      HIGH
    ---------------- ---------- ------------- ------------ ---------------------------------------- ---------- ---------- ----------
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     1 =VR						 0 0.000007   0.000017
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     0 =VR						 0 0.000007   0.000008
    
    SQL> exec :vr := 3
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
    	22
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y Y
    
    SQL> @show_sel
    
    ADDRESS 	 HASH_VALUE SQL_ID	  CHILD_NUMBER PREDICATE				  RANGE_ID LOW	      HIGH
    ---------------- ---------- ------------- ------------ ---------------------------------------- ---------- ---------- ----------
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     2 =VR						 0 0.000007   0.000034
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     1 =VR						 0 0.000007   0.000017
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     0 =VR						 0 0.000007   0.000008
    
    SQL> exec :vr := 1
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
    	 1
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y Y
    
    SQL> exec :vr := 8
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
         24512
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y Y
    
    SQL> @show_sel
    
    ADDRESS 	 HASH_VALUE SQL_ID	  CHILD_NUMBER PREDICATE				  RANGE_ID LOW	      HIGH
    ---------------- ---------- ------------- ------------ ---------------------------------------- ---------- ---------- ----------
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     3 =VR						 0 0.000007   0.001074
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     2 =VR						 0 0.000007   0.000034
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     1 =VR						 0 0.000007   0.000017
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     0 =VR						 0 0.000007   0.000008
    
    SQL> exec :vr := 14
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
     100659200
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y Y
    
    SQL> @show_sel
    
    ADDRESS 	 HASH_VALUE SQL_ID	  CHILD_NUMBER PREDICATE				  RANGE_ID LOW	      HIGH
    ---------------- ---------- ------------- ------------ ---------------------------------------- ---------- ---------- ----------
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     4 =VR						 0 0.000007   0.068751
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     3 =VR						 0 0.000007   0.001074
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     2 =VR						 0 0.000007   0.000034
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     1 =VR						 0 0.000007   0.000017
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     0 =VR						 0 0.000007   0.000008
    
    SQL> exec :vr := 15
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y Y
    
    已选择6行。
    
    SQL> @show_sel
    
    ADDRESS 	 HASH_VALUE SQL_ID	  CHILD_NUMBER PREDICATE				  RANGE_ID LOW	      HIGH
    ---------------- ---------- ------------- ------------ ---------------------------------------- ---------- ---------- ----------
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     5 =VR						 0 0.000003   0.068751 --由于15不存在,所以选择性向下扩充
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     4 =VR						 0 0.000007   0.068751
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     3 =VR						 0 0.000007   0.001074
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     2 =VR						 0 0.000007   0.000034
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     1 =VR						 0 0.000007   0.000017
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     0 =VR						 0 0.000007   0.000008
    
    已选择6行。
    
    SQL> exec :vr := 17
    
    PL/SQL 过程已成功完成。
    
    SQL> select /*+ bind_aware */ sum(id) from t1 where rtype = :vr;
    
       SUM(ID)
    ----------
    8455659520
    
    SQL> @show_sql
    
    SQL_TEXT						     EXECUTIONS I I I
    ------------------------------------------------------------ ---------- - - -
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      2 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y N
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y Y
    select /*+ bind_aware */ sum(id) from t1 where rtype = :vr	      1 Y Y Y
    
    已选择7行。
    
    SQL> @show_sel
    
    ADDRESS 	 HASH_VALUE SQL_ID	  CHILD_NUMBER PREDICATE				  RANGE_ID LOW	      HIGH
    ---------------- ---------- ------------- ------------ ---------------------------------------- ---------- ---------- ----------
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     6 =VR						 0 0.787503   0.962503 --新的子游标的执行计划于旧子游标不同,所以均保留
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     5 =VR						 0 0.000003   0.068751
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     4 =VR						 0 0.000007   0.068751
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     3 =VR						 0 0.000007   0.001074
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     2 =VR						 0 0.000007   0.000034
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     1 =VR						 0 0.000007   0.000017
    00000000DD40C0E0 2679189014 082txyqgv2bhq	     0 =VR						 0 0.000007   0.000008
    
    已选择7行。
    
    由此可见,计算绑定变量的谓词选择性在bind_aware中扮演者重要角色