码迷,mamicode.com
首页 > 数据库 > 详细

Oracle 11.2.0.1.0下ACS测试令人失望

时间:2014-12-10 18:10:59      阅读:921      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   使用   

   在版本11.2.0.1.0,ACS(adaptiver cursor sharing)很令我失望,测试的结果是:

    1. 在绑定窥探有效的情况下,使用绑定变量,在直方图准确的情况下,CBO总是以执行的第一条SQL的执行计划为准。

    2. 在绑定窥探失效的情况下,使用绑定变量,CBO完全忽略直方图。

   在这个版本下的SQL,如果是偏于OLAP的SQL,还是不用使用绑定变量为佳,跟10g一样的处理方式。

SQL> select * from v$version;

BANNER
----------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

SQL> show parameter _optimizer_adaptive_cursor_sharing;
NAME                                 TYPE        VALUE
------------------------------------ ----------- ----------------------
_optimizer_adaptive_cursor_sharing   boolean     TRUE
SQL> show parameter _optimizer_extended_cursor_sharing;
NAME                                      TYPE        VALUE
------------------------------------   ----------- ---------------------
_optimizer_extended_cursor_sharing       string      UDO
_optimizer_extended_cursor_sharing_rel   string      SIMPLE

--制造数据
drop table test purge;
create table test(id number,name varchar2(10),descri varchar2(1000));
begin
for i in 1 .. 100000 loop
  insert into test values(i,‘100‘,lpad(‘a‘,900,‘b‘));
  end loop;
  commit;
end;
/
update test set name=‘200‘ where rownum=1;
commit;
create index ind_name on TEST(name) nologging;

--由于在11.2.0.1.0版本下用dbms_stats收集直方图不准确,所以用analyze

--在Oracle 11.2.0.1.0下dbms_stats.gather_table_stats收集直方图不准

SQL> analyze table test compute statistics for table for columns name size 2;
--exec dbms_stats.gather_table_stats(user,‘test‘,cascade => true,method_opt =>‘for all columns size skewonly‘);
--数据分布
SQL> select name,count(1) from test group by name;
NAME         COUNT(1)
---------- ----------
100             99999
200                 1
SQL> set autotrace traceonly
SQL> select * from test where name=‘100‘;
已选择99999行。
执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 99999 |    86M|  2664   (1)| 00:00:38 |
|*  1 |  TABLE ACCESS FULL| TEST | 99999 |    86M|  2664   (1)| 00:00:38 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"=‘100‘)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
      13515  consistent gets
        133  physical reads
          0  redo size
    1564422  bytes sent via SQL*Net to client
      73663  bytes received via SQL*Net from client
       6668  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      99999  rows processed
SQL> select * from test where name=‘200‘;
执行计划
----------------------------------------------------------
Plan hash value: 688048857
----------------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |     1 |   909 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST     |     1 |   909 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IND_NAME |     1 |       |     1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("NAME"=‘200‘)
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          3  consistent gets
          0  physical reads
          0  redo size
       1342  bytes sent via SQL*Net to client
        337  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

SQL> set autotrace off

--使用绑定窥探,先使用查大部分数据的条件,可以看到两个都是全表扫描

--先看到绑定窥探是有效的

SQL> show parameter _optim_peek_user_binds;
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
_optim_peek_user_binds               boolean     TRUE
SQL> alter system flush shared_pool;
SQL> alter system flush buffer_cache;
SQL> var ccc varchar2(10);
SQL> exec :ccc:=‘100‘;
SQL> set autotrace trace stat
SQL> select * from test where name=:ccc;
已选择99999行。
统计信息
----------------------------------------------------------
        354  recursive calls
          0  db block gets
      13565  consistent gets
       6853  physical reads
          0  redo size
    1564422  bytes sent via SQL*Net to client
      73664  bytes received via SQL*Net from client
       6668  SQL*Net roundtrips to/from client
          7  sorts (memory)
          0  sorts (disk)
      99999  rows processed
SQL> set autotrace off
SQL> select hash_value, child_number from v$sql
    where sql_text = ‘select * from test where name=:ccc‘;
HASH_VALUE CHILD_NUMBER
---------- ------------
1301684711            0
SQL> select * from table(dbms_xplan.display_cursor(1301684711,0));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
HASH_VALUE  1301684711, child number 0
--------------------------------------
select * from test where name=:ccc
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |  2664 (100)|          |
|*  1 |  TABLE ACCESS FULL| TEST | 99999 |    86M|  2664   (1)| 00:00:38 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"=:CCC)
SQL> exec :ccc:=‘200‘;
SQL> set autotrace trace stat
SQL> select * from test where name=:ccc;
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
       6849  consistent gets
          0  physical reads
          0  redo size
       1342  bytes sent via SQL*Net to client
        338  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL> set autotrace off

--可以看到执行两条SQL,都是一个执行计划
SQL> select hash_value, child_number from v$sql
    where sql_text = ‘select * from test where name=:ccc‘;
HASH_VALUE CHILD_NUMBER
---------- ------------
1301684711            0


--使用绑定窥探,先使用查小部分数据的条件

SQL> alter system flush shared_pool;
SQL> alter system flush buffer_cache;
SQL> select hash_value, child_number from v$sql
    where sql_text = ‘select * from test where name=:ccc‘;
未选定行
SQL> exec :ccc:=‘200‘;
SQL> set autotrace trace stat
SQL> select * from test where name=:ccc;
统计信息
----------------------------------------------------------
        354  recursive calls
          0  db block gets
         53  consistent gets
         27  physical reads
          0  redo size
       1342  bytes sent via SQL*Net to client
        338  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          7  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL> set autotrace off
SQL> select hash_value, child_number from v$sql
    where sql_text = ‘select * from test where name=:ccc‘;
HASH_VALUE CHILD_NUMBER
---------- ------------
1301684711            0
SQL> select * from table(dbms_xplan.display_cursor(1301684711,0));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------
HASH_VALUE  1301684711, child number 0
--------------------------------------
select * from test where name=:ccc
Plan hash value: 688048857
----------------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |       |       |     2 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST     |     1 |   909 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IND_NAME |     1 |       |     1   (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("NAME"=:CCC)
SQL> exec :ccc:=‘100‘;
SQL> set autotrace trace stat
SQL> select * from test where name=:ccc;
已选择99999行。
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      20106  consistent gets
       6884  physical reads
          0  redo size
    1564422  bytes sent via SQL*Net to client
      73664  bytes received via SQL*Net from client
       6668  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      99999  rows processed
SQL> set autotrace off

--可以看到执行两条SQL,都是一个执行计划
SQL> select hash_value, child_number from v$sql
    where sql_text = ‘select * from test where name=:ccc‘;
HASH_VALUE CHILD_NUMBER
---------- ------------
1301684711            0


--不使用绑定窥探,先使用查小部分数据的条件

SQL> alter session set "_optim_peek_user_binds"=false;
SQL> alter system flush shared_pool;
SQL> alter system flush buffer_cache;
SQL> select hash_value, child_number from v$sql
    where sql_text = ‘select * from test where name=:ccc‘;
SQL> exec :ccc:=‘200‘;
SQL> set autotrace trace stat
SQL> select * from test where name=:ccc;
统计信息
----------------------------------------------------------
        354  recursive calls
          0  db block gets
       6899  consistent gets
       6853  physical reads
          0  redo size
       1342  bytes sent via SQL*Net to client
        338  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          7  sorts (memory)
          0  sorts (disk)
          1  rows processed
SQL> set autotrace off
SQL> select hash_value, child_number from v$sql
    where sql_text = ‘select * from test where name=:ccc‘;
HASH_VALUE CHILD_NUMBER
---------- ------------
1301684711            0
--看到是全表扫描,正确的是索引扫描
SQL> select * from table(dbms_xplan.display_cursor(1301684711,0));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
HASH_VALUE  1301684711, child number 0
--------------------------------------
select * from test where name=:ccc
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |  2663 (100)|          |
|*  1 |  TABLE ACCESS FULL| TEST | 50000 |    43M|  2663   (1)| 00:00:38 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("NAME"=:CCC)
SQL> exec :ccc:=‘100‘;
SQL> set autotrace trace stat
SQL> select * from test where name=:ccc;
已选择99999行。
统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      13515  consistent gets
          0  physical reads
          0  redo size
    1564422  bytes sent via SQL*Net to client
      73664  bytes received via SQL*Net from client
       6668  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      99999  rows processed
SQL> set autotrace off
SQL> select hash_value, child_number from v$sql
    where sql_text = ‘select * from test where name=:ccc‘;
HASH_VALUE CHILD_NUMBER
---------- ------------
1301684711            0

Oracle 11.2.0.1.0下ACS测试令人失望

标签:des   style   blog   http   io   ar   color   os   使用   

原文地址:http://blog.csdn.net/stevendbaguo/article/details/41847829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!