日期:2014-05-16  浏览次数:20656 次

在数据分布严重不均的列上使用绑定变量容易错过更好的执行计划

在数据分布严重不均的列上使用绑定变量容易错过更好的执行计划,原因在于9i引入bind peeking机制导致的。

SQL> desc tt
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
OWNER VARCHAR2(30)
OBJECT_NAME VARCHAR2(128)
SUBOBJECT_NAME VARCHAR2(30)
OBJECT_ID NUMBER
DATA_OBJECT_ID NUMBER
OBJECT_TYPE VARCHAR2(19)
CREATED DATE
LAST_DDL_TIME DATE
TIMESTAMP VARCHAR2(19)
STATUS VARCHAR2(7)
TEMPORARY VARCHAR2(1)
GENERATED VARCHAR2(1)
SECONDARY VARCHAR2(1)

SQL> select count(*) from tt;

COUNT(*)
----------
46080

SQL> update tt set object_id=999 where object_id>5;

已更新46064行。

SQL> commit;

提交完成。

SQL> create index idx_tt on tt(object_id) tablespace users;

索引已创建。

SQL> set autotrace traceonly
SQL> variable i number
SQL> exec :i := 2;

PL/SQL 过程已成功完成。

SQL> set linesize 200
SQL> select * from tt where object_id=:i;


执行计划
----------------------
Plan hash value: 6977672

--------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------
| 0 | SELECT STATEMENT | | 548 | 96996 | 22 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TT | 548 | 96996 | 22 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_TT | 219 | | 19 (0)| 00:00:01 |
--------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("OBJECT_ID"=TO_NUMBER(:I))

Note
-----
- dynamic sampling used for this statement


统计信息
----------------------
0 recursive calls
0 db block gets
7 consistent gets
0 physical reads
0 redo size
1472 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed

SQL> exec :i := 999;

PL/SQL 过程已成功完成。

SQL> select * from tt where object_id=:i;

已选择46064行。


执行计划
----------------------
Plan hash value: 6977672

--------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------
| 0 | SELECT STATEMENT | | 548 | 96996 | 22 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TT | 548 | 96996 | 22 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_TT | 219 | | 19 (0)| 00:00:01 |
--------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("OBJECT_ID"=TO_NUMBER(:I))

Note
-----
- dynamic sampling used for this statement


统计信息
----------------------
0 recursive calls
0 db block gets
6774 con