标签: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--由于在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;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
标签:des style blog http io ar color os 使用
原文地址:http://blog.csdn.net/stevendbaguo/article/details/41847829